-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtestIbPy.py
More file actions
51 lines (44 loc) · 1.28 KB
/
testIbPy.py
File metadata and controls
51 lines (44 loc) · 1.28 KB
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
from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
def error_handler(msg):
"""Handles the capturing of error messages"""
print "Server Error: %s" % msg
def reply_handler(msg):
"""Handles of server replies"""
print "Server Response: %s, %s" % (msg.typeName, msg)
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:
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
def test_make_order():
conn = Connection.create(port=7496, clientId=99)
conn.connect()
conn.register(error_handler, 'Error')
conn.registerAll(reply_handler)
print 'connect'
oid = 7;
contract = make_contract('TSLA', 'STK', 'SMART', 'SMART', 'USD')
offer = make_order('BUY', 100, 200)
print contract
print offer
conn.placeOrder(oid, contract, offer)
#conn.disconnect()
test_make_order()
print "hello"