This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
stanford_charles.py
186 lines (156 loc) · 6.02 KB
/
stanford_charles.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import numpy as np
def wwma(values, n):
"""
J. Welles Wilder's EMA
"""
return values.ewm(alpha=2/(n+1), min_periods=n, adjust=False).mean()
def ema(values, n):
"""
EMA
"""
return values.ewm(alpha=1/n, min_periods=n, adjust=False).mean()
def atr(df, n=14):
data = df.copy()
high = data['High']
low = data['Low']
close = data['Close']
data['tr0'] = abs(high - low)
data['tr1'] = abs(high - close.shift())
data['tr2'] = abs(low - close.shift())
tr = data[['tr0', 'tr1', 'tr2']].max(axis=1)
atr = wwma(tr, n)
return round(atr, 2)
def adx(df, n=14):
data = df.copy()
data['atr'] = atr(df, n)
data['Up Move'] = np.nan
data['Down Move'] = np.nan
data['PDM'] = np.nan
data['NDM'] = np.nan
data['PDI'] = np.nan
data['EMAP'] = np.nan
data['NDI'] = np.nan
data['EMAN'] = np.nan
data['PMINUSN'] = np.nan
data['PPLUSN'] = np.nan
data['EMAPMINUSN'] = np.nan
data['ADX'] = np.nan
# find +di and -di
for x in range(1, len(data)):
data['Up Move'][x] = 0
data['Down Move'][x] = 0
data['Up Move'][x] = data['High'][x] - data['High'][x-1]
data['Down Move'][x] = data['Low'][x-1] - data['Low'][x]
if data['Up Move'][x] > 0 and data['Up Move'][x] > data['Down Move'][x]:
data['PDM'][x] = data['Up Move'][x]
else:
data['PDM'][x] = 0
if data['Down Move'][x] > 0 and data['Down Move'][x] > data['Up Move'][x]:
data['NDM'][x] = data['Down Move'][x]
else:
data['NDM'][x] = 0
data['EMAP'] = ema(data['PDM'], n)
data['EMAN'] = ema(data['NDM'], n)
data['PDI'] = (data['EMAP'] / data['atr'])
data['NDI'] = (data['EMAN'] / data['atr'])
data['PMINUSN'] = abs(data['PDI'] - data['NDI'])
data['PPLUSN'] = abs(data['PDI'] + data['NDI'])
dx = ((data['PMINUSN'])/(data['PPLUSN'])) * 100
data['ADX'] = ema(dx, n)
return round(data['ADX'], 2)
def day_low(df, days=5):
low = 9223372036854775807
data = df.tail(days)
for x in range(0, len(data)):
if (data['Low'][x] < low):
low = data['Low'][x]
return round(low, 3)
def day_high(df, days=5):
high = -1
data = df.tail(days)
for x in range(0, len(data)):
if (data['Close'][x] > high):
high = data['Close'][x]
return round(high, 3)
def moving_average(df, col, n=200):
data = df[col].rolling(n, center=False).mean()
return round(data[len(df) - 1], 2)
def average_volume(df):
average = moving_average(df, 'Volume', 57)
return average
def relative_volume(df):
data = df.copy()
average = average_volume(data)
rel_average = data['Volume'][len(df)-1] / average
return round(rel_average, 2)
def rsi14(df):
#14_Day RSI
data = df.copy()
data['Up Move'] = np.nan
data['Down Move'] = np.nan
data['Average Up'] = np.nan
data['Average Down'] = np.nan
#Relative Strength
data['RS'] = np.nan
#Relative Strength Index
data['RSI'] = np.nan
#Calculate 'Up Move' & 'Down Move'
for x in range(0, len(data)):
data['Up Move'][x] = 0
data['Down Move'][x] = 0
if data['Adj Close'][x] > data['Adj Close'][x-1]:
data['Up Move'][x] = data['Adj Close'][x] - data['Adj Close'][x-1]
if data['Adj Close'][x] < data['Adj Close'][x-1]:
data['Down Move'][x] = abs(data['Adj Close'][x] - data['Adj Close'][x-1])
## Calculate initial Average Up & Down, RS and RSI
data['Average Up'][14] = data['Up Move'][1:15].mean()
data['Average Down'][14] = data['Down Move'][1:15].mean()
data['RS'][14] = data['Average Up'][14] / data['Average Down'][14]
data['RSI'][14] = 100 - (100/(1+data['RS'][14]))
## Calculate rest of Average Up, Average Down, RS, RSI
for x in range(15, len(data)):
data['Average Up'][x] = (data['Average Up'][x-1]*13+data['Up Move'][x])/14
data['Average Down'][x] = (data['Average Down'][x-1]*13+data['Down Move'][x])/14
data['RS'][x] = data['Average Up'][x] / data['Average Down'][x]
data['RSI'][x] = 100 - (100/(1+data['RS'][x]))
return round(data['RSI'], 2)
def rsi2(df):
#2_Day RSI
data = df.copy()
data['Up Move'] = np.nan
data['Down Move'] = np.nan
data['Average Up'] = np.nan
data['Average Down'] = np.nan
#Relative Strength
data['RS'] = np.nan
#Relative Strength Index
data['RSI'] = np.nan
#Calculate 'Up Move' & 'Down Move'
for x in range(0, len(data)):
data['Up Move'][x] = 0
data['Down Move'][x] = 0
if data['Adj Close'][x] > data['Adj Close'][x-1]:
data['Up Move'][x] = data['Adj Close'][x] - data['Adj Close'][x-1]
if data['Adj Close'][x] < data['Adj Close'][x-1]:
data['Down Move'][x] = abs(data['Adj Close'][x] - data['Adj Close'][x-1])
## Calculate initial Average Up & Down, RS and RSI
data['Average Up'][2] = data['Up Move'][1:3].mean()
data['Average Down'][2] = data['Down Move'][1:3].mean()
data['RS'][2] = data['Average Up'][2] / data['Average Down'][2]
data['RSI'][2] = 100 - (100/(1+data['RS'][2]))
## Calculate rest of Average Up, Average Down, RS, RSI
for x in range(3, len(data)):
data['Average Up'][x] = (data['Average Up'][x-1]*1+data['Up Move'][x])/2
data['Average Down'][x] = (data['Average Down'][x-1]*1+data['Down Move'][x])/2
data['RS'][x] = data['Average Up'][x] / data['Average Down'][x]
data['RSI'][x] = 100 - (100/(1+data['RS'][x]))
return round(data['RSI'], 2)
def rsi_pctRank(df, period=14, days=63):
data = df.copy()
count = 0
data = data.tail(days)
for x in range(0, days):
if (data['RSI'+str(period)][x] < data['RSI'+str(period)][days - 1]):
count += 1
pctRank = count / days
return round(pctRank, 2)