-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathibtest1.py
More file actions
162 lines (128 loc) · 5.14 KB
/
ibtest1.py
File metadata and controls
162 lines (128 loc) · 5.14 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
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
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import *
import threading
import time
class IBapi(EWrapper, EClient):
lastAction = None
def __init__(self):
EClient.__init__(self, self)
def nextValidId(self, orderId: int):
super().nextValidId(orderId)
self.nextorderId = orderId
print('The next valid order id is: ', self.nextorderId)
def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice):
print('orderStatus - orderid:', orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice)
contract2 = Contract() # Creates a contract object from the import
contract2.symbol = "QQQ" # Sets the ticker symbol
contract2.secType = "STK" # Defines the security type as stock
contract2.currency = "USD" # Currency is US dollars
# In the API side, NASDAQ is always defined as ISLAND in the exchange field
contract2.exchange = "SMART"
# SELL + SHORT (TRAIL STOP LIMIT)
if self.lastAction == "BUY" and remaining == 0 and status == "Filled" and app.nextorderId-1 == orderId :
#Create order object
orderSell = Order()
orderSell.action = 'SELL'
orderSell.totalQuantity = 60 # 30 sell + 30 short
orderSell.orderType = "TRAIL LIMIT"
orderSell.trailStopPrice = avgFillPrice + 0.10
orderSell.lmtPriceOffset = 0.05
orderSell.auxPrice = 0.10
orderSell.orderId = self.nextorderId
orderSell.tif = "GTC"
self.nextorderId += 1
orderSell.transmit = True
print('--- Decided to', orderSell.action)
# time.sleep(5)
self.placeOrder(orderSell.orderId, contract2, orderSell)
self.lastAction = orderSell.action
print("--- This just happened:",self.lastAction, orderSell.orderId)
# BUYBACK + BUY (TRAIL STOP LOSS)
elif self.lastAction == "SELL" and remaining == 0 and status == "Filled" and app.nextorderId-1 == orderId :
#Create order object
orderSell = Order()
orderSell.action = 'BUY'
orderSell.totalQuantity = 60 # 30 sell + 30 short
orderSell.orderType = "TRAIL LIMIT"
orderSell.trailStopPrice = avgFillPrice
orderSell.lmtPriceOffset = 0.05
orderSell.auxPrice = 0.20
orderSell.orderId = self.nextorderId
orderSell.tif = "GTC"
self.nextorderId += 1
orderSell.transmit = True
print('--- Decided to', orderSell.action)
# time.sleep(5)
self.placeOrder(orderSell.orderId, contract2, orderSell)
self.lastAction = orderSell.action
print("--- This just happened:",self.lastAction, orderSell.orderId)
# # BUY (TRAIL STOP LOSS)
# elif self.lastAction == "SELL" and remaining == 0 and status == "Filled" and app.nextorderId-1 == orderId :
# #Create order object
# orderBuy = Order()
# orderBuy.action = 'BUY'
# orderBuy.totalQuantity = 30
# orderBuy.orderType = "TRAIL LIMIT"
# # orderBuy.trailStopPrice = 346.30
# orderBuy.lmtPriceOffset = 0.05
# orderBuy.auxPrice = 0.10
# orderBuy.tif = "GTC"
# orderBuy.orderId = app.nextorderId
# app.nextorderId += 1
# orderBuy.transmit = True
# print('--- Decided to', orderBuy.action)
# # time.sleep(5)
# self.placeOrder(orderBuy.orderId, contract2, orderBuy)
# self.lastAction = orderBuy.action
# print("--- This just happened:",self.lastAction, orderBuy.orderId)
else:
print("--- Nothing to do. Will wait for next order update.\n\n")
def openOrder(self, orderId, contract, order, orderState):
print('openOrder id:', orderId, contract.symbol, contract.secType, '@', contract.exchange, ':', order.action, order.orderType, order.totalQuantity, orderState.status)
def execDetails(self, reqId, contract, execution):
print('Order Executed: ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId, execution.orderId, execution.shares, execution.lastLiquidity, execution)
def run_loop():
app.run()
app = IBapi()
app.connect('127.0.0.1', 7497, 123)
currentTime = EWrapper.reqCurrentTime
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')
print()
break
else:
print('waiting for connection')
# time.sleep(1)
#Create contract
contract = Contract() # Creates a contract object from the import
contract.symbol = "QQQ" # Sets the ticker symbol
contract.secType = "STK" # Defines the security type as stock
contract.currency = "USD" # Currency is US dollars
# In the API side, NASDAQ is always defined as ISLAND in the exchange field
contract.exchange = "SMART"
#Create order object
orderBuy = Order()
orderBuy.action = 'BUY'
orderBuy.totalQuantity = 30
orderBuy.orderType = "TRAIL"
# orderBuy.trailStopPrice = 346.30
# orderBuy.lmtPriceOffset = 0.05
orderBuy.auxPrice = 0.20
orderBuy.tif = "GTC"
orderBuy.orderId = app.nextorderId
app.nextorderId += 1
orderBuy.transmit = True
#Place orders
app.placeOrder(orderBuy.orderId, contract, orderBuy)
app.lastAction = orderBuy.action
print("--- This just happened:",app.lastAction, orderBuy.orderId)
print(currentTime)
app.disconnect()