-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·506 lines (437 loc) · 22.2 KB
/
client.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/usr/local/bin/jython
__version__ = ".00"
__author__ = "gazzman"
__copyright__ = "(C) 2013 gazzman GNU GPL 3."
__contributors__ = []
from datetime import datetime, timedelta
from time import sleep
from logging.handlers import TimedRotatingFileHandler
import java.io.EOFException
import logging
import pickle
import sys
from com.ib.client import (EWrapper, EWrapperMsgGenerator, EClientSocket,
Contract, ExecutionFilter, TickType)
from ib.contractkeys import (ContractId, Currency, CurrencyLocal, Index,
Option, OptionLocal, Stock)
LOGLEVEL = logging.INFO
CONKEYTYPES = [str(x) for x in (ContractId, Currency, CurrencyLocal, Option,
OptionLocal, Stock)]
class CallbackBase():
realtime_bars = dict()
historical_data = dict()
mkt_data = dict()
fundamentals = dict()
satisfied_requests = dict()
req_errs = dict()
orders = dict()
req_cds = dict()
fulfilled_contracts = dict()
fulfilled_open_orders_req = dict()
fulfilled_execution_req = dict()
fulfilled_tick_snapshots = dict()
failed_contracts = dict()
data_requests = dict()
executions = dict()
mkt_data_requests = dict()
data_req_fnames = dict()
# Callback data handlers
def error(self, *args):
''' We either get an (int, int, str), (Exception, ), or (str, )
'''
errmsg = EWrapperMsgGenerator.error(*args)
errmsg = ' '.join(errmsg.split('\n'))
if len(args) == 3:
(req_id, err_code, err_msg) = args
if err_code == 200:
self.failed_contracts[req_id] = (err_code, err_msg)
elif req_id >= 0: self.req_errs[req_id] = (err_code, err_msg)
if err_code == 162: self.logger.warn(errmsg) # historical data
elif err_code == 200: self.logger.warn(errmsg) # no contract
elif err_code == 202: self.logger.info(errmsg) # cancel order
elif err_code == 399: self.logger.warn(errmsg) # after-hours
elif err_code == 502:
self.logger.warn(errmsg) # no connection
raise Exception('%s | %s | %s' % (req_id, err_code, err_msg))
elif err_code < 1100: self.logger.error(errmsg)
elif err_code < 2100: self.logger.critical(errmsg)
else: self.logger.warn(errmsg)
elif type(args[0]) is (Exception or java.io.EOFException):
self.logger.error(args[0])
raise args[0]
elif type(args[0]) is str: self.logger.error(errmsg)
else:
m = 'Unexpected result from AnyWrapperMsgGenerator: %s, %s'
self.logger.error(m, str(args[0]), str(type(args[0])))
def msghandler(self, msg, req_id=None, order_id=None):
if req_id is not None:
self.satisfied_requests[req_id] = datetime.now()
self.logger.info(msg)
def datahandler(self, req_id, datamsg, finish_indicator=None):
self.logger.debug('Received datapoint for req_id %i' % req_id)
fnm = self.data_req_fnames[req_id]
f = open(fnm, 'a')
f.write('%s %s\n' % (datetime.now().isoformat(), datamsg))
f.close()
self.logger.debug('Wrote datapoint for req_id %i to %s', req_id, fnm)
if finish_indicator:
if 'finished' in finish_indicator:
self.satisfied_requests[req_id] = datetime.now()
self.logger.info('Historical data request %s stored in %s',
req_id, fnm)
def currentTime(self, time):
msg = EWrapperMsgGenerator.currentTime(time)
self.msghandler(msg)
# Order callbacks
def nextValidId(self, orderId):
self.nextId = orderId
msg = EWrapperMsgGenerator.nextValidId(orderId)
self.logger.info('nextValidID: ' + msg)
def orderStatus(self, orderId, status, filled, remaining, avgFillPrice,
permId, parentId, lastFillPrice, clientId, whyHeld):
if clientId not in self.orders: self.orders[clientId] = dict()
self.orders[clientId][orderId] = {'status': status, 'filled': filled,
'remaining': remaining,
'avgFillPrice': avgFillPrice,
'permId': permId,
'parentId': parentId,
'lastFillPrice': lastFillPrice,
'clientId': clientId,
'whyHeld': whyHeld}
# msg = EWrapperMsgGenerator.orderStatus(orderId, status, filled,
# remaining, avgFillPrice, permId,
# parentId, lastFillPrice,
# clientId, whyHeld)
msg = 'submitted by: %2i, oid: %2i, status: %s'
self.msghandler(msg % (clientId, orderId, status))
def openOrder(self, orderId, contract, order, orderState):
# msg = EWrapperMsgGenerator.openOrder(orderId, contract, order,
# orderState)
msg = 'submitted by: %2i, oid: %2i, action: %s, type: %s, symbol: %s'
msg = ', '.join([msg, 'secType: %s, qty: %3i'])
msg_data = (order.m_clientId, orderId, order.m_action,
order.m_orderType, contract.m_symbol, contract.m_secType,
order.m_totalQuantity)
self.msghandler(msg % msg_data)
def openOrderEnd(self):
msg = EWrapperMsgGenerator.openOrderEnd()
self.msghandler(msg)
# Account callbacks
def updateAccountValue(self, key, value, currency, accountName):
msg = EWrapperMsgGenerator.updateAccountValue(key, value, currency,
accountName)
self.msghandler('updateAccVal: ' + msg)
def updatePortfolio(self, contract, position, marketPrice, marketValue,
averageCost, unrealizedPNL, realizedPNL, accountName):
msg = EWrapperMsgGenerator.updatePortfolio(contract, position,
marketPrice, marketValue,
averageCost, unrealizedPNL,
realizedPNL, accountName)
self.msghandler('updatePort: ' + msg)
def updateAccountTime(self, timeStamp):
msg = EWrapperMsgGenerator.updateAccountTime(timeStamp)
self.msghandler('updateAccTime: ' + msg)
def accountDownloadEnd(self, accountName):
msg = EWrapperMsgGenerator.accountDownloadEnd(accountName)
self.msghandler('acctDLEnd: ' + msg)
def managedAccounts(self, accountsList):
msg = EWrapperMsgGenerator.managedAccounts(accountsList)
self.logger.info(msg)
def receiveFA(self, faDataType, xml):
msg = EWrapperMsgGenerator.receiveFA(faDataType, xml)
self.msghandler('receiveFA: ' + msg)
# Contract callbacks
def contractDetails(self, reqId, contractDetails):
if reqId not in self.req_cds: self.req_cds[reqId] = list()
self.req_cds[reqId].append(contractDetails)
# msg = EWrapperMsgGenerator.contractDetails(reqId, contractDetails)
msg = 'reqId = %i received details for'
msg = ' '.join([msg, 'con_id: %i symbol: %s, secType: %s'])
msg_data = (reqId, contractDetails.m_summary.m_conId,
contractDetails.m_summary.m_symbol,
contractDetails.m_summary.m_secType)
self.msghandler(msg % msg_data, req_id=reqId)
def contractMsg(self, contract):
msg = EWrapperMsgGenerator.contractMsg(contract)
self.msghandler('contractMsg: ' + msg)
def bondContractDetails(self, reqId, contractDetails):
msg = EWrapperMsgGenerator.bondContractDetails(reqId, contractDetails)
self.msghandler('bondConDet: ' + msg, req_id=reqId)
def contractDetailsEnd(self, reqId):
msg = EWrapperMsgGenerator.contractDetailsEnd(reqId)
self.fulfilled_contracts[reqId] = datetime.now()
self.msghandler(msg)
# Execution callbacks
def execDetails(self, reqId, contract, execution):
msg = EWrapperMsgGenerator.execDetails(reqId, contract, execution)
self.executions[execution.m_execId] = (reqId, contract, execution)
if execution.m_orderId == sys.maxint: oid = 0
else: oid = execution.m_orderId
msg = 'Client %2i executed oid %4i: %s %3i %5s %s at price %7.3f,'
msg += ' avgprice %7.3f'
msg_data = (execution.m_clientId, oid, execution.m_side,
execution.m_cumQty, contract.m_symbol, contract.m_secType,
execution.m_price, execution.m_avgPrice)
self.msghandler(msg % msg_data, req_id=reqId)
def execDetailsEnd(self, reqId):
msg = EWrapperMsgGenerator.execDetailsEnd(reqId)
self.fulfilled_execution_req[reqId] = datetime.now()
self.msghandler(msg, req_id=reqId)
# Data callbacks
def updateMktDepth(self, tickerId, position, operation, side, price, size):
msg = EWrapperMsgGenerator.updateMktDepth(tickerId, position,
operation, side, price, size)
self.msghandler('updateMktDepth: ' + msg)
def updateMktDepthL2(self, tickerId, position, marketMaker, operation,
side, price, size):
msg = EWrapperMsgGenerator.updateMktDepthL2(tickerId, position,
marketMaker, operation,
side, price, size)
self.msghandler('updateMktDepthL2: ' + msg)
def updateNewsBulletin(self, msgId, msgType, message, origExchange):
msg = EWrapperMsgGenerator.updateNewsBulletin(msgId, msgType, message,
origExchange)
self.msghandler('updateNewsBull: ' + msg)
def historicalData(self, reqId, date, open_, high, low, close, volume,
count, WAP, hasGaps):
msg = EWrapperMsgGenerator.historicalData(reqId, date, open_, high,
low, close, volume, count,
WAP, hasGaps)
self.datahandler(reqId, msg, date)
def realtimeBar(self, reqId, time, open_, high, low, close, volume, wap,
count):
msg = EWrapperMsgGenerator.realtimeBar(reqId, time, open_, high, low,
close, volume, wap, count)
self.datahandler(reqId, msg)
def fundamentalData(self, reqId, data):
msg = EWrapperMsgGenerator.fundamentalData(reqId, data)
self.datahandler(reqId, msg)
# Scanner callbacks
def scannerParameters(self, xml):
msg = EWrapperMsgGenerator.scannerParameters(xml)
self.msghandler('scannerParams: ' + msg)
def scannerData(self, reqId, rank, contractDetails, distance, benchmark,
projection, legsStr):
msg = EWrapperMsgGenerator.scannerData(reqId, rank, contractDetails,
distance, benchmark,
projection, legsStr)
self.msghandler('scannerData: ' + msg, req_id=reqId)
def scannerDataEnd(self, reqId):
msg = EWrapperMsgGenerator.scannerDataEnd(reqId)
self.msghandler('scannerDataEnd: ' + msg, req_id=reqId)
# Market data Tick callbacks
def tickPrice(self, tickerId, field, price, canAutoExecute):
msg = EWrapperMsgGenerator.tickPrice(tickerId, field, price,
canAutoExecute)
self.datahandler(tickerId, msg)
def tickSize(self, tickerId, field, size):
msg = EWrapperMsgGenerator.tickSize(tickerId, field, size)
self.datahandler(tickerId, msg)
def tickOptionComputation(self, tickerId, field, impliedVol, delta,
optPrice, pvDividend, gamma, vega, theta,
undPrice):
msg = EWrapperMsgGenerator.tickOptionComputation(tickerId, field,
impliedVol, delta,
optPrice, pvDividend,
gamma, vega, theta,
undPrice)
self.datahandler(tickerId, msg)
def tickGeneric(self, tickerId, tickType, value):
msg = EWrapperMsgGenerator.tickGeneric(tickerId, tickType, value)
self.datahandler(tickerId, msg)
def tickString(self, tickerId, tickType, value):
msg = EWrapperMsgGenerator.tickString(tickerId, tickType, value)
self.datahandler(tickerId, msg)
def tickEFP(self, tickerId, tickType, basisPoints, formattedBasisPoints,
impliedFuture, holdDays, futureExpiry, dividendImpact,
dividendsToExpiry):
msg = EWrapperMsgGenerator.tickEFP(tickerId, tickType, basisPoints,
formattedBasisPoints, impliedFuture,
holdDays, futureExpiry,
dividendImpact, dividendsToExpiry)
self.datahandler(tickerId, msg)
def deltaNeutralValidation(self, reqId, underComp):
msg = EWrapperMsgGenerator.deltaNeutralValidation(reqId, underComp)
self.msghandler('deltaNeutralValid: ' + msg, req_id=reqId)
def tickSnapshotEnd(self, tickerId):
msg = EWrapperMsgGenerator.tickSnapshotEnd(tickerId)
self.fulfilled_tick_snapshots[tickerId] = datetime.now()
del self.mkt_data[tickerId]
self.msghandler(msg)
def marketDataType(self, reqId, marketDataType):
msg = EWrapperMsgGenerator.marketDataType(reqId, marketDataType)
self.msghandler('marketDataType: ' + msg, req_id=reqId)
class Client(CallbackBase, EWrapper):
cached_cds = dict()
id_to_cd = dict()
stk_base = {'m_secType': 'STK', 'm_exchange': 'SMART', 'm_currency': 'USD'}
opt_base = {'m_secType': 'OPT', 'm_exchange': 'SMART', 'm_currency': 'USD'}
ind_base = {'m_secType': 'IND', 'm_currency': 'USD'}
fx_base = {'m_secType': 'CASH'}
def __init__(self, client_id=9):
self.client_id = client_id
self.init_logger()
self.req_id = 0
self.m_client = EClientSocket(self)
self.tt = TickType()
def init_logger(self):
cid = '%2i' % self.client_id
logger_fmt = ' '.join(['%(levelno)s, [%(asctime)s #%(process)5i]',
'client', cid, '%(levelname)8s: %(message)s'])
self.logger = logging.getLogger(__name__)
hdlr = TimedRotatingFileHandler('ib_client.log', when='midnight')
fmt = logging.Formatter(fmt=logger_fmt)
hdlr.setFormatter(fmt)
self.logger.addHandler(hdlr)
self.logger.setLevel(LOGLEVEL)
def connect(self, host='', port=7496):
self.m_client.eConnect(host, port, self.client_id)
def disconnect(self):
self.m_client.eDisconnect()
self.logger.info('Client disconnected')
id_to_localSymbol = dict()
for cid in self.id_to_cd:
id_to_localSymbol[cid] = self.id_to_cd[cid].m_summary.m_localSymbol
fnm = 'id_to_localSymbol_%s.pkl' % datetime.now().isoformat()
pickle.dump(id_to_localSymbol, open(fnm, 'w'))
def request_contract_details(self, key):
if key not in self.cached_cds:
args = key._asdict()
if type(key) == ContractId:
if key[0] in self.id_to_cd: return [self.id_to_cd[key[0]]]
elif type(key) in (Currency, CurrencyLocal):
args.update(self.fx_base)
elif type(key) == Index:
args.update(self.ind_base)
elif type(key) in (Option, OptionLocal):
args.update(self.opt_base)
elif type(key) == Stock:
args.update(self.stk_base)
else:
errmsg = 'Valid arg types are %s; not %s'
raise TypeError(errmsg % (', '.join(CONKEYTYPES), str(type(key))))
args = dict([(k, v) for (k, v) in args.items() if v])
contract = Contract(**args)
self.req_id += 1
self.m_client.reqContractDetails(self.req_id, contract)
while self.req_id not in (self.failed_contracts.keys() +
self.fulfilled_contracts.keys()): sleep(.5)
if self.req_id in self.failed_contracts: return None
self.cached_cds[key] = self.req_cds[self.req_id]
for cd in self.req_cds[self.req_id]:
self.id_to_cd[cd.m_summary.m_conId] = cd
return self.cached_cds[key]
# Request data methods
def request_mkt_data(self, contract, gtick_list='', snapshot=True,
fname=None):
self.req_id += 1
self.data_requests[self.req_id] = datetime.now()
if not fname: fname = 'MKT_%i_%s.txt' % (contract.m_conId, gtick_list)
self.data_req_fnames[self.req_id] = fname
self.mkt_data[self.req_id] = (contract.m_conId, gtick_list)
self.m_client.reqMktData(self.req_id, contract, gtick_list, snapshot)
return self.req_id
def start_realtime_bars(self, contract, show='TRADES', fname=None):
if self.too_many_requests():
self.logger.error('Too many requests.')
return None
self.req_id += 1
self.data_requests[self.req_id] = datetime.now()
self.realtime_bars[self.req_id] = (contract.m_conId, show)
if not fname: fname = 'RTBARS_%i_%s.txt' % (contract.m_conId, show)
self.data_req_fnames[self.req_id] = fname
self.m_client.reqRealTimeBars(self.req_id, contract, 5, show, 0)
self.logger.info('Realtime bars started for req_id %i', self.req_id)
return self.req_id
def request_historical_data(self, contract, end_time=None, duration='1 D',
bar_size='1 min', show='TRADES', fname=None):
if self.too_many_requests(): return None
if not end_time: end_time = datetime.now().strftime('%Y%m%d %H:%M:%S')
self.req_id += 1
self.data_requests[self.req_id] = datetime.now()
self.historical_data[self.req_id] = (contract.m_conId, show)
if not fname: fname = 'HSTBARS_%i_%s.txt' % (contract.m_conId, show)
self.data_req_fnames[self.req_id] = fname
self.m_client.reqHistoricalData(self.req_id, contract, end_time,
duration, bar_size, show, 1, 1)
return self.req_id
def request_fundamentals(self, contract, report_type, fname=None):
if self.too_many_requests(): return None
self.req_id += 1
self.data_requests[self.req_id] = datetime.now()
self.fundamentals[self.req_id] = (contract.m_conId, report_type)
if not fname: fname = 'FND_%i_%s.txt' % (contract.m_conId, report_type)
self.data_req_fnames[req_id] = fname
self.m_client.reqFundamentalData(self.req_id, contract, report_type)
return self.req_id
# Cancel data methods
def cancel_mkt_data(self, req_id):
self.m_client.cancelMktData(req_id)
del self.mkt_data[req_id]
self.logger.info('Market data canceled for req_id %i', req_id)
return True
def cancel_realtime_bars(self, req_id):
self.m_client.cancelRealTimeBars(req_id)
del self.realtime_bars[req_id]
self.logger.info('Realtime bars canceled for req_id %i', req_id)
return True
def cancel_historical_data(self, req_id):
self.m_client.cancelHistoricalData(req_id)
del self.historical_data[req_id]
self.logger.info('Historical data canceled for req_id %i', req_id)
return True
def cancel_fundamentals(self, req_id):
self.m_client.cancelFundamentalData(req_id)
del self.fundamentals[req_id]
self.logger.info('Fundamentals canceled for req_id %i', req_id)
return True
# Cancel all data methods
def cancel_all_mkt_data(self):
bar_ids = self.mkt_data.keys()
[self.cancel_mkt_data(x) for x in bar_ids]
def cancel_all_realtime_bars(self):
bar_ids = self.realtime_bars.keys()
[self.cancel_realtime_bars(x) for x in bar_ids]
def cancel_all_historical_data(self):
historical_ids = self.historical_data.keys()
[self.cancel_historical_data(x) for x in historical_ids]
def cancel_all_fundamentals(self):
fundamental_ids = self.fundamentals.keys()
[self.cancel_fundamentals(x) for x in fundamental_ids]
# Orders and Executions methods
def place_order(self, contract, order):
order_id = self.nextId
self.m_client.placeOrder(self.nextId, contract, order)
self.m_client.reqIds(1)
while order_id == self.nextId:
self.logger.debug('Waiting for next order id')
sleep(.1)
return order_id
def request_open_orders(self):
self.m_client.reqOpenOrders()
def request_all_orders(self):
self.m_client.reqAllOpenOrders()
def cancel_order(self, order_id):
self.m_client.cancelOrder(order_id)
del self.orders[self.client_id][order_id]
def cancel_open_orders(self):
self.request_open_orders()
if self.client_id not in self.orders:
self.logger.error('Client has no open orders')
else:
order_ids = self.orders[self.client_id].keys()
[self.cancel_order(x) for x in order_ids
if self.orders[self.client_id][x]['status'].lower() != 'cancelled']
def request_executions(self, client_id=None, time=None, symbol=None,
sec_type=None, side=None, exchange=None):
args = {'m_clientId': client_id, 'm_time': time, 'm_symbol': symbol,
'm_secType': sec_type, 'm_side': side, 'm_exchange': exchange}
args = dict([(k, v) for (k, v) in args.items() if v])
self.req_id += 1
self.m_client.reqExecutions(self.req_id, ExecutionFilter(**args))
# Helper methods
def too_many_requests(self):
since = datetime.now() - timedelta(seconds=600)
count =len([x for x in self.data_requests.values() if x > since])
if count >= 60: return True
else: return False