Donchian Channel(DC) and Super Trend back testing with python(Part-1)

Donchian Channel(DC) and Super Trend

About DC — https://en.wikipedia.org/wiki/Donchian_channel

About Super Trend — https://investobull.com/what-is-super-trend-indicator-and-how-to-use-it/

Here I am going to explain DC-super trend back testing with python.

Donchian Channel(DC) formula:

upper channel = highest high of last N-periods

lower channel = lowest low of last N-periods

middle channel =( (upper channel + lower channel)/2)

python code:

df['upper_channel'] = df['High'].rolling(N).max().shift(N-20)
df['lower_channel'] = df['Low'].rolling(N).min().shift(N-20)
df['middle_channel'] = ((df['upper_channel']+df['lower_channel'])/2)

super trend python code:

#Calculation of SuperTrend
df['Upper Basic']=(df['High']+df['Low'])/2+(f*df['atr'])
df['Lower Basic']=(df['High']+df['Low'])/2-(f*df['atr'])
df['Upper Band']=df['Upper Basic']
df['Lower Band']=df['Lower Basic']
for i in range(n,len(df)):
if df['Close'][i-1]<=df['Upper Band'][i-1]:
df['Upper Band'][i]=min(df['Upper Basic'][i],df['Upper Band'][i-1])
else:
df['Upper Band'][i]=df['Upper Basic'][i]
for i in range(n,len(df)):
if df['Close'][i-1]>=df['Lower Band'][i-1]:
df['Lower Band'][i]=max(df['Lower Basic'][i],df['Lower Band'][i-1])
else:
df['Lower Band'][i]=df['Lower Basic'][i]
df['SuperTrend']=np.nan
for i in df['SuperTrend']:
if df['Close'][n-1]<=df['Upper Band'][n-1]:
df['SuperTrend'][n-1]=df['Upper Band'][n-1]
elif df['Close'][n-1]>df['Upper Band'][i]:
df['SuperTrend'][n-1]=df['Lower Band'][n-1]
for i in range(n,len(df)):
if df['SuperTrend'][i-1]==df['Upper Band'][i-1] and df['Close'][i]<=df['Upper Band'][i]:
df['SuperTrend'][i]=df['Upper Band'][i]
elif df['SuperTrend'][i-1]==df['Upper Band'][i-1] and df['Close'][i]>=df['Upper Band'][i]:
df['SuperTrend'][i]=df['Lower Band'][i]
elif df['SuperTrend'][i-1]==df['Lower Band'][i-1] and df['Close'][i]>=df['Lower Band'][i]:
df['SuperTrend'][i]=df['Lower Band'][i]
elif df['SuperTrend'][i-1]==df['Lower Band'][i-1] and df['Close'][i]<=df['Lower Band'][i]:
df['SuperTrend'][i]=df['Upper Band'][i]

buy long trade : upper channel <= high of the candle : buy at=upper channel
stop loss long : supertrend >= low low of the candle : sell at = supertrend

sold short : lower channel >= low of the candle : sell at = lowerchannel
stoploss short: supertrend <= high of the candle: buy at = supertrend

Tuning parameters:

timeframe = 5minutes, 10 minutes, 15 minutes, etc…

factor(f) = 1.5 or 3

ATR period = 7 or 14 or 18

N — value = 20, 50, 100, …

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