forked from keel1982/pyTrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windfeed.py
140 lines (110 loc) · 5.32 KB
/
windfeed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# PyAlgoTrade
#
# Copyright 2011-2015 Gabriel Martin Becedillas Ruiz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
.. moduleauthor:: Gabriel Martin Becedillas Ruiz <gabriel.becedillas@gmail.com>
"""
from pyalgotrade.barfeed import sqlitefeed
from pyalgotrade.barfeed import common
from pyalgotrade.utils import dt
from pyalgotrade import bar
from pyalgotrade import dataseries
import datetime
######################################################################
## Yahoo Finance CSV parser
# Each bar must be on its own line and fields must be separated by comma (,).
#
# Bars Format:
# Date,Open,High,Low,Close,Volume,Adj Close
#
# The csv Date column must have the following format: YYYY-MM-DD hh:mm:ss
def parse_date(dateTime):
# Sample: 2005-12-30 09:24:00
ret= datetime.datetime.strptime(dateTime, "%Y-%m-%d %H:%M:%S")
return ret
class RowParser(sqlitefeed.RowParser):
def __init__(self, dailyBarTime, frequency, timezone=None, sanitize=False):
self.__dailyBarTime = dailyBarTime
self.__frequency = frequency
self.__timezone = timezone
self.__sanitize = sanitize
def __parseDate(self, dateString):
ret = parse_date(dateString)
# Time on Yahoo! Finance CSV files is empty. If told to set one, do it.
if self.__dailyBarTime is not None:
ret = datetime.datetime.combine(ret, self.__dailyBarTime)
# Localize the datetime if a timezone was given.
if self.__timezone:
ret = dt.localize(ret, self.__timezone)
return ret
def getFieldNames(self):
# It is expected for the first row to have the field names.
return None
def getDelimiter(self):
return ","
def parseBar(self, csvRowDict):
dateTime = self.__parseDate(csvRowDict["TimeStamp"])
close = float(csvRowDict["Close"])
open_ = float(csvRowDict["Open"])
high = float(csvRowDict["High"])
low = float(csvRowDict["Low"])
volume = float(csvRowDict["Volume"])
adjClose = float(csvRowDict["Close"])
if self.__sanitize:
open_, high, low, close = common.sanitize_ohlc(open_, high, low, close)
return bar.BasicBar(dateTime, open_, high, low, close, volume, adjClose, self.__frequency)
class Feed(sqlitefeed.BarFeed):
"""A :class:`pyalgotrade.barfeed.sqlitefeed.BarFeed` that loads bars from CSV files downloaded from Yahoo! Finance.
:param frequency: The frequency of the bars. Only **pyalgotrade.bar.Frequency.DAY** or **pyalgotrade.bar.Frequency.WEEK**
are supported.
:param timezone: The default timezone to use to localize bars. Check :mod:`pyalgotrade.marketsession`.
:type timezone: A pytz timezone.
:param maxLen: The maximum number of values that the :class:`pyalgotrade.dataseries.bards.BarDataSeries` will hold.
Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.
:type maxLen: int.
.. note::
Yahoo! Finance csv files lack timezone information.
When working with multiple instruments:
* If all the instruments loaded are in the same timezone, then the timezone parameter may not be specified.
* If any of the instruments loaded are in different timezones, then the timezone parameter must be set.
"""
def __init__(self, frequency=bar.Frequency.DAY, timezone=None, maxLen=dataseries.DEFAULT_MAX_LEN):
if isinstance(timezone, int):
raise Exception("timezone as an int parameter is not supported anymore. Please use a pytz timezone instead.")
if frequency not in [bar.Frequency.DAY, bar.Frequency.WEEK]:
raise Exception("Invalid frequency.")
sqlitefeed.BarFeed.__init__(self, frequency, maxLen)
self.__timezone = timezone
self.__sanitizeBars = False
def sanitizeBars(self, sanitize):
self.__sanitizeBars = sanitize
def barsHaveAdjClose(self):
return True
def addBarsFromSQL(self, instrument, path, timezone=None):
"""Loads bars for a given instrument from a CSV formatted file.
The instrument gets registered in the bar feed.
:param instrument: Instrument identifier.
:type instrument: string.
:param path: The path to the CSV file.
:type path: string.
:param timezone: The timezone to use to localize bars. Check :mod:`pyalgotrade.marketsession`.
:type timezone: A pytz timezone.
"""
if isinstance(timezone, int):
raise Exception("timezone as an int parameter is not supported anymore. Please use a pytz timezone instead.")
if timezone is None:
timezone = self.__timezone
rowParser = RowParser(self.getDailyBarTime(), self.getFrequency(), timezone, self.__sanitizeBars)
sqlitefeed.BarFeed.addBarsFromSQL(self, instrument, path, rowParser)