/
ib_trade.py
74 lines (48 loc) · 1.29 KB
/
ib_trade.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
from functools import wraps
from ib.opt import Connection, message #Import IbPy
from ib.ext.Contract import Contract
from ib.ext.Order import Order
def counter(func):
@wraps(func)
def tmp(*args, **kwargs):
tmp.count += 1
return func(*args, **kwargs)
tmp.count = 73
return tmp
def make_contract(symbol, sec_type, exch, prim_exch, curr):
Contract.m_symbol = symbol
Contract.m_secType = sec_type
Contract.m_exchange = exch
Contract.m_primaryExch = prim_exch
Contract.m_currency = curr
return Contract
def make_order(action, quantity, price=None):
if price is not None:
print(price)
order = Order()
order.m_orderType = 'LMT'
order.m_totalQuantity = quantity
order.m_action = action
order.m_lmtPrice = price
else:
order = Order()
order.m_orderType = 'MKT'
order.m_totalQuantity = quantity
order.m_action = action
return order
@counter
def main():
conn = Connection.create(port=7497, clientId=88) #Has to be connected to Global settings of TWS
conn.connect()
oid = main.count #Order number
print (oid)
cont = make_contract('anf', 'STK', 'SMART', 'SMART', 'USD') #Check exch name for stock symbol
offer = make_order('BUY',100,16.30)
conn.placeOrder(oid, cont, offer)
print(oid, str(cont), offer)
conn.disconnect()
main()
# main()
# main()
# main()
# main()