This repository has been archived by the owner on Dec 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
livefeed.py
executable file
·293 lines (231 loc) · 7.8 KB
/
livefeed.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
# -*- coding: utf-8 -*-
import Queue
import datetime
import threading
import time
from pyalgotrade import bar
from pyalgotrade import barfeed
from pyalgotrade import dataseries
from pyalgotrade import logger
from pyalgotrade import observer
from pyalgotrade.utils import dt
import api
logger = logger.getLogger("mercadobitcoin")
def utcnow():
return dt.as_utc(datetime.datetime.utcnow())
class TradeBar(bar.Bar):
__slots__ = ('__dateTime', '__tradeId', '__price', '__amount', '__type')
last_datetime = None
def __init__(self, bardict):
trade_dt = datetime.datetime.fromtimestamp(bardict['date'])
if TradeBar.last_datetime is not None:
if trade_dt <= TradeBar.last_datetime:
trade_dt = (
TradeBar.last_datetime +
datetime.timedelta(seconds=0.01)
)
TradeBar.last_datetime = trade_dt
self.__dateTime = trade_dt
self.__tradeId = bardict['tid']
self.__price = float(bardict['price'])
self.__amount = float(bardict['amount'])
self.__type = bardict['type']
def __setstate__(self, state):
(
self.__dateTime,
self.__tradeId,
self.__price,
self.__amount,
self.__type
) = state
def __getstate__(self):
return (
self.__dateTime,
self.__tradeId,
self.__price,
self.__amount,
self.__type
)
def setUseAdjustedValue(self, useAdjusted):
if useAdjusted:
raise Exception("Adjusted close is not available")
def getTradeId(self):
return self.__tradeId
def getType(self):
return self.__type
def getFrequency(self):
return bar.Frequency.TRADE
def getDateTime(self):
return self.__dateTime
def getOpen(self, adjusted=False):
return self.__price
def getHigh(self, adjusted=False):
return self.__price
def getLow(self, adjusted=False):
return self.__price
def getClose(self, adjusted=False):
return self.__price
def getVolume(self):
return self.__amount
def getAdjClose(self):
return None
def getTypicalPrice(self):
return self.__price
def getPrice(self):
return self.__price
def getUseAdjValue(self):
return False
class PollingThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.__stopped = False
def __wait(self):
nextCall = self.getNextCallDateTime()
while not self.__stopped and utcnow() < nextCall:
time.sleep(0.5)
def stop(self):
self.__stopped = True
def stopped(self):
return self.__stopped
def run(self):
logger.info("Thread started")
while not self.__stopped:
self.__wait()
if not self.__stopped:
try:
self.doCall()
except Exception, e:
logger.critical("Unhandled exception", exc_info=e)
logger.debug("Thread finished.")
# Must return a non-naive datetime.
def getNextCallDateTime(self):
raise NotImplementedError()
def doCall(self):
raise NotImplementedError()
class TradesAPIThread(PollingThread):
# Events
ON_TRADE = 1
ON_ORDER_BOOK_UPDATE = 2
def __init__(self, queue, identifiers, apiCallDelay):
PollingThread.__init__(self)
self.__queue = queue
self.__identifiers = identifiers
self.__frequency = bar.Frequency.TRADE
self.__apiCallDelay = apiCallDelay
self.last_tid = 0
self.last_orderbook_ts = 0
def getNextCallDateTime(self):
return utcnow() + self.__apiCallDelay
def doCall(self):
for identifier in self.__identifiers:
try:
trades = api.get_trades() #identifier
trades.reverse()
for barDict in trades:
bar = {}
trade = TradeBar(barDict)
bar[identifier] = trade
tid = trade.getTradeId()
if tid > self.last_tid:
self.last_tid = tid
self.__queue.put((
TradesAPIThread.ON_TRADE, bar
))
orders = api.get_orderbook(identifier)
if len(orders['bids']) and len(orders['asks']):
best_ask = orders['asks'][0]
best_bid = orders['bids'][0]
#last_update = self.last_orderbook_ts + 1;
# max(
# best_ask['timestamp'], best_bid['timestamp']
#)
# if last_update > self.last_orderbook_ts:
# self.last_orderbook_ts = last_update
self.__queue.put((
TradesAPIThread.ON_ORDER_BOOK_UPDATE,
{
'bid': float(best_bid[0]),
'ask': float(best_ask[0])
}
))
except api.MercadobitcoinError, e:
logger.error(e)
class LiveFeed(barfeed.BaseBarFeed):
QUEUE_TIMEOUT = 0.01
def __init__(
self,
identifiers,
apiCallDelay=5,
maxLen=dataseries.DEFAULT_MAX_LEN
):
logger.info('Livefeed created')
barfeed.BaseBarFeed.__init__(self, bar.Frequency.TRADE, maxLen)
if not isinstance(identifiers, list):
raise Exception("identifiers must be a list")
self.__queue = Queue.Queue()
self.__orderBookUpdateEvent = observer.Event()
self.__thread = TradesAPIThread(
self.__queue,
identifiers,
datetime.timedelta(seconds=apiCallDelay)
)
self.__bars = []
for instrument in identifiers:
self.registerInstrument(instrument)
# observer.Subject interface
def start(self):
if self.__thread.is_alive():
raise Exception("Already strated")
self.__thread.start()
def stop(self):
self.__thread.stop()
def join(self):
if self.__thread.is_alive():
self.__thread.join()
def eof(self):
return self.__thread.stopped()
def peekDateTime(self):
return None
# barfeed.BaseBarFeed interface
def getCurrentDateTime(self):
return utcnow()
def barsHaveAdjClose(self):
return False
def dispatch(self):
ret = False
if self.__dispatchImpl(None):
ret = True
if barfeed.BaseBarFeed.dispatch(self):
ret = True
return ret
def __dispatchImpl(self, eventFilter):
ret = False
try:
eventType, eventData = self.__queue.get(
True, LiveFeed.QUEUE_TIMEOUT
)
if eventFilter is not None and eventType not in eventFilter:
return False
ret = True
if eventType == TradesAPIThread.ON_TRADE:
self.__onTrade(eventData)
elif eventType == TradesAPIThread.ON_ORDER_BOOK_UPDATE:
self.__orderBookUpdateEvent.emit(eventData)
else:
ret = False
logger.error(
"Invalid event received to dispatch: %s - %s" % (
eventType, eventData
)
)
except Queue.Empty:
pass
return ret
def __onTrade(self, barData):
self.__bars.append(barData)
def getNextBars(self):
if len(self.__bars):
return bar.Bars(self.__bars.pop(0))
return None
def getOrderBookUpdateEvent(self):
return self.__orderBookUpdateEvent