Straddle and strangle Back testing With Python

For Short

import datetime

trade=0
Trade= pd.DataFrame(columns=['Trade_Date'])
for i in range(len(df_options)):
if (trade==0) & (df_options['Time'][i]>=datetime.datetime.strptime('10:00:00', '%H:%M:%S').time()) & (df_options['Time'][i]<=datetime.datetime.strptime('10:05:00', '%H:%M:%S').time()):
Entry_Price=df_options['Open'][i]
Entry_Time=df_options['Time'][i]
Entry_date=df_options['date'][i]
sl=Entry_Price*1.7
trade=1
continue
if (trade==1):
if (Entry_date==df_options['date'][i]) & (df_options['Time'][i]>=datetime.datetime.strptime('15:15:00', '%H:%M:%S').time()):
Exit_Price=df_options['Open'][i]
Exit_Time=df_options['Time'][i]
trade=0
Trade = Trade.append({'Trade_Date':df_options['Date'][i],
'Option_type':df_options['Option_type'][i],
'Strike_Price':df_options['Strike_Price'][i],
'Entry_Price':Entry_Price,
'Exit_Price':Exit_Price,
'Entry_Time':Entry_Time,
'Exit_Time':Exit_Time,
'sl':sl,
'sale_type':'sell_option',
'Exit_Type':'day_exit',
"pnl":(Entry_Price-Exit_Price)},
ignore_index=True)
continue
elif (Entry_date==df_options['date'][i]) & (df_options['Time'][i]<datetime.datetime.strptime('15:15:00', '%H:%M:%S').time()) & (sl<=df_options['High'][i]):
Exit_Price=sl
Exit_Time=df_options['Time'][i]
trade=0
Trade = Trade.append({'Trade_Date':df_options['Date'][i],
'Option_type':df_options['Option_type'][i],
'Strike_Price':df_options['Strike_Price'][i],
'Entry_Price':Entry_Price,
'Exit_Price':Exit_Price,
'Entry_Time':Entry_Time,
'Exit_Time':Exit_Time,
'sl':sl,
'sale_type':'sell_option',
'Exit_Type':'sl_exit',
"pnl":(Entry_Price-Exit_Price)},
ignore_index=True)
continue
elif (trade==1) & (Entry_date!=df_options['date'][i]):
trade=0
continue

for long at the same exit Time of short and Out of The money

def trading_combined_target_price(df_straddle,sl_percentage=1.7,Target_percentage=0.7):
df_options = df_straddle.copy()
trade=0
Trade= pd.DataFrame(columns=['Trade_Date'])
for i in range(len(df_options)):
if (trade==0) & (df_options['Time'][i]>=datetime.datetime.strptime('09:30:00', '%H:%M:%S').time()) & (df_options['Time'][i]<=datetime.datetime.strptime('09:35:00', '%H:%M:%S').time()):
Entry_Price=df_options['Open'][i]
Entry_Time=df_options['Time'][i]
Entry_date=df_options['date'][i]
sl=Entry_Price*sl_percentage
Target_Price = Entry_Price*Target_percentage
trade=1
continue
if (trade==1):
if (Entry_date==df_options['date'][i]) & (df_options['Time'][i]>=datetime.datetime.strptime('15:15:00', '%H:%M:%S').time()):
Exit_Price=df_options['Open'][i]
Exit_Time=df_options['Time'][i]
trade=0
Trade = Trade.append({'Trade_Date':df_options['Date'][i],
'Strike_Price':df_options['Strike_Price'][i],
'Entry_Price':Entry_Price,
'Exit_Price':Exit_Price,
'Entry_Time':Entry_Time,
'Exit_Time':Exit_Time,
'sl':sl,
'Target_Price':Target_Price,
'sale_type':'sell_option',
'Exit_Type':'day_exit',
"pnl":(Entry_Price-Exit_Price)},
ignore_index=True)
continue
elif (Entry_date==df_options['date'][i]) & (df_options['Time'][i]<datetime.datetime.strptime('15:15:00', '%H:%M:%S').time()) & (sl<=df_options['High'][i]):
Exit_Price=sl
Exit_Time=df_options['Time'][i]
trade=0
Trade = Trade.append({'Trade_Date':df_options['Date'][i],
'Strike_Price':df_options['Strike_Price'][i],
'Entry_Price':Entry_Price,
'Exit_Price':Exit_Price,
'Entry_Time':Entry_Time,
'Exit_Time':Exit_Time,
'sl':sl,
'Target_Price':Target_Price,
'sale_type':'sell_option',
'Exit_Type':'sl_exit',
"pnl":(Entry_Price-Exit_Price)},
ignore_index=True)
continue
elif (Entry_date==df_options['date'][i]) & (df_options['Time'][i]<datetime.datetime.strptime('15:15:00', '%H:%M:%S').time()) & (Target_Price>=df_options['Low'][i]):
Exit_Price=Target_Price
Exit_Time=df_options['Time'][i]
trade=0
Trade = Trade.append({'Trade_Date':df_options['Date'][i],
'Strike_Price':df_options['Strike_Price'][i],
'Entry_Price':Entry_Price,
'Exit_Price':Exit_Price,
'Entry_Time':Entry_Time,
'Exit_Time':Exit_Time,
'sl':sl,
'Target_Price':Target_Price,
'sale_type':'sell_option',
'Exit_Type':'Target_price_exit',
"pnl":(Entry_Price-Exit_Price)},
ignore_index=True)
continue
elif (trade==1) & (Entry_date!=df_options['date'][i]):
trade=0
continue
return Trade

for complete code follow my git link

for more Feature updates follow me

Thank you for visiting my blog.

Linkdin-

I'm a Data Science enthusiast who loves to play with machine learning and deep learning.