-
Notifications
You must be signed in to change notification settings - Fork 1
/
Indicators.py
61 lines (51 loc) · 1.51 KB
/
Indicators.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
60
61
from math import atan
import numpy as np
import pandas as pd
def ATR(DF,n):
"function to calculate True Range and Average True Range"
df = DF.copy()
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,skipna=False)
df['ATR'] = df['TR'].rolling(n).mean()
#df['ATR'] = df['TR'].ewm(span=n,adjust=False,min_periods=n).mean()
df2 = df.drop(['H-L','H-PC','L-PC'],axis=1)
return df2
def VWAP(ohlcv):
df = ohlcv.copy()
df['cumVol'] = df['vol'].cumsum()
df['cum_ohc3Vol'] = (((df[['open','high','close']].sum(axis=1))/3)*df['vol']).cumsum()
df['VWAP'] = df['cum_ohc3Vol']/df['cumVol']
df.drop(df.columns[[5,6]],axis=1,inplace=True)
return df
def slope(df,col,n):
"""
n = bars back
"""
df = df.copy()
ser = df[col]
slp = []
for i, x in enumerate(ser):
if i < n:
slp.append(0)
continue
#getting values to find the slope
b = ser[i]
a = ser[i-n]
slope = atan((b-a)/n)
slp.append(slope)
df[f'{col}_Slope'] = pd.Series(slp,index=df.index)
return df
def vwap_slope(ohlcv_data):
df = ohlcv_data.copy()
df = VWAP(df)
df = slope(df,'VWAP',3)
return df
def o_c(ohlcv,n=33):
df = ohlcv.copy()
o = df['open'].rolling(window=n).mean()
c = df['close'].rolling(window=n).mean()
df['o_c'] = c-o
df.dropna(inplace=True)
return df