Skip to content

Commit e6cf59e

Browse files
add options order
1 parent 9b9be01 commit e6cf59e

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

options_order.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from ibapi.client import EClient
2+
from ibapi.wrapper import EWrapper
3+
from ibapi.contract import Contract
4+
from ibapi.order import *
5+
6+
import threading
7+
import time
8+
9+
10+
class IBapi(EWrapper, EClient):
11+
def __init__(self):
12+
EClient.__init__(self, self)
13+
14+
def nextValidId(self, orderId: int):
15+
super().nextValidId(orderId)
16+
self.nextorderId = orderId
17+
print('The next valid order id is: ', self.nextorderId)
18+
19+
def orderStatus(self, orderId, status, filled, remaining, avgFullPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice):
20+
print('orderStatus - orderid:', orderId, 'status:', status, 'filled', filled, 'remaining', remaining, 'lastFillPrice', lastFillPrice)
21+
22+
def openOrder(self, orderId, contract, order, orderState):
23+
print('openOrder id:', orderId, contract.symbol, contract.secType, '@', contract.exchange, ':', order.action, order.orderType, order.totalQuantity, orderState.status)
24+
25+
def execDetails(self, reqId, contract, execution):
26+
print('Order Executed: ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId, execution.orderId, execution.shares, execution.lastLiquidity)
27+
28+
29+
def run_loop():
30+
app.run()
31+
32+
33+
34+
app = IBapi()
35+
app.connect('127.0.0.1', 7496, 123)
36+
37+
app.nextorderId = None
38+
39+
#Start the socket in a thread
40+
api_thread = threading.Thread(target=run_loop, daemon=True)
41+
api_thread.start()
42+
43+
#Check if the API is connected via orderid
44+
while True:
45+
if isinstance(app.nextorderId, int):
46+
print('connected')
47+
print()
48+
break
49+
else:
50+
print('waiting for connection')
51+
time.sleep(1)
52+
53+
#Create contract
54+
contract = Contract()
55+
contract.symbol = 'TSLA'
56+
contract.secType = 'OPT'
57+
contract.exchange = 'SMART'
58+
contract.lastTradeDateOrContractMonth = '20201002'
59+
contract.strike = 424
60+
contract.right = 'C'
61+
contract.multiplier = '100'
62+
63+
#Create order object
64+
order = Order()
65+
order.action = 'BUY'
66+
order.totalQuantity = 1
67+
order.orderType = 'MKT'
68+
69+
#Place order
70+
app.placeOrder(app.nextorderId, contract, order)
71+
72+
73+
time.sleep(3)
74+
app.disconnect()

0 commit comments

Comments
 (0)