forked from J-e-e-t/Algo-Trading
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHypothesis Testing 01 - Straddle.py
283 lines (177 loc) · 10.7 KB
/
Hypothesis Testing 01 - Straddle.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
#import libraries
import pandas as pd
import datetime
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
from dateutil.relativedelta import relativedelta, TH
#fetch all the files from the diretory
path = pd.DataFrame(glob('/nfo_2019-20_data/*'),columns=['location'])
path['data_date'] = path['location'].apply(lambda x: x.split('_')[-1].split('.')[0])
path['data_date'] = path['data_date'].apply(lambda x: datetime.datetime.strptime(x,'%Y-%m-%d'))
path = path.sort_values(['data_date'])
path.reset_index(drop=True,inplace=True)
intraday_trade_log = pd.DataFrame(columns=['Entry_Datetime','Future_Traded_Price','ATM','Days_to_Expiry','CE_Symbol','CE_Entry_Price','CE_Exit_Price','CE_Exit_Datetime','PE_Symbol','PE_Entry_Price','PE_Exit_Price','PE_Exit_Datetime','PnL'])
for index, row in path.iterrows():
try:
print(index)
data = pd.read_pickle(row['location'])
entry_datetime = datetime.datetime.combine(row['data_date'].date(),datetime.time(9,20))
exit_datetime = datetime.datetime.combine(row['data_date'].date(),datetime.time(15,10))
#I-CurrentMonth
#II-NextMonth
#III-NextToNextMonth
data['expiry_type'] = np.where((data['instrument_type'] == 'FUT'),data['ticker'].apply(lambda x: x.split('-')[-1].split('.')[0]),'')
future_expiry_offset = 'I'
instrument = 'BANKNIFTY'
base = 100
futures_data = data[(data['instrument_type'] == 'FUT') & (data['instrument_name'] == instrument) & (data['expiry_type'] == future_expiry_offset)]
futures_data.reset_index(drop=True,inplace=True)
atm = futures_data[futures_data['datetime'] == entry_datetime]['open'].iloc[0]
atm = base*round(atm/base)
nearest_expiry = row['data_date'].date() + relativedelta(weekday=TH(+1))
ce_data = data[(data['instrument_type'] == 'CE') & (data['instrument_name'] == instrument) & ((data['expiry_date'] == nearest_expiry)|(data['expiry_date'] == nearest_expiry - datetime.timedelta(days=1))|(data['expiry_date'] == nearest_expiry - datetime.timedelta(days=2))) & (data['strike_price'] == atm)]
ce_data.reset_index(drop=True,inplace=True)
pe_data = data[(data['instrument_type'] == 'PE') & (data['instrument_name'] == instrument) & ((data['expiry_date'] == nearest_expiry)|(data['expiry_date'] == nearest_expiry - datetime.timedelta(days=1))|(data['expiry_date'] == nearest_expiry - datetime.timedelta(days=2))) & (data['strike_price'] == atm)]
pe_data.reset_index(drop=True,inplace=True)
ce_symbol = ce_data['ticker'].iloc[0]
pe_symbol = pe_data['ticker'].iloc[0]
futures_data = futures_data[['datetime','close']].set_index('datetime')
ce_data = ce_data[['datetime','close']].set_index('datetime')
pe_data = pe_data[['datetime','close']].set_index('datetime')
intraday_data = pd.concat([futures_data,ce_data,pe_data],axis=1)
intraday_data.columns = ['futures_close','ce_close','pe_close']
intraday_data = intraday_data.ffill()
#pd.set_option('display.max_rows',400)
intraday_data.reset_index(inplace=True)
traded_prices = intraday_data[intraday_data['datetime'] == entry_datetime]
futures_entry_price = traded_prices['futures_close'].iloc[0]
ce_entry_price = traded_prices['ce_close'].iloc[0]
pe_entry_price = traded_prices['pe_close'].iloc[0]
stop_loss_percentage = 20/100
ce_stop_loss = ce_entry_price + ce_entry_price*stop_loss_percentage
pe_stop_loss = pe_entry_price + pe_entry_price*stop_loss_percentage
entry_time_index = intraday_data[intraday_data['datetime'] == entry_datetime].index[0]
exit_time_index = intraday_data[intraday_data['datetime'] == exit_datetime].index[0]
intraday_data = intraday_data[entry_time_index:exit_time_index+1]
intraday_data['ce_pnl'] = 0
intraday_data['pe_pnl'] = 0
intraday_data.reset_index(drop=True,inplace=True)
ce_stop_loss_counter = 0
pe_stop_loss_counter = 0
ce_exit_datetime = ''
pe_exit_datetime = ''
ce_exit_price = 0
pe_exit_price = 0
ce_pnl = 0
pe_pnl = 0
pnl = 0
for index, row in intraday_data.iterrows():
ce_ltp = row['ce_close']
pe_ltp = row['pe_close']
#criterias for exit
#none of the stoplosses were hit and time limit is reached
if (ce_stop_loss_counter == 0) & (pe_stop_loss_counter == 0) & (row['datetime'] == exit_datetime):
ce_pnl = ce_entry_price - ce_ltp
pe_pnl = pe_entry_price - pe_ltp
ce_stop_loss_counter = 1
pe_stop_loss_counter = 1
ce_exit_datetime = row['datetime']
pe_exit_datetime = row['datetime']
ce_exit_price = ce_ltp
pe_exit_price = pe_ltp
intraday_data.loc[index,'ce_pnl'] = ce_pnl
intraday_data.loc[index,'pe_pnl'] = pe_pnl
print('none of the stoplosses were hit and time limit is reached')
pnl = ce_pnl + pe_pnl
break
#ce stop loss is hit and none were hit till now
elif (ce_ltp >= ce_stop_loss) & (ce_stop_loss_counter == 0) & (pe_stop_loss_counter == 0):
ce_pnl = ce_entry_price - ce_stop_loss
pe_pnl = pe_entry_price - pe_ltp
ce_stop_loss_counter = 1
ce_exit_datetime = row['datetime']
ce_exit_price = ce_stop_loss
intraday_data.loc[index,'ce_pnl'] = ce_pnl
intraday_data.loc[index,'pe_pnl'] = pe_pnl
print('ce stop loss is hit and none were hit till now')
pnl = ce_pnl + pe_pnl
#pe stop loss is hit and none were hit till now
elif (pe_ltp >= pe_stop_loss) & (ce_stop_loss_counter == 0) & (pe_stop_loss_counter == 0):
ce_pnl = ce_entry_price - ce_ltp
pe_pnl = pe_entry_price - pe_stop_loss
pe_stop_loss_counter = 1
pe_exit_datetime = row['datetime']
pe_exit_price = pe_stop_loss
intraday_data.loc[index,'ce_pnl'] = ce_pnl
intraday_data.loc[index,'pe_pnl'] = pe_pnl
print('pe stop loss is hit and none were hit till now')
pnl = ce_pnl + pe_pnl
#either ce or pe sl was hit and now other one is also hit
elif (ce_stop_loss_counter == 1) & (pe_stop_loss_counter == 0):
if (pe_ltp >= pe_stop_loss) & (row['datetime'] < exit_datetime):
pe_pnl = pe_entry_price - pe_stop_loss
pe_stop_loss_counter = 1
pe_exit_price = pe_stop_loss
pe_exit_datetime = row['datetime']
intraday_data.loc[index,'ce_pnl'] = ce_pnl
intraday_data.loc[index,'pe_pnl'] = pe_pnl
print('ce sl was hit and now other one is also hit')
pnl = ce_pnl + pe_pnl
break
elif (row['datetime'] == exit_datetime):
pe_pnl = pe_entry_price - pe_ltp
pe_stop_loss_counter = 1
pe_exit_price = pe_ltp
pe_exit_datetime = row['datetime']
intraday_data.loc[index,'ce_pnl'] = ce_pnl
intraday_data.loc[index,'pe_pnl'] = pe_pnl
print('pe sl was hit and now other exited based on exit time')
pnl = ce_pnl + pe_pnl
break
#either ce or pe sl was hit and now other one is also hit
elif (ce_stop_loss_counter == 0) & (pe_stop_loss_counter == 1):
if (ce_ltp >= ce_stop_loss) & (row['datetime'] < exit_datetime):
ce_pnl = ce_entry_price - ce_stop_loss
ce_stop_loss_counter = 1
ce_exit_price = ce_stop_loss
ce_exit_datetime = row['datetime']
intraday_data.loc[index,'ce_pnl'] = ce_pnl
intraday_data.loc[index,'pe_pnl'] = pe_pnl
print('pe sl was hit and now other one is also hit')
pnl = ce_pnl + pe_pnl
break
elif (row['datetime'] == exit_datetime):
ce_pnl = ce_entry_price - ce_ltp
ce_stop_loss_counter = 1
ce_exit_price = ce_ltp
ce_exit_datetime = row['datetime']
intraday_data.loc[index,'ce_pnl'] = ce_pnl
intraday_data.loc[index,'pe_pnl'] = pe_pnl
print('ce sl was hit and now other exited based on exit time')
pnl = ce_pnl + pe_pnl
break
#update the pnl during the normal course of the day
elif (((ce_stop_loss_counter == 0) & (pe_stop_loss_counter == 0)) | ((ce_stop_loss_counter == 1) & (pe_stop_loss_counter == 0)) | ((ce_stop_loss_counter == 0) & (pe_stop_loss_counter == 1)) | ((ce_stop_loss_counter == 1) & (pe_stop_loss_counter == 1))) or row['datetime'] <= exit_datetime:
ce_pnl = ce_entry_price - ce_ltp
pe_pnl = pe_entry_price - pe_ltp
intraday_data.loc[index,'ce_pnl'] = ce_pnl
intraday_data.loc[index,'pe_pnl'] = pe_pnl
pnl = ce_pnl + pe_pnl
intraday_trade_log = intraday_trade_log.append({'Entry_Datetime':entry_datetime,
'Future_Traded_Price':futures_entry_price,
'ATM':atm,
'Days_to_Expiry':(nearest_expiry - entry_datetime.date()).days,
'CE_Symbol':ce_symbol,
'CE_Entry_Price':ce_entry_price,
'CE_Exit_Price':ce_exit_price,
'CE_Exit_Datetime':ce_exit_datetime,
'PE_Symbol':pe_symbol,
'PE_Entry_Price':pe_entry_price,
'PE_Exit_Price':pe_exit_price,
'PE_Exit_Datetime':pe_exit_datetime,
'PnL':pnl},ignore_index=True)
except Exception as e:
print(e)
print(row['location'])
intraday_trade_log['PnL'].cumsum().plot()