forked from francinexue/xuefu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataFrameBarfeed.py
373 lines (296 loc) · 11.9 KB
/
dataFrameBarfeed.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# -*- coding: utf-8 -*-
#
# 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>
"""
import datetime
import pytz
from pyalgotrade import barfeed, utils
from pyalgotrade import dataseries
from pyalgotrade.utils import dt
import bar
import tickfeed
# Interface for csv row parsers.
class RowParser(object):
def parseBar(self, csvRowDict):
raise NotImplementedError()
def getFieldNames(self):
raise NotImplementedError()
def getDelimiter(self):
raise NotImplementedError()
# Interface for bar filters.
class BarFilter(object):
def includeBar(self, bar_):
raise NotImplementedError()
class DateRangeFilter(BarFilter):
def __init__(self, fromDate=None, toDate=None):
self.__fromDate = fromDate
self.__toDate = toDate
def includeBar(self, bar_):
if self.__toDate and bar_.getDateTime() > self.__toDate:
return False
if self.__fromDate and bar_.getDateTime() < self.__fromDate:
return False
return True
# US Equities Regular Trading Hours filter
# Monday ~ Friday
# 9:30 ~ 16 (GMT-5)
class USEquitiesRTH(DateRangeFilter):
timezone = pytz.timezone("US/Eastern")
def __init__(self, fromDate=None, toDate=None):
DateRangeFilter.__init__(self, fromDate, toDate)
self.__fromTime = datetime.time(9, 30, 0)
self.__toTime = datetime.time(16, 0, 0)
def includeBar(self, bar_):
ret = DateRangeFilter.includeBar(self, bar_)
if ret:
# Check day of week
barDay = bar_.getDateTime().weekday()
if barDay > 4:
return False
# Check time
barTime = dt.localize(bar_.getDateTime(), USEquitiesRTH.timezone).time()
if barTime < self.__fromTime:
return False
if barTime > self.__toTime:
return False
return ret
class BarFeed(barfeed.BaseBarFeed):
"""Base class for CSV file based :class:`pyalgotrade.barfeed.BarFeed`.
.. note::
This is a base class and should not be used directly.
"""
def __init__(self, frequency, maxLen=dataseries.DEFAULT_MAX_LEN):
barfeed.BaseBarFeed.__init__(self, frequency, maxLen)
self.__bars = {}
self.__nextPos = {}
self.__started = False
self.__currDateTime = None
self.__barFilter = None
self.__dailyTime = datetime.time(0, 0, 0)
def reset(self):
self.__nextPos = {}
for instrument in self.__bars.keys():
self.__nextPos.setdefault(instrument, 0)
self.__currDateTime = None
super(BarFeed, self).reset()
def getCurrentDateTime(self):
return self.__currDateTime
def start(self):
super(BarFeed, self).start()
self.__started = True
def stop(self):
pass
def join(self):
pass
def addBarsFromSequence(self, instrument, bars):
if self.__started:
raise Exception("Can't add more bars once you started consuming bars")
self.__bars.setdefault(instrument, [])
self.__nextPos.setdefault(instrument, 0)
# Add and sort the bars
self.__bars[instrument].extend(bars)
barCmp = lambda x, y: cmp(x.getDateTime(), y.getDateTime())
self.__bars[instrument].sort(barCmp)
self.registerInstrument(instrument)
def eof(self):
ret = True
# print 'called eof'
# Check if there is at least one more bar to return.
for instrument, bars in self.__bars.iteritems():
nextPos = self.__nextPos[instrument]
if nextPos < len(bars):
ret = False
break
return ret
def peekDateTime(self):
ret = None
for instrument, bars in self.__bars.iteritems():
nextPos = self.__nextPos[instrument]
if nextPos < len(bars):
ret = utils.safe_min(ret, bars[nextPos].getDateTime())
return ret
def getNextBars(self):
# All bars must have the same datetime. We will return all the ones with the smallest datetime.
smallestDateTime = self.peekDateTime()
if smallestDateTime is None:
return None
# Make a second pass to get all the bars that had the smallest datetime.
ret = {}
for instrument, bars in self.__bars.iteritems():
nextPos = self.__nextPos[instrument]
if nextPos < len(bars) and bars[nextPos].getDateTime() == smallestDateTime:
ret[instrument] = bars[nextPos]
self.__nextPos[instrument] += 1
if self.__currDateTime == smallestDateTime:
raise Exception("Duplicate bars found for %s on %s" % (ret.keys(), smallestDateTime))
self.__currDateTime = smallestDateTime
return bar.Bars(ret)
def loadAll(self):
for dateTime, bars in self:
pass
def getDailyBarTime(self):
return self.__dailyTime
def setDailyBarTime(self, time):
self.__dailyTime = time
def getBarFilter(self):
return self.__barFilter
def setBarFilter(self, barFilter):
self.__barFilter = barFilter
# 使用apply+handler最提高效率,但是层层调用显得麻烦
def addBarsFromDataFrame(self, instrument, rowParser, df):
# Load the csv file
loadedBars = []
for row in df.iterrows():
bar_ = rowParser.parseBar(row)
if bar_ is not None and (self.__barFilter is None or self.__barFilter.includeBar(bar_)):
loadedBars.append(bar_)
self.addBarsFromSequence(instrument, loadedBars)
class TickFeed(tickfeed.BaseBarFeed):
"""Base class for CSV file based :class:`pyalgotrade.barfeed.BarFeed`.
.. note::
This is a base class and should not be used directly.
"""
def __init__(self, frequency, maxLen=dataseries.DEFAULT_MAX_LEN):
tickfeed.BaseBarFeed.__init__(self, frequency, maxLen)
self.__bars = {}
self.__nextPos = {}
self.__started = False
self.__currDateTime = None
self.__barFilter = None
self.__dailyTime = datetime.time(0, 0, 0)
def reset(self):
self.__nextPos = {}
for instrument in self.__bars.keys():
self.__nextPos.setdefault(instrument, 0)
self.__currDateTime = None
super(TickFeed, self).reset()
def getCurrentDateTime(self):
return self.__currDateTime
def start(self):
super(TickFeed, self).start()
self.__started = True
def stop(self):
pass
def join(self):
pass
def addBarsFromSequence(self, instrument, bars):
if self.__started:
raise Exception("Can't add more bars once you started consuming bars")
self.__bars.setdefault(instrument, [])
self.__nextPos.setdefault(instrument, 0)
# Add and sort the bars
self.__bars[instrument].extend(bars)
barCmp = lambda x, y: cmp(x.getDateTime(), y.getDateTime())
self.__bars[instrument].sort(barCmp)
self.registerInstrument(instrument)
def eof(self):
ret = True
# print 'called eof'
# Check if there is at least one more bar to return.
for instrument, bars in self.__bars.iteritems():
nextPos = self.__nextPos[instrument]
if nextPos < len(bars):
ret = False
break
return ret
def peekDateTime(self):
ret = None
for instrument, bars in self.__bars.iteritems():
nextPos = self.__nextPos[instrument]
if nextPos < len(bars):
ret = utils.safe_min(ret, bars[nextPos].getDateTime())
return ret
def getNextBars(self):
# All bars must have the same datetime. We will return all the ones with the smallest datetime.
smallestDateTime = self.peekDateTime()
if smallestDateTime is None:
return None
# Make a second pass to get all the bars that had the smallest datetime.
ret = {}
for instrument, bars in self.__bars.iteritems():
nextPos = self.__nextPos[instrument]
if nextPos < len(bars) and bars[nextPos].getDateTime() == smallestDateTime:
ret[instrument] = bars[nextPos]
self.__nextPos[instrument] += 1
if self.__currDateTime == smallestDateTime:
raise Exception("Duplicate bars found for %s on %s" % (ret.keys(), smallestDateTime))
self.__currDateTime = smallestDateTime
return bar.Bars(ret)
def loadAll(self):
for dateTime, bars in self:
pass
def getDailyBarTime(self):
return self.__dailyTime
def setDailyBarTime(self, time):
self.__dailyTime = time
def getBarFilter(self):
return self.__barFilter
def setBarFilter(self, barFilter):
self.__barFilter = barFilter
# 使用apply+handler最提高效率,但是层层调用显得麻烦
def addBarsFromDataFrame(self, instrument, rowParser,df):
# Load the csv file
loadedBars = []
for id,row in df.iterrows():
bar_ = rowParser.parseTickBar(id,row)
if bar_ is not None and (self.__barFilter is None or self.__barFilter.includeBar(bar_)):
loadedBars.append(bar_)
self.addBarsFromSequence(instrument, loadedBars)
class GenericRowParser(RowParser):
def __init__(self, columnNames, dateTimeFormat, dailyBarTime, frequency, timezone):
self.__dateTimeFormat = dateTimeFormat
self.__dailyBarTime = dailyBarTime
self.__frequency = frequency
self.__timezone = timezone
self.__haveAdjClose = False
# Column names.
self.__dateTimeColName = columnNames["datetime"]
self.__openColName = columnNames["open"]
self.__highColName = columnNames["high"]
self.__lowColName = columnNames["low"]
self.__closeColName = columnNames["close"]
self.__volumeColName = columnNames["volume"]
self.__adjCloseColName = columnNames["adj_close"]
def _parseDate(self, dateString):
ret = datetime.datetime.strptime(dateString, self.__dateTimeFormat)
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 barsHaveAdjClose(self):
return self.__haveAdjClose
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[self.__dateTimeColName])
open_ = float(csvRowDict[self.__openColName])
high = float(csvRowDict[self.__highColName])
low = float(csvRowDict[self.__lowColName])
close = float(csvRowDict[self.__closeColName])
volume = float(csvRowDict[self.__volumeColName])
adjClose = None
if self.__adjCloseColName is not None:
adjCloseValue = csvRowDict.get(self.__adjCloseColName, "")
if len(adjCloseValue) > 0:
adjClose = float(adjCloseValue)
self.__haveAdjClose = True
return bar.BasicBar(dateTime, open_, high, low, close, volume, adjClose, self.__frequency)