This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 710
/
client.py
959 lines (810 loc) · 31.1 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
"""Socket client for communicating with Interactive Brokers."""
import asyncio
import io
import logging
import struct
import time
from collections import deque
from typing import List, Optional
from eventkit import Event
from .connection import Connection
from .contract import Contract
from .decoder import Decoder
from .objects import ConnectionStats
from .util import UNSET_DOUBLE, UNSET_INTEGER, dataclassAsTuple, run
class Client:
"""
Replacement for ``ibapi.client.EClient`` that uses asyncio.
The client is fully asynchronous and has its own
event-driven networking code that replaces the
networking code of the standard EClient.
It also replaces the infinite loop of ``EClient.run()``
with the asyncio event loop. It can be used as a drop-in
replacement for the standard EClient as provided by IBAPI.
Compared to the standard EClient this client has the following
additional features:
* ``client.connect()`` will block until the client is ready to
serve requests; It is not necessary to wait for ``nextValidId``
to start requests as the client has already done that.
The reqId is directly available with :py:meth:`.getReqId()`.
* ``client.connectAsync()`` is a coroutine for connecting asynchronously.
* When blocking, ``client.connect()`` can be made to time out with
the timeout parameter (default 2 seconds).
* Optional ``wrapper.priceSizeTick(reqId, tickType, price, size)`` that
combines price and size instead of the two wrapper methods
priceTick and sizeTick.
* Automatic request throttling.
* Optional ``wrapper.tcpDataArrived()`` method;
If the wrapper has this method it is invoked directly after
a network packet has arrived.
A possible use is to timestamp all data in the packet with
the exact same time.
* Optional ``wrapper.tcpDataProcessed()`` method;
If the wrapper has this method it is invoked after the
network packet's data has been handled.
A possible use is to write or evaluate the newly arrived data in
one batch instead of item by item.
Attributes:
MaxRequests (int):
Throttle the number of requests to ``MaxRequests`` per
``RequestsInterval`` seconds. Set to 0 to disable throttling.
RequestsInterval (float):
Time interval (in seconds) for request throttling.
MinClientVersion (int):
Client protocol version.
MaxClientVersion (int):
Client protocol version.
Events:
* ``apiStart`` ()
* ``apiEnd`` ()
* ``apiError`` (errorMsg: str)
* ``throttleStart`` ()
* ``throttleEnd`` ()
"""
events = ('apiStart', 'apiEnd', 'apiError', 'throttleStart', 'throttleEnd')
MaxRequests = 45
RequestsInterval = 1
MinClientVersion = 142
MaxClientVersion = 163
(DISCONNECTED, CONNECTING, CONNECTED) = range(3)
def __init__(self, wrapper):
self.wrapper = wrapper
self.decoder = Decoder(wrapper, 0)
self.apiStart = Event('apiStart')
self.apiEnd = Event('apiEnd')
self.apiError = Event('apiError')
self.throttleStart = Event('throttleStart')
self.throttleEnd = Event('throttleEnd')
self._logger = logging.getLogger('ib_insync.client')
self.conn = Connection()
self.conn.hasData += self._onSocketHasData
self.conn.disconnected += self._onSocketDisconnected
# extra optional wrapper methods
self._priceSizeTick = getattr(wrapper, 'priceSizeTick', None)
self._tcpDataArrived = getattr(wrapper, 'tcpDataArrived', None)
self._tcpDataProcessed = getattr(wrapper, 'tcpDataProcessed', None)
self.host = ''
self.port = -1
self.clientId = -1
self.optCapab = ''
self.connectOptions = b''
self.reset()
def reset(self):
self.connState = Client.DISCONNECTED
self._apiReady = False
self._serverVersion = None
self._data = b''
self._hasReqId = False
self._reqIdSeq = 0
self._accounts = []
self._startTime = time.time()
self._numBytesRecv = 0
self._numMsgRecv = 0
self._isThrottling = False
self._msgQ = deque()
self._timeQ = deque()
def serverVersion(self):
return self._serverVersion
def run(self):
loop = asyncio.get_event_loop()
loop.run_forever()
def isConnected(self):
return self.connState == Client.CONNECTED
def isReady(self) -> bool:
"""Is the API connection up and running?"""
return self._apiReady
def connectionStats(self) -> ConnectionStats:
"""Get statistics about the connection."""
if not self.isReady():
raise ConnectionError('Not connected')
return ConnectionStats(
self._startTime,
time.time() - self._startTime,
self._numBytesRecv, self.conn.numBytesSent,
self._numMsgRecv, self.conn.numMsgSent)
def getReqId(self) -> int:
"""Get new request ID."""
if not self.isReady():
raise ConnectionError('Not connected')
newId = self._reqIdSeq
self._reqIdSeq += 1
return newId
def updateReqId(self, minReqId):
"""Update the next reqId to be at least ``minReqId``."""
self._reqIdSeq = max(self._reqIdSeq, minReqId)
def getAccounts(self) -> List[str]:
"""Get the list of account names that are under management."""
if not self.isReady():
raise ConnectionError('Not connected')
return self._accounts
def setConnectOptions(self, connectOptions: str):
"""
Set additional connect options.
Args:
connectOptions: Use "+PACEAPI" to use request-pacing built
into TWS/gateway 974+.
"""
self.connectOptions = connectOptions.encode()
def connect(
self, host: str, port: int, clientId: int,
timeout: Optional[float] = 2.0):
"""
Connect to a running TWS or IB gateway application.
Args:
host: Host name or IP address.
port: Port number.
clientId: ID number to use for this client; must be unique per
connection.
timeout: If establishing the connection takes longer than
``timeout`` seconds then the ``asyncio.TimeoutError`` exception
is raised. Set to 0 to disable timeout.
"""
run(self.connectAsync(host, port, clientId, timeout))
async def connectAsync(self, host, port, clientId, timeout=2.0):
try:
self._logger.info(
f'Connecting to {host}:{port} with clientId {clientId}...')
self.host = host
self.port = int(port)
self.clientId = int(clientId)
self.connState = Client.CONNECTING
timeout = timeout or None
await asyncio.wait_for(self.conn.connectAsync(host, port), timeout)
self._logger.info('Connected')
msg = b'API\0' + self._prefix(b'v%d..%d%s' % (
self.MinClientVersion, self.MaxClientVersion,
b' ' + self.connectOptions if self.connectOptions else b''))
self.conn.sendMsg(msg)
await asyncio.wait_for(self.apiStart, timeout)
self._logger.info('API connection ready')
except BaseException as e:
self.disconnect()
msg = f'API connection failed: {e!r}'
self._logger.error(msg)
self.apiError.emit(msg)
if isinstance(e, ConnectionRefusedError):
self._logger.error('Make sure API port on TWS/IBG is open')
raise
def disconnect(self):
"""Disconnect from IB connection."""
self._logger.info('Disconnecting')
self.connState = Client.DISCONNECTED
self.conn.disconnect()
self.reset()
def send(self, *fields):
"""Serialize and send the given fields using the IB socket protocol."""
if not self.isConnected():
raise ConnectionError('Not connected')
msg = io.StringIO()
for field in fields:
typ = type(field)
if field in (None, UNSET_INTEGER, UNSET_DOUBLE):
s = ''
elif typ in (str, int, float):
s = str(field)
elif typ is bool:
s = '1' if field else '0'
elif typ is list:
# list of TagValue
s = ''.join(f'{v.tag}={v.value};' for v in field)
elif isinstance(field, Contract):
c = field
s = '\0'.join(str(f) for f in (
c.conId, c.symbol, c.secType,
c.lastTradeDateOrContractMonth, c.strike,
c.right, c.multiplier, c.exchange,
c.primaryExchange, c.currency,
c.localSymbol, c.tradingClass))
else:
s = str(field)
msg.write(s)
msg.write('\0')
self.sendMsg(msg.getvalue())
def sendMsg(self, msg):
loop = asyncio.get_event_loop()
t = loop.time()
times = self._timeQ
msgs = self._msgQ
while times and t - times[0] > self.RequestsInterval:
times.popleft()
if msg:
msgs.append(msg)
while msgs and (len(times) < self.MaxRequests or not self.MaxRequests):
msg = msgs.popleft()
self.conn.sendMsg(self._prefix(msg.encode()))
times.append(t)
if self._logger.isEnabledFor(logging.DEBUG):
self._logger.debug('>>> %s', msg[:-1].replace('\0', ','))
if msgs:
if not self._isThrottling:
self._isThrottling = True
self.throttleStart.emit()
self._logger.debug('Started to throttle requests')
loop.call_at(
times[0] + self.RequestsInterval,
self.sendMsg, None)
else:
if self._isThrottling:
self._isThrottling = False
self.throttleEnd.emit()
self._logger.debug('Stopped to throttle requests')
def _prefix(self, msg):
# prefix a message with its length
return struct.pack('>I', len(msg)) + msg
def _onSocketHasData(self, data):
debug = self._logger.isEnabledFor(logging.DEBUG)
if self._tcpDataArrived:
self._tcpDataArrived()
self._data += data
self._numBytesRecv += len(data)
while True:
if len(self._data) <= 4:
break
# 4 byte prefix tells the message length
msgEnd = 4 + struct.unpack('>I', self._data[:4])[0]
if len(self._data) < msgEnd:
# insufficient data for now
break
msg = self._data[4:msgEnd].decode(errors='backslashreplace')
self._data = self._data[msgEnd:]
fields = msg.split('\0')
fields.pop() # pop off last empty element
self._numMsgRecv += 1
if debug:
self._logger.debug('<<< %s', ','.join(fields))
if not self._serverVersion and len(fields) == 2:
# this concludes the handshake
version, _connTime = fields
self._serverVersion = int(version)
if self._serverVersion < self.MinClientVersion:
self._onSocketDisconnected(
'TWS/gateway version must be >= 972')
return
self.decoder.serverVersion = self._serverVersion
self.connState = Client.CONNECTED
self.startApi()
self.wrapper.connectAck()
self._logger.info(
f'Logged on to server version {self._serverVersion}')
else:
if not self._apiReady:
# snoop for nextValidId and managedAccounts response,
# when both are in then the client is ready
msgId = int(fields[0])
if msgId == 9:
_, _, validId = fields
self.updateReqId(int(validId))
self._hasReqId = True
elif msgId == 15:
_, _, accts = fields
self._accounts = [a for a in accts.split(',') if a]
if self._hasReqId and self._accounts:
self._apiReady = True
self.apiStart.emit()
# decode and handle the message
self.decoder.interpret(fields)
if self._tcpDataProcessed:
self._tcpDataProcessed()
def _onSocketDisconnected(self, msg):
wasReady = self.isReady()
if not self.isConnected():
self._logger.info('Disconnected.')
elif not msg:
msg = 'Peer closed connection.'
if not wasReady:
msg += f' clientId {self.clientId} already in use?'
if msg:
self._logger.error(msg)
self.apiError.emit(msg)
self.wrapper.setEventsDone()
if wasReady:
self.wrapper.connectionClosed()
self.reset()
if wasReady:
self.apiEnd.emit()
# client request methods
# the message type id is sent first, often followed by a version number
def reqMktData(
self, reqId, contract, genericTickList, snapshot,
regulatorySnapshot, mktDataOptions):
fields = [1, 11, reqId, contract]
if contract.secType == 'BAG':
legs = contract.comboLegs or []
fields += [len(legs)]
for leg in legs:
fields += [leg.conId, leg.ratio, leg.action, leg.exchange]
dnc = contract.deltaNeutralContract
if dnc:
fields += [True, dnc.conId, dnc.delta, dnc.price]
else:
fields += [False]
fields += [
genericTickList, snapshot, regulatorySnapshot, mktDataOptions]
self.send(*fields)
def cancelMktData(self, reqId):
self.send(2, 2, reqId)
def placeOrder(self, orderId, contract, order):
version = self.serverVersion()
fields = [3]
if version < 145:
fields += [45]
fields += [
orderId,
contract,
contract.secIdType,
contract.secId,
order.action,
order.totalQuantity,
order.orderType,
order.lmtPrice,
order.auxPrice,
order.tif,
order.ocaGroup,
order.account,
order.openClose,
order.origin,
order.orderRef,
order.transmit,
order.parentId,
order.blockOrder,
order.sweepToFill,
order.displaySize,
order.triggerMethod,
order.outsideRth,
order.hidden]
if contract.secType == 'BAG':
legs = contract.comboLegs or []
fields += [len(legs)]
for leg in legs:
fields += [
leg.conId,
leg.ratio,
leg.action,
leg.exchange,
leg.openClose,
leg.shortSaleSlot,
leg.designatedLocation,
leg.exemptCode]
legs = order.orderComboLegs or []
fields += [len(legs)]
for leg in legs:
fields += [leg.price]
params = order.smartComboRoutingParams or []
fields += [len(params)]
for param in params:
fields += [param.tag, param.value]
fields += [
'',
order.discretionaryAmt,
order.goodAfterTime,
order.goodTillDate,
order.faGroup,
order.faMethod,
order.faPercentage,
order.faProfile,
order.modelCode,
order.shortSaleSlot,
order.designatedLocation,
order.exemptCode,
order.ocaType,
order.rule80A,
order.settlingFirm,
order.allOrNone,
order.minQty,
order.percentOffset,
order.eTradeOnly,
order.firmQuoteOnly,
order.nbboPriceCap,
order.auctionStrategy,
order.startingPrice,
order.stockRefPrice,
order.delta,
order.stockRangeLower,
order.stockRangeUpper,
order.overridePercentageConstraints,
order.volatility,
order.volatilityType,
order.deltaNeutralOrderType,
order.deltaNeutralAuxPrice]
if order.deltaNeutralOrderType:
fields += [
order.deltaNeutralConId,
order.deltaNeutralSettlingFirm,
order.deltaNeutralClearingAccount,
order.deltaNeutralClearingIntent,
order.deltaNeutralOpenClose,
order.deltaNeutralShortSale,
order.deltaNeutralShortSaleSlot,
order.deltaNeutralDesignatedLocation]
fields += [
order.continuousUpdate,
order.referencePriceType,
order.trailStopPrice,
order.trailingPercent,
order.scaleInitLevelSize,
order.scaleSubsLevelSize,
order.scalePriceIncrement]
if (0 < order.scalePriceIncrement < UNSET_DOUBLE):
fields += [
order.scalePriceAdjustValue,
order.scalePriceAdjustInterval,
order.scaleProfitOffset,
order.scaleAutoReset,
order.scaleInitPosition,
order.scaleInitFillQty,
order.scaleRandomPercent]
fields += [
order.scaleTable,
order.activeStartTime,
order.activeStopTime,
order.hedgeType]
if order.hedgeType:
fields += [order.hedgeParam]
fields += [
order.optOutSmartRouting,
order.clearingAccount,
order.clearingIntent,
order.notHeld]
dnc = contract.deltaNeutralContract
if dnc:
fields += [True, dnc.conId, dnc.delta, dnc.price]
else:
fields += [False]
fields += [order.algoStrategy]
if order.algoStrategy:
params = order.algoParams or []
fields += [len(params)]
for param in params:
fields += [param.tag, param.value]
fields += [
order.algoId,
order.whatIf,
order.orderMiscOptions,
order.solicited,
order.randomizeSize,
order.randomizePrice]
if order.orderType == 'PEG BENCH':
fields += [
order.referenceContractId,
order.isPeggedChangeAmountDecrease,
order.peggedChangeAmount,
order.referenceChangeAmount,
order.referenceExchangeId]
fields += [len(order.conditions)]
if order.conditions:
for cond in order.conditions:
fields += dataclassAsTuple(cond)
fields += [
order.conditionsIgnoreRth,
order.conditionsCancelOrder]
fields += [
order.adjustedOrderType,
order.triggerPrice,
order.lmtPriceOffset,
order.adjustedStopPrice,
order.adjustedStopLimitPrice,
order.adjustedTrailingAmount,
order.adjustableTrailingUnit,
order.extOperator,
order.softDollarTier.name,
order.softDollarTier.val,
order.cashQty]
if version >= 138:
fields += [order.mifid2DecisionMaker, order.mifid2DecisionAlgo]
if version >= 139:
fields += [order.mifid2ExecutionTrader, order.mifid2ExecutionAlgo]
if version >= 141:
fields += [order.dontUseAutoPriceForHedge]
if version >= 145:
fields += [order.isOmsContainer]
if version >= 148:
fields += [order.discretionaryUpToLimitPrice]
if version >= 151:
fields += [order.usePriceMgmtAlgo]
if version >= 158:
fields += [order.duration]
if version >= 160:
fields += [order.postToAts]
if version >= 162:
fields += [order.autoCancelParent]
self.send(*fields)
def cancelOrder(self, orderId):
self.send(4, 1, orderId)
def reqOpenOrders(self):
self.send(5, 1)
def reqAccountUpdates(self, subscribe, acctCode):
self.send(6, 2, subscribe, acctCode)
def reqExecutions(self, reqId, execFilter):
self.send(
7, 3, reqId,
execFilter.clientId,
execFilter.acctCode,
execFilter.time,
execFilter.symbol,
execFilter.secType,
execFilter.exchange,
execFilter.side)
def reqIds(self, numIds):
self.send(8, 1, numIds)
def reqContractDetails(self, reqId, contract):
self.send(
9, 8, reqId, contract, contract.includeExpired,
contract.secIdType, contract.secId)
def reqMktDepth(
self, reqId, contract, numRows, isSmartDepth, mktDepthOptions):
version = self.serverVersion()
fields = [
10, 5, reqId,
contract.conId,
contract.symbol,
contract.secType,
contract.lastTradeDateOrContractMonth,
contract.strike,
contract.right,
contract.multiplier,
contract.exchange]
if version >= 149:
fields += [contract.primaryExchange]
fields += [
contract.currency,
contract.localSymbol,
contract.tradingClass,
numRows]
if version >= 146:
fields += [isSmartDepth]
fields += [mktDepthOptions]
self.send(*fields)
def cancelMktDepth(self, reqId, isSmartDepth):
self.send(11, 1, reqId, isSmartDepth)
def reqNewsBulletins(self, allMsgs):
self.send(12, 1, allMsgs)
def cancelNewsBulletins(self):
self.send(13, 1)
def setServerLogLevel(self, logLevel):
self.send(14, 1, logLevel)
def reqAutoOpenOrders(self, bAutoBind):
self.send(15, 1, bAutoBind)
def reqAllOpenOrders(self):
self.send(16, 1)
def reqManagedAccts(self):
self.send(17, 1)
def requestFA(self, faData):
self.send(18, 1, faData)
def replaceFA(self, reqId, faData, cxml):
fields = [19, 1, faData, cxml]
if self.serverVersion() >= 157:
fields += [reqId]
self.send(*fields)
def reqHistoricalData(
self, reqId, contract, endDateTime, durationStr, barSizeSetting,
whatToShow, useRTH, formatDate, keepUpToDate, chartOptions):
fields = [
20, reqId, contract, contract.includeExpired,
endDateTime, barSizeSetting, durationStr, useRTH,
whatToShow, formatDate]
if contract.secType == 'BAG':
legs = contract.comboLegs or []
fields += [len(legs)]
for leg in legs:
fields += [leg.conId, leg.ratio, leg.action, leg.exchange]
fields += [keepUpToDate, chartOptions]
self.send(*fields)
def exerciseOptions(
self, reqId, contract, exerciseAction,
exerciseQuantity, account, override):
self.send(
21, 2, reqId,
contract.conId,
contract.symbol,
contract.secType,
contract.lastTradeDateOrContractMonth,
contract.strike,
contract.right,
contract.multiplier,
contract.exchange,
contract.currency,
contract.localSymbol,
contract.tradingClass,
exerciseAction, exerciseQuantity, account, override)
def reqScannerSubscription(
self, reqId, subscription, scannerSubscriptionOptions,
scannerSubscriptionFilterOptions):
version = self.serverVersion()
sub = subscription
fields = [22]
if version < 143:
fields += [4]
fields += [
reqId,
sub.numberOfRows,
sub.instrument,
sub.locationCode,
sub.scanCode,
sub.abovePrice,
sub.belowPrice,
sub.aboveVolume,
sub.marketCapAbove,
sub.marketCapBelow,
sub.moodyRatingAbove,
sub.moodyRatingBelow,
sub.spRatingAbove,
sub.spRatingBelow,
sub.maturityDateAbove,
sub.maturityDateBelow,
sub.couponRateAbove,
sub.couponRateBelow,
sub.excludeConvertible,
sub.averageOptionVolumeAbove,
sub.scannerSettingPairs,
sub.stockTypeFilter]
if version >= 143:
fields += [scannerSubscriptionFilterOptions]
fields += [scannerSubscriptionOptions]
self.send(*fields)
def cancelScannerSubscription(self, reqId):
self.send(23, 1, reqId)
def reqScannerParameters(self):
self.send(24, 1)
def cancelHistoricalData(self, reqId):
self.send(25, 1, reqId)
def reqCurrentTime(self):
self.send(49, 1)
def reqRealTimeBars(
self, reqId, contract, barSize, whatToShow,
useRTH, realTimeBarsOptions):
self.send(
50, 3, reqId, contract, barSize, whatToShow,
useRTH, realTimeBarsOptions)
def cancelRealTimeBars(self, reqId):
self.send(51, 1, reqId)
def reqFundamentalData(
self, reqId, contract, reportType, fundamentalDataOptions):
options = fundamentalDataOptions or []
self.send(
52, 2, reqId,
contract.conId,
contract.symbol,
contract.secType,
contract.exchange,
contract.primaryExchange,
contract.currency,
contract.localSymbol,
reportType, len(options), options)
def cancelFundamentalData(self, reqId):
self.send(53, 1, reqId)
def calculateImpliedVolatility(
self, reqId, contract, optionPrice, underPrice, implVolOptions):
self.send(
54, 3, reqId, contract, optionPrice, underPrice,
len(implVolOptions), implVolOptions)
def calculateOptionPrice(
self, reqId, contract, volatility, underPrice, optPrcOptions):
self.send(
55, 3, reqId, contract, volatility, underPrice,
len(optPrcOptions), optPrcOptions)
def cancelCalculateImpliedVolatility(self, reqId):
self.send(56, 1, reqId)
def cancelCalculateOptionPrice(self, reqId):
self.send(57, 1, reqId)
def reqGlobalCancel(self):
self.send(58, 1)
def reqMarketDataType(self, marketDataType):
self.send(59, 1, marketDataType)
def reqPositions(self):
self.send(61, 1)
def reqAccountSummary(self, reqId, groupName, tags):
self.send(62, 1, reqId, groupName, tags)
def cancelAccountSummary(self, reqId):
self.send(63, 1, reqId)
def cancelPositions(self):
self.send(64, 1)
def verifyRequest(self, apiName, apiVersion):
self.send(65, 1, apiName, apiVersion)
def verifyMessage(self, apiData):
self.send(66, 1, apiData)
def queryDisplayGroups(self, reqId):
self.send(67, 1, reqId)
def subscribeToGroupEvents(self, reqId, groupId):
self.send(68, 1, reqId, groupId)
def updateDisplayGroup(self, reqId, contractInfo):
self.send(69, 1, reqId, contractInfo)
def unsubscribeFromGroupEvents(self, reqId):
self.send(70, 1, reqId)
def startApi(self):
self.send(71, 2, self.clientId, self.optCapab)
def verifyAndAuthRequest(self, apiName, apiVersion, opaqueIsvKey):
self.send(72, 1, apiName, apiVersion, opaqueIsvKey)
def verifyAndAuthMessage(self, apiData, xyzResponse):
self.send(73, 1, apiData, xyzResponse)
def reqPositionsMulti(self, reqId, account, modelCode):
self.send(74, 1, reqId, account, modelCode)
def cancelPositionsMulti(self, reqId):
self.send(75, 1, reqId)
def reqAccountUpdatesMulti(self, reqId, account, modelCode, ledgerAndNLV):
self.send(76, 1, reqId, account, modelCode, ledgerAndNLV)
def cancelAccountUpdatesMulti(self, reqId):
self.send(77, 1, reqId)
def reqSecDefOptParams(
self, reqId, underlyingSymbol, futFopExchange,
underlyingSecType, underlyingConId):
self.send(
78, reqId, underlyingSymbol, futFopExchange,
underlyingSecType, underlyingConId)
def reqSoftDollarTiers(self, reqId):
self.send(79, reqId)
def reqFamilyCodes(self):
self.send(80)
def reqMatchingSymbols(self, reqId, pattern):
self.send(81, reqId, pattern)
def reqMktDepthExchanges(self):
self.send(82)
def reqSmartComponents(self, reqId, bboExchange):
self.send(83, reqId, bboExchange)
def reqNewsArticle(
self, reqId, providerCode, articleId, newsArticleOptions):
self.send(84, reqId, providerCode, articleId, newsArticleOptions)
def reqNewsProviders(self):
self.send(85)
def reqHistoricalNews(
self, reqId, conId, providerCodes, startDateTime, endDateTime,
totalResults, historicalNewsOptions):
self.send(
86, reqId, conId, providerCodes, startDateTime, endDateTime,
totalResults, historicalNewsOptions)
def reqHeadTimeStamp(
self, reqId, contract, whatToShow, useRTH, formatDate):
self.send(
87, reqId, contract, contract.includeExpired,
useRTH, whatToShow, formatDate)
def reqHistogramData(self, tickerId, contract, useRTH, timePeriod):
self.send(
88, tickerId, contract, contract.includeExpired,
useRTH, timePeriod)
def cancelHistogramData(self, tickerId):
self.send(89, tickerId)
def cancelHeadTimeStamp(self, reqId):
self.send(90, reqId)
def reqMarketRule(self, marketRuleId):
self.send(91, marketRuleId)
def reqPnL(self, reqId, account, modelCode):
self.send(92, reqId, account, modelCode)
def cancelPnL(self, reqId):
self.send(93, reqId)
def reqPnLSingle(self, reqId, account, modelCode, conid):
self.send(94, reqId, account, modelCode, conid)
def cancelPnLSingle(self, reqId):
self.send(95, reqId)
def reqHistoricalTicks(
self, reqId, contract, startDateTime, endDateTime,
numberOfTicks, whatToShow, useRth, ignoreSize, miscOptions):
self.send(
96, reqId, contract, contract.includeExpired,
startDateTime, endDateTime, numberOfTicks, whatToShow,
useRth, ignoreSize, miscOptions)
def reqTickByTickData(
self, reqId, contract, tickType, numberOfTicks, ignoreSize):
self.send(97, reqId, contract, tickType, numberOfTicks, ignoreSize)
def cancelTickByTickData(self, reqId):
self.send(98, reqId)
def reqCompletedOrders(self, apiOnly):
self.send(99, apiOnly)