#buy take profit levels and sell stop loss levels
data['btp1']= low+(rangeX*tplev1)
data['btp2']= low+(rangeX*tplev2)
data['btp3']= low+(rangeX*tplev3)
data['btp4']= low+(rangeX*tplev4)
data['btp5']= low+(rangeX*tplev5)
#buy stop loss levels
#data['sl1']=high+(range*sllev1)
#data['sl2']=high+(range*sllev2)
#data['sl3']=high+(range*sllev3)
#data['sl4']=high+(range*sllev4)
#data['sl5']=high+(range*sllev5)
#sell take profit and buy stop loss levels
data['stp1']=high-(rangeX*tplev1)
data['stp2']=high-(rangeX*tplev2)
data['stp3']=high-(rangeX*tplev3)
data['stp4']=high-(rangeX*tplev4)
data['stp5']=high-(rangeX*tplev5)
#if high-low >1*atr and close > open then buy
#if high-low >1*atr and close < open then sell
condition_buy = (rangeX>data['atr25']/2) & (data['PX_CLOSE_1D'] > data['PX_OPEN'])
data['Action'] = ''
data.loc[condition_buy,'Action'] = 'buy'
condition_sell = (rangeX>data['atr25']/2) & (data['PX_CLOSE_1D'] < data['PX_OPEN'])
data.loc[condition_sell,'Action'] = 'sell'
data
#if above true then on the following day buy 1 unit at lev5, 1 unit at lev4, one unit at lev3
#stoploss=sell 1 unit at
data.columns
print(data)
#### step 1: check execution buy and execution sell of each level (total 10 columns)
data[['Check_Execution1_buy','Check_Execution2_buy','Check_Execution3_buy','Check_Execution4_buy','Check_Execution5_buy','Check_Execution1_sell','Check_Execution2_sell','Check_Execution3_sell','Check_Execution4_sell','Check_Execution5_sell']] = ''
for i in range(0,len(data)-1):
if data.loc[i,'Action'] == 'sell':
# checking overlapping between all entry level and PX_LOW
if data.loc[i,'lev5'] < data.loc[i+1,'PX_HIGH']:
data.loc[i+1,'Check_Execution5_sell'] = 'executed'
if data.loc[i,'lev4'] < data.loc[i+1,'PX_HIGH']:
data.loc[i+1,'Check_Execution4_sell'] = 'executed'
if data.loc[i,'lev3'] < data.loc[i+1,'PX_HIGH']:
data.loc[i+1,'Check_Execution3_sell'] = 'executed'
if data.loc[i,'lev2'] < data.loc[i+1,'PX_HIGH']:
data.loc[i+1,'Check_Execution2_sell'] = 'executed'
if data.loc[i,'lev1'] < data.loc[i+1,'PX_HIGH']:
data.loc[i+1,'Check_Execution1_sell'] = 'executed'
elif data.loc[i,'Action'] == 'buy':
# checking overlapping between all entry level and PX_HIGH
if data.loc[i,'lev5'] > data.loc[i+1,'PX_LOW']:
data.loc[i+1,'Check_Execution5_buy'] = 'executed'
if data.loc[i,'lev4'] > data.loc[i+1,'PX_LOW']:
data.loc[i+1,'Check_Execution4_buy'] = 'executed'
if data.loc[i,'lev3'] > data.loc[i+1,'PX_LOW']:
data.loc[i+1,'Check_Execution3_buy'] = 'executed'
if data.loc[i,'lev2'] > data.loc[i+1,'PX_LOW']:
data.loc[i+1,'Check_Execution2_buy'] = 'executed'
if data.loc[i,'lev1'] > data.loc[i+1,'PX_LOW']:
data.loc[i+1,'Check_Execution1_buy'] = 'executed'
data
data.columns
### step2 : check if stop loss [sell and buy] executed (5 columns)
import numpy as np
data[['stop_loss_sell1','stop_loss_sell2','stop_loss_sell3','stop_loss_sell4','stop_loss_sell5', 'stop_profit_sell1','stop_profit_sell2','stop_profit_sell3','stop_profit_sell4','stop_profit_sell5']] = ''
for i in range(0,len(data)-1):
if data.loc[i,'Action'] == 'sell':
# checking overlapping between all stop loss level and PX_HIGH
if (data.loc[i,'btp1'] < data.loc[i+1,'PX_HIGH']) & (data.loc[i+1,'Check_Execution1_sell'] == 'executed'):
data.loc[i+1,'stop_loss_sell1'] = 'closed'
if (data.loc[i,'btp2'] < data.loc[i+1,'PX_HIGH']) & (data.loc[i+1,'Check_Execution2_sell'] == 'executed'):
data.loc[i+1,'stop_loss_sell2'] = 'closed'
if (data.loc[i,'btp3'] < data.loc[i+1,'PX_HIGH']) & (data.loc[i+1,'Check_Execution3_sell'] == 'executed'):
data.loc[i+1,'stop_loss_sell3'] = 'closed'
if (data.loc[i,'btp4'] < data.loc[i+1,'PX_HIGH']) & (data.loc[i+1,'Check_Execution4_sell'] == 'executed'):
data.loc[i+1,'stop_loss_sell4'] = 'closed'
if (data.loc[i,'btp5'] < data.loc[i+1,'PX_HIGH']) & (data.loc[i+1,'Check_Execution5_sell'] == 'executed'):
data.loc[i+1,'stop_loss_sell5'] = 'closed'
# checking overlapping between all stop profit level and PX_LOW
if (data.loc[i,'stp1'] > data.loc[i+1,'PX_LOW']) & (data.loc[i+1,'Check_Execution1_sell'] == 'executed'):
data.loc[i+1,'stop_profit_sell1'] = 'profit'
if (data.loc[i,'stp2'] > data.loc[i+1,'PX_LOW']) & (data.loc[i+1,'Check_Execution2_sell'] == 'executed'):
data.loc[i+1,'stop_profit_sell2'] = 'profit'
if (data.loc[i,'stp3'] > data.loc[i+1,'PX_LOW']) & (data.loc[i+1,'Check_Execution3_sell'] == 'executed'):
data.loc[i+1,'stop_profit_sell3'] = 'profit'
if (data.loc[i,'stp4'] > data.loc[i+1,'PX_LOW']) & (data.loc[i+1,'Check_Execution4_sell'] == 'executed'):
data.loc[i+1,'stop_profit_sell4'] = 'profit'
if (data.loc[i,'stp5'] > data.loc[i+1,'PX_LOW']) & (data.loc[i+1,'Check_Execution5_sell'] == 'executed'):
data.loc[i+1,'stop_profit_sell5'] = 'profit'
elif data.loc[i,'Action'] == 'buy':
if (data.loc[i,'stp1'] > data.loc[i+1,'PX_LOW']) & (data.loc[i+1,'Check_Execution1_buy'] == 'executed'):
data.loc[i+1,'stop_loss_buy1'] = 'loss'
if (data.loc[i,'stp2'] > data.loc[i+1,'PX_LOW']) & (data.loc[i+1,'Check_Execution2_buy'] == 'executed'):
data.loc[i+1,'stop_loss_buy2'] = 'loss'
if (data.loc[i,'stp3'] > data.loc[i+1,'PX_LOW']) & (data.loc[i+1,'Check_Execution3_buy'] == 'executed'):
data.loc[i+1,'stop_loss_buy3'] = 'loss'
if (data.loc[i,'stp4'] > data.loc[i+1,'PX_LOW']) & (data.loc[i+1,'Check_Execution4_buy'] == 'executed'):
data.loc[i+1,'stop_loss_buy4'] = 'loss'
if (data.loc[i,'stp5'] > data.loc[i+1,'PX_LOW']) & (data.loc[i+1,'Check_Execution5_buy'] == 'executed'):
data.loc[i+1,'stop_loss_buy5'] = 'loss'
if (data.loc[i,'btp1'] < data.loc[i+1,'PX_HIGH']) & (data.loc[i+1,'Check_Execution1_buy'] == 'executed'):
data.loc[i+1,'stop_profit_buy1'] = 'profit'
if (data.loc[i,'btp2'] < data.loc[i+1,'PX_HIGH']) & (data.loc[i+1,'Check_Execution2_buy'] == 'executed'):
data.loc[i+1,'stop_profit_buy2'] = 'profit'
if (data.loc[i,'btp3'] < data.loc[i+1,'PX_HIGH']) & (data.loc[i+1,'Check_Execution3_buy'] == 'executed'):
data.loc[i+1,'stop_profit_buy3'] = 'profit'
if (data.loc[i,'btp4'] < data.loc[i+1,'PX_HIGH']) & (data.loc[i+1,'Check_Execution4_buy'] == 'executed'):
data.loc[i+1,'stop_profit_buy4'] = 'profit'
if (data.loc[i,'btp5'] < data.loc[i+1,'PX_HIGH']) & (data.loc[i+1,'Check_Execution5_buy'] == 'executed'):
data.loc[i+1,'stop_profit_buy5'] = 'profit'
data