-
Notifications
You must be signed in to change notification settings - Fork 1
/
supertrend.py
59 lines (54 loc) · 2.31 KB
/
supertrend.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
'''
@author: techietrader
'''
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#SuperTrend
def ST(df,f,n): #df is the dataframe, n is the period, f is the factor; f=3, n=7 are commonly used.
#Calculation of ATR
df['H-L']=abs(df['High']-df['Low'])
df['H-PC']=abs(df['High']-df['Close'].shift(1))
df['L-PC']=abs(df['Low']-df['Close'].shift(1))
df['TR']=df[['H-L','H-PC','L-PC']].max(axis=1)
df['ATR']=np.nan
df['ATR'].iloc[n-1]=df['TR'][:n-1].mean()
for i in range(n,len(df)):
df['ATR'][i]=(df['ATR'][i-1]*(n-1)+ df['TR'][i])/n
#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]
df['Signal'] = df.apply(signal, axis=1)
return df
def signal(c):
if c['Close'] > c['SuperTrend']:
return 'Buy'
else:
return 'Sell'