Frequently Asked Questions

import pandas as pd
data= pd.read_csv("fibo.csv")
data.head() # pivot
import numpy as  np

rangeX =data['PX_HIGH']-data['PX_LOW']
data['range']= rangeX.copy()
data['rangeh']=abs(data['PX_HIGH']-data['PX_CLOSE_1D'].shift(1) )
data['rangel']=abs(data['PX_LOW']-data['PX_CLOSE_1D'].shift(1)  )

mat=data[['range','rangeh','rangel']]
data['tr'] = mat.max(axis=1)
data

data['atr25'] = data['tr'].rolling(25).mean()
data
high= data['PX_HIGH']

ret1=0.764
ret2=0.618
ret3=0.5
ret4=0.382
ret5=0.236
low= data['PX_LOW']
####################################################
tplev1 =1.236
tplev2 = 1.382
tplev3 = 1.50
tplev4=1.618
tplev5=1.7640
#########################################################
sllev1 = -1.236
sllev2 = -1.382
sllev3 = -1.50
sllev4 = -1.618
sllev5 = -1.7640
#entry levels
data['lev5']=high-(rangeX*ret1)
data['lev4']=high-(rangeX*ret2)
data['lev3']=high-(rangeX*ret3)
data['lev2']=high-(rangeX*ret4)
data['lev1']=high-(rangeX*ret5)
#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)
#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)
data.head(5)
#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
        
        
### calculate level wise loss and profit
# to be to continued
data.to_csv('out4.csv',index=None)