-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ibkr.py
101 lines (81 loc) · 2.69 KB
/
test_ibkr.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
import pickle
import threading
import time
from trading import IBAccount
from ibapi.contract import Contract
from ibapi.order import *
from utils import PrintLogger
def run_loop():
app.run()
def Stock_contract(symbol, secType="STK", exchange="SMART", currency="USD"):
"""custom function to create stock contract"""
contract = Contract()
contract.symbol = symbol
contract.secType = secType
contract.exchange = exchange
contract.currency = currency
return contract
if __name__ == "__main__":
app = IBAccount(PrintLogger())
app.connect("127.0.0.1", 7496, 124)
app.nextorderId = None
# Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()
# Check if the API is connected via orderid
while True:
if isinstance(app.nextorderId, int):
print("connected")
break
else:
print("waiting for connection")
time.sleep(1)
# Create contract object
apple_contract = Stock_contract("AAPL")
# Create order object
order = Order()
order.action = "BUY"
order.totalQuantity = 1
order.orderType = "LMT"
order.lmtPrice = "150"
# Request Market Data
# _start = time.time()
# app.placeOrder(app.nextorderId, apple_contract, order)
# print(f"Took {(time.time() - _start) * 1000:.2f} ms")
# time.sleep(3)
# _start = time.time()
# app.cancelOrder(app.nextorderId)
# print(f"Took {(time.time() - _start) * 1000:.2f} ms")
_start = time.time()
# app.get_executions("AAPL")
buy_order = app.buy_limit("AAPL", 1, 159)
# buy_order = app.buy_market("AAPL", 1)
print(buy_order)
# print(buy_order)
print(f"Took {(time.time() - _start) * 1000:.2f} ms")
time.sleep(1)
order = app.get_order_info(buy_order.order_id)
print(order)
_start = time.time()
# sell_order = app.sell_limit("AAPL", 1, 159.5)
cancelled = app.cancel_order(buy_order.order_id)
print("State from cancel function:")
print(cancelled)
print(f"Took {(time.time() - _start) * 1000:.2f} ms")
# time.sleep(1)
# order = app.get_order_info(buy_order.order_id)
# print("After cancelling:")
# print(order)
# sell_order_updated = app.get_order_info(sell_order.order_id)
# time.sleep(1) # Sleep interval to allow time for incoming price data
# with open("ibkr_test_output.pkl", "wb") as out_file:
# pickle.dump(
# {
# "buy_resp": buy_order,
# "buy_order": order,
# "sell_resp": sell_order,
# "sell_order": sell_order_updated,
# },
# out_file,
# )
app.disconnect()