-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainprog.py
174 lines (128 loc) · 4.79 KB
/
mainprog.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
#
# Main program using IBFramework
#
# Features:
# - Reading account value every interval
#
# - Stock data is saved in StockData object class for each stock ticker
# StockData has the data structure, such as position, symbol, pnl, etc
# self.symbols = a list contains the symbol list used in the program lifetime
# self.symbols --> converted to IB Contract Class (java api ref) --> stored in StockData.contract
#
# -
__author__ = 'ssg'
from time import sleep
import misc.ibdata_types as datatype
from classes.ibaccount import IBAccount
from classes.ibevents import IBEvents
from ibframework import IBFramework
import time
import sys
from PyQt4.QtGui import *
ACCOUNT_CODES = ['U129661', 'U1465027']
def __event_handler(msg):
if msg.typeName == datatype.MSG_TYPE_HISTORICAL_DATA:
__on_historical_data(msg)
elif msg.typeName == datatype.MSG_TYPE_UPDATE_PORTFOLIO:
__on_portfolio_update(msg)
elif msg.typeName == datatype.MSG_TYPE_MANAGED_ACCOUNTS:
account_code = msg.accountsList
elif msg.typeName == datatype.MSG_TYPE_NEXT_ORDER_ID:
order_id = msg.orderId
elif msg.typeName == datatype.MSG_TYPE_UPDATE_ACCOUNT_VALUE:
__on_account_value(msg)
elif msg.typeName == datatype.MSG_TYPE_ACCOUNT_SUMMARY:
print msg
__on_account_summary(msg)
elif msg.typeName == datatype.MSG_TYPE_POSITION:
#print msg.typeName
print msg
print msg.contract.m_symbol
elif msg.typeName == datatype.MSG_TYPE_POSITION_END:
print "PositionEnd()"
else:
print msg
def __error_handler(msg):
if msg.typeName == "error" and msg.id != -1:
print "Server Error:", msg
def __tick_event_handler(msg):
pass
"""
ticker_id = msg.tickerId
field_type = msg.field
# Store information from last traded price
if field_type == datatype.FIELD_LAST_PRICE:
last_price = msg.price
self.__add_market_data(ticker_id, dt.datetime.now(), last_price)
self.__trim_data_series()
"""
def __on_account_summary(msg):
if msg.tag == "NetLiquidation":
print "Net Liquidation = " + msg.value
result = QMessageBox.question(w, 'Message', "Net liquidation exceeds", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
print msg
"""
key value currency accountName
"""
def __on_account_value(msg):
if msg.key == datatype.MSG_KEY_NET_LIQUIDATION:
print msg
print msg.accountName
def __on_historical_data(msg):
print msg
ticker_index = msg.reqId
if msg.WAP == -1:
print "Done"
#self.__on_historical_data_completed(ticker_index)
else:
print " "
#self.__add_historical_data(ticker_index, msg)
print msg
def __on_historical_data_completed(ticker_index):
pass
def __on_portfolio_update(msg):
print msg
print msg.contract.m_symbol
print msg.contract.m_expiry
print msg.contract.m_strike
print msg.contract.m_tradingClass
print msg.contract.m_right
"""
for key, stock_data in self.stocks_data.iteritems():
if stock_data.contract.m_symbol == msg.contract.m_symbol:
stock_data.update_position(msg.position,
msg.marketPrice,
msg.marketValue,
msg.averageCost,
msg.unrealizedPNL,
msg.realizedPNL,
msg.accountName)
return
"""
if __name__ == "__main__":
# init framework that contains symbols, position, etc
ib_framework = IBFramework()
acc = IBAccount(3) # client id = 3
acc.connect_to_tws()
sleep(1)
# Register events using IB Events
events = IBEvents(acc.tws_conn)
# Types of events: other events, error handlers, market data (ticks)
events.register_callback_functions(__event_handler, __error_handler, __tick_event_handler)
acc.tws_conn.reqPositions()
#acc.tws_conn.reqAccountUpdates(1, ACCOUNT_CODES[1])
acc.tws_conn.reqAccountSummary(2, 'All', 'NetLiquidation,BuyingPower,TotalCashValue')
sleep(3)
#acc.tws_conn.reqAccountUpdates(0, ACCOUNT_CODES[1])
# Test Historical data
"""
ib_framework.init_stocks_data(["SPY", "QQQ"])
ib_framework.request_historical_data(acc.tws_conn,
#time.strftime(datatype.DATE_TIME_FORMAT), # end time is now
'20151118 13:00:00',
datatype.DURATION_1_HR,
datatype.BAR_SIZE_5_MIN,
datatype.WHAT_TO_SHOW_TRADES,
datatype.RTH_ONLY_TRADING_HRS)
"""
print('disconnected', acc.disconnect_from_tws())