-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathibProgram2.1.py
More file actions
161 lines (124 loc) · 4.74 KB
/
ibProgram2.1.py
File metadata and controls
161 lines (124 loc) · 4.74 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
from ibapi import order
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
# Quantity Logic for # of shares
# buyQuantity = 30
# if currentTime < 14:30:
# sellQuantity = 30
# else:
# sellQuantity = 60
# if sharesfilled == 30:
# buybackQuantity = 30
# elif sharesfilled = 60:
# buybackQuantity = 60
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 = "VT" # 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"
# After Long --- Do Short
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 = 200 # Try 150 next
orderSell.orderType = "TRAIL LIMIT"
orderSell.trailStopPrice = avgFillPrice - 0.10
orderSell.lmtPriceOffset = 0.20
orderSell.auxPrice = 0.20
orderSell.orderId = self.nextorderId
orderSell.tif = "GTC"
orderSell.outsideRth = True
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)
# After Short --- Do Long
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 = 200 # 30 sell + 30 short
orderBuy.orderType = "TRAIL LIMIT"
orderBuy.trailStopPrice = avgFillPrice + 0.10
orderBuy.lmtPriceOffset = 1.00
orderBuy.auxPrice = 0.20
orderBuy.orderId = self.nextorderId
orderBuy.tif = "GTC"
orderBuy.outsideRth = True
self.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 = app.reqCurrentTime
# print(currentTime)
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 = "VT" # 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
# Open
orderBuy = Order()
orderBuy.action = 'BUY'
orderBuy.totalQuantity = 100
orderBuy.orderType = "TRAIL"
# orderBuy.trailStopPrice = 346.30
# orderBuy.lmtPriceOffset = 0.05
orderBuy.auxPrice = 0.20
orderBuy.tif = "DAY"
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)
# app.disconnect()
# ADD Stop for short at $5.00 loss