This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
tickstreamer_demo.py
77 lines (64 loc) · 2.18 KB
/
tickstreamer_demo.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
import ibapi
from tws_async import TWSClient, iswrapper, Stock, Forex, CFD
class TickStreamer(TWSClient):
"""
Get live streaming tick data from TWS or gateway server.
"""
def __init__(self):
TWSClient.__init__(self)
self.accountName = ''
self._reqIdSeq = 0
self._reqId2Contract = {}
def subscribe(self):
contracts = [
Stock('GOOG'),
Stock('INTC', primaryExchange='NASDAQ'),
Forex('EURUSD'),
CFD('IBUS500')
]
for contract in contracts:
reqId = self.getReqId()
self._reqId2Contract[reqId] = contract
self.reqMktData(reqId, contract, genericTickList='', snapshot=False,
regulatorySnapshot=False, mktDataOptions=[])
@iswrapper
def connectAck(self):
self.reqAccountUpdates(1, '')
self.reqOpenOrders()
self.reqPositions()
@iswrapper
def tickPrice(self, reqId: int,
tickType: ibapi.ticktype.TickType,
price: float,
attrib: ibapi.common.TickAttrib):
contract = self._reqId2Contract[reqId]
print('{} price {}'.format(contract.symbol, price))
@iswrapper
def tickSize(self, reqId: int,
tickType: ibapi.ticktype.TickType,
size: int):
contract = self._reqId2Contract[reqId]
print('{} size {}'.format(contract.symbol, size))
@iswrapper
def updateAccountTime(self, timeStamp: str):
print('Time {}'.format(timeStamp))
@iswrapper
def accountDownloadEnd(self, accountName: str):
self.accountName = accountName
@iswrapper
def updateAccountValue(self, key: str, val: str, currency: str,
accountName: str):
print('Account update: {} = {} {}'.format(key, val, currency))
@iswrapper
def position(self, account: str,
contract: ibapi.contract.Contract,
position: float,
avgCost: float):
print('Position: {} {} @ {}'.format(position, contract.symbol, avgCost))
@iswrapper
def positionEnd(self):
pass
tws = TickStreamer()
tws.connect(host='127.0.0.1', port=7497, clientId=1)
tws.subscribe()
tws.run()