-
Notifications
You must be signed in to change notification settings - Fork 11
/
Fut_Opt_trade_second_version.py
284 lines (229 loc) · 11.4 KB
/
Fut_Opt_trade_second_version.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# -*- coding: utf-8 -*-
"""
Created on Mon May 18 23:43:59 2020
@author: alial
"""
import numpy as np
from ib_insync import IB, MarketOrder, util, Future, FuturesOption
import datetime
import talib as ta
import nest_asyncio
nest_asyncio.apply()
class trade_ES():
def __init__(self):
self.ib = IB()
self.ib.connect('127.0.0.1', 7497, clientId=np.random.randint(10, 1000))
self.tickers_ret = {}
self.endDateTime = ''
self.No_days = '43200 S'
self.interval = '30 secs'
self.tickers_signal = "Hold"
self.ES = Future(symbol='ES', lastTradeDateOrContractMonth='20200619', exchange='GLOBEX',
currency='USD')
self.ib.qualifyContracts(self.ES)
self.ES_df = self.ib.reqHistoricalData(contract=self.ES, endDateTime=self.endDateTime, durationStr=self.No_days,
barSizeSetting=self.interval, whatToShow='TRADES', useRTH=False,
keepUpToDate=True)
self.tickers_ret = []
self.options_ret = []
self.option = {'call': FuturesOption, 'put': FuturesOption}
self.options_history = {}
self.trade_options = {'call': [], 'put': []}
self.price = 0
self.i = -1
self.ES_df.updateEvent += self.make_clean_df
self.Buy = True
self.Sell = False
self.ib.positionEvent += self.order_verify
self.waitTimeInSeconds = 120
self.tradeTime = 0
def run(self):
while self.ib.waitOnUpdate():
util.allowCtrlC()
self.ib.setCallback('error', x.checkError)
self.make_clean_df(self.ES_df)
def next_exp_weekday(self):
weekdays = {2: [6, 0], 4: [0, 1, 2], 0: [3, 4]}
today = datetime.date.today().weekday()
for exp, day in weekdays.items():
if today in day:
return exp
def next_weekday(self, d, weekday):
days_ahead = weekday - d.weekday()
if days_ahead <= 0: # Target day already happened this week
days_ahead += 7
date_to_return = d + datetime.timedelta(days_ahead) # 0 = Monday, 1=Tuself.ESday, 2=Wednself.ESday...
return date_to_return.strftime('%Y%m%d')
def get_strikes_and_expiration(self):
expiration = self.next_weekday(datetime.date.today(), self.next_exp_weekday())
chains = self.ib.reqSecDefOptParams(underlyingSymbol='ES', futFopExchange='GLOBEX', underlyingSecType='FUT',
underlyingConId=self.ES.conId)
chain = util.df(chains)
strikes = chain[chain['expirations'].astype(str).str.contains(expiration)].loc[:, 'strikes'].values[0]
[ESValue] = self.ib.reqTickers(self.ES)
ES_price = ESValue.marketPrice()
strikes = [strike for strike in strikes
if strike % 5 == 0
and ES_price - 10 < strike < ES_price + 10]
return strikes, expiration
def get_contract(self, right, net_liquidation):
strikes, expiration = self.get_strikes_and_expiration()
for strike in strikes:
contract = FuturesOption(symbol='ES', lastTradeDateOrContractMonth=expiration,
strike=strike, right=right, exchange='GLOBEX')
self.ib.qualifyContracts(contract)
self.price = self.ib.reqMktData(contract, "", False, False)
if float(self.price.last) * 50 >= net_liquidation:
continue
else:
return contract
def make_clean_df(self, ES_df, hashbar=None):
ES_df = util.df(ES_df)
ES_df['RSI'] = ta.RSI(ES_df['close'])
ES_df['macd'], ES_df['macdsignal'], ES_df['macdhist'] = ta.MACD(ES_df['close'], fastperiod=12, slowperiod=26,
signalperiod=9)
ES_df['MA_9'] = ta.MA(ES_df['close'], timeperiod=9)
ES_df['MA_21'] = ta.MA(ES_df['close'], timeperiod=21)
ES_df['MA_200'] = ta.MA(ES_df['close'], timeperiod=200)
ES_df['EMA_9'] = ta.EMA(ES_df['close'], timeperiod=9)
ES_df['EMA_21'] = ta.EMA(ES_df['close'], timeperiod=21)
ES_df['EMA_200'] = ta.EMA(ES_df['close'], timeperiod=200)
ES_df['ATR'] = ta.ATR(ES_df['high'], ES_df['low'], ES_df['close'])
ES_df['roll_max_cp'] = ES_df['high'].rolling(20).max()
ES_df['roll_min_cp'] = ES_df['low'].rolling(20).min()
ES_df['roll_max_vol'] = ES_df['volume'].rolling(20).max()
ES_df.dropna(inplace=True)
self.loop_function(ES_df)
def placeOrder(self, contract, order):
trade = self.ib.placeOrder(contract, order)
tradeTime = datetime.datetime.now()
return([trade, contract, tradeTime])
def sell(self, contract, position):
self.ib.qualifyContracts(contract)
if position.position>0:
order = 'Sell'
else:
order = 'Buy'
marketorder = MarketOrder(order, abs(position.position))
if self.tradeTime!=0:
timeDelta = datetime.datetime.now() - self.tradeTime
if timeDelta.seconds > self.waitTimeInSeconds:
marketTrade, contract, self.tradeTime = self.placeOrder(contract, marketorder)
else:
marketTrade, contract, tradeTime = self.placeOrder(contract, marketorder)
condition = marketTrade.isDone
timeout = 20
for c in self.ib.loopUntil(condition=condition, timeout=timeout):
marketorder = MarketOrder('Sell', position.position)
marketTrade = self.ib.placeOrder(contract, marketorder)
if not condition == 'Filled':
self.ib.cancelOrder(marketorder)
marketorder = MarketOrder('Sell', position.position)
marketTrade = self.ib.placeOrder(contract, marketorder)
def buy(self, contract):
self.ib.qualifyContracts(contract)
marketorder = MarketOrder('Buy', 1)
if self.tradeTime!=0:
timeDelta = datetime.datetime.now() - self.tradeTime
if timeDelta.seconds > self.waitTimeInSeconds:
marketTrade, contract, self.tradeTime = self.placeOrder(contract, marketorder)
else:
marketTrade, contract, tradeTime = self.placeOrder(contract, marketorder)
condition = marketTrade.isDone
timeout = 10
for c in self.ib.loopUntil(condition=condition, timeout=timeout):
marketorder = MarketOrder('Buy', 1)
marketTrade = self.ib.placeOrder(contract, marketorder)
if not condition == 'Filled':
self.ib.cancelOrder(marketorder)
marketorder = MarketOrder('Buy', 1)
marketTrade = self.ib.placeOrder(contract, marketorder)
def order_verify(self, order):
if order.position == 0.0 or order.position < 0:
self.Buy= True
self.Sell= False
elif order.position > 0:
self.Buy = False
self.Sell = True
else:
self.Buy = False
self.Sell = False
print(f'Buy= {self.Buy}, sell = {self.Sell}')
def loop_function(self, ES_df):
df = ES_df[
['high', 'low', 'volume', 'close', 'RSI', 'ATR', 'roll_max_cp', 'roll_min_cp', 'roll_max_vol', 'EMA_9',
'EMA_21', 'macd', 'macdsignal']]
if self.tickers_signal == "Hold":
print('Hold')
if df["high"].iloc[self.i] >= df["roll_max_cp"].iloc[self.i] and \
df["volume"].iloc[self.i] > df["roll_max_vol"].iloc[self.i - 1] and df['RSI'].iloc[self.i] > 30 \
and df['macd'].iloc[self.i] > df['macdsignal'].iloc[self.i] :
self.tickers_signal = "Buy"
return
elif df["low"].iloc[self.i] <= df["roll_min_cp"].iloc[self.i] and \
df["volume"].iloc[self.i] > df["roll_max_vol"].iloc[self.i - 1] and df['RSI'].iloc[self.i] < 70 \
and df['macd'].iloc[self.i] < df['macdsignal'].iloc[self.i]:
self.tickers_signal = "Sell"
return
elif self.tickers_signal == "Buy":
print('BUY SIGNAL')
if df["close"].iloc[self.i] > df["close"].iloc[self.i - 1] - (0.75 * df["ATR"].iloc[self.i - 1]) and self.Sell and not self.Buy:
self.tickers_signal = "Hold"
positions = self.ib.positions()
for position in positions:
if position.contract.right == 'C':
self.sell(position.contract, position)
return
elif df["low"].iloc[self.i] <= df["roll_min_cp"].iloc[self.i] and \
df["volume"].iloc[self.i] > df["roll_max_vol"].iloc[self.i - 1] and df['RSI'].iloc[self.i] < 70 \
and df['macd'].iloc[self.i] < df['macdsignal'].iloc[self.i] and self.Sell and not self.Buy:
self.tickers_signal = "Sell"
positions = self.ib.positions()
for position in positions:
if position.contract.right == 'C':
self.sell(position.contract, position)
return
self.option['put'] = self.get_contract(right="P", net_liquidation=2000)
self.buy(self.option['put'])
elif not self.Sell and self.Buy:
self.option['call'] = self.get_contract(right="C", net_liquidation=2000)
self.buy(self.option['call'])
elif self.tickers_signal == "Sell":
print('SELL SIGNAL')
if df["close"].iloc[self.i] < df["close"].iloc[self.i - 1] + (0.75 * df["ATR"].iloc[self.i - 1]) and self.Sell and not self.Buy:
self.tickers_signal = "Hold"
positions = self.ib.positions()
for position in positions:
if position.contract.right == 'P':
self.sell(position.contract, position)
return
elif df["high"].iloc[self.i] >= df["roll_max_cp"].iloc[self.i] and \
df["volume"].iloc[self.i] > df["roll_max_vol"].iloc[self.i - 1] and df['RSI'].iloc[self.i] > 30 \
and df['macd'].iloc[self.i] > df['macdsignal'].iloc[self.i] and self.Sell and not self.Buy:
self.tickers_signal = "Buy"
positions = self.ib.positions()
for position in positions:
if position.contract.right == 'P':
self.sell(position.contract, position)
return
self.option['call'] = self.get_contract(right="C", net_liquidation=2000)
self.buy(self.option['call'])
elif not self.Sell and self.Buy:
self.option['put'] = self.get_contract(right="P", net_liquidation=2000)
self.buy(self.option['put'])
def checkError(self, errCode, errString):
print('Error Callback', errCode, errString)
if errCode == 2104:
print('re-connect after 5 secs')
self.ib.sleep(5)
self.ib.disconnect()
self.ib.connect('127.0.0.1', 7497, clientId=np.random.randint(10, 1000))
self.make_clean_df(self.ES)
if __name__ == '__main__':
ib=IB()
x = trade_ES()
try:
while ib.waitOnUpdate():
x.run()
except Exception as error:
print(error)