-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
overlap_studies.py
364 lines (338 loc) · 10.9 KB
/
overlap_studies.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
""""""
# Import Built-Ins:
# Import Third-Party:
# Import Homebrew:
import jhtalib as jhta
def BBANDS(df, n, f=2, high='High', low='Low', close='Close'):
"""
Bollinger Bands
Returns: dict of lists of floats = jhta.BBANDS(df, n, f=2, high='High', low='Low', close='Close')
Source: https://www.fmlabs.com/reference/default.htm?url=Bollinger.htm
"""
bbands_dict = {'midband': [], 'upperband': [], 'lowerband': []}
tp_dict = {'tp': jhta.TYPPRICE(df, high, low, close)}
sma_list = jhta.SMA(tp_dict, n, 'tp')
stdev_list = jhta.STDEV(tp_dict, n, 'tp')
for i in range(len(df[close])):
if i + 1 < n:
midband = float('NaN')
upperband = float('NaN')
lowerband = float('NaN')
else:
midband = sma_list[i]
upperband = midband + f * stdev_list[i]
lowerband = midband - f * stdev_list[i]
bbands_dict['midband'].append(midband)
bbands_dict['upperband'].append(upperband)
bbands_dict['lowerband'].append(lowerband)
return bbands_dict
def BBANDW(df, n, f=2, high='High', low='Low', close='Close'):
"""
Bollinger Band Width
Returns: list of floats = jhta.BBANDW(df, n, f=2, high='High', low='Low', close='Close')
Source: https://www.fmlabs.com/reference/default.htm?url=BollingerWidth.htm
"""
bbandw_list = []
tp_dict = {'tp': jhta.TYPPRICE(df, high, low, close)}
stdev_list = jhta.STDEV(tp_dict, n, 'tp')
for i in range(len(df[close])):
if i + 1 < n:
bbandw = float('NaN')
else:
bbandw = 2 * f * stdev_list[i]
bbandw_list.append(bbandw)
return bbandw_list
def DEMA(df, n):
"""
Double Exponential Moving Average
"""
def EMA(df, n, price='Close'):
"""
Exponential Moving Average
Returns: list of floats = jhta.EMA(df, n, price='Close')
Source: https://www.fmlabs.com/reference/default.htm?url=ExpMA.htm
"""
ema_list = []
for i in range(len(df[price])):
if i + 1 < n:
ema = float('NaN')
else:
if ema != ema:
ema = df[price][i]
k = 2 / (n + 1)
ema = k * df[price][i] + (1 - k) * ema
ema_list.append(ema)
return ema_list
def ENVP(df, pct=.01, price='Close'):
"""
Envelope Percent
Returns: dict of lists of floats = jhta.ENVP(df, pct=.01, price='Close')
Source: https://www.fmlabs.com/reference/default.htm?url=EnvelopePct.htm
"""
envp_dict = {'hi': [], 'lo': []}
for i in range(len(df[price])):
hi = df[price][i] + df[price][i] * pct
lo = df[price][i] - df[price][i] * pct
envp_dict['hi'].append(hi)
envp_dict['lo'].append(lo)
return envp_dict
def KAMA(df, n):
"""
Kaufman Adaptive Moving Average
"""
def MA(df, n):
"""
Moving average
"""
def MAMA(df, price='Close'):
"""
MESA Adaptive Moving Average
"""
def MAVP(df, price='Close'):
"""
Moving average with variable period
"""
def MIDPOINT(df, n, price='Close'):
"""
MidPoint over period
Returns: list of floats = jhta.MIDPOINT(df, n, price='Close')
Source: http://www.tadoc.org/indicator/MIDPOINT.htm
"""
midpoint_list = []
for i in range(len(df[price])):
if i + 1 < n:
midpoint = float('NaN')
else:
start = i + 1 - n
end = i + 1
midpoint = (max(df[price][start:end]) + min(df[price][start:end])) / 2
midpoint_list.append(midpoint)
return midpoint_list
def MIDPRICE(df, n, high='High', low='Low'):
"""
Midpoint Price over period
Returns: list of floats = jhta.MIDPRICE(df, n, high='High', low='Low')
Source: http://www.tadoc.org/indicator/MIDPRICE.htm
"""
midprice_list = []
for i in range(len(df[low])):
if i + 1 < n:
midprice = float('NaN')
else:
start = i + 1 - n
end = i + 1
midprice = (max(df[high][start:end]) + min(df[low][start:end])) / 2
midprice_list.append(midprice)
return midprice_list
def MMR(df, n=200, price='Close'):
"""
Mayer Multiple Ratio
Returns: list of floats = jhta.MMR(df, n=200, price='Close')
Source: https://www.theinvestorspodcast.com/bitcoin-mayer-multiple/
"""
mmr_list = []
sma_list = jhta.SMA(df, n, price)
for i in range(len(df[price])):
if i + 1 < n:
mmr = float('NaN')
else:
mmr = df[price][i] / sma_list[i]
mmr_list.append(mmr)
return mmr_list
def SAR(df, af_step=.02, af_max=.2, high='High', low='Low'):
"""
Parabolic SAR (J. Welles Wilder)
Returns: list of floats = jhta.SAR(df, af_step=.02, af_max=.2, high='High', low='Low')
Source: book: New Concepts in Technical Trading Systems
"""
sar_list = []
for i in range(len(df[low])):
if i < 1:
sar = float('NaN')
sar_list.append(sar)
is_long = True
sar = df[low][i]
ep = df[high][i]
af = af_step
else:
if is_long:
if df[low][i] <= sar:
is_long = False
sar = ep
if sar < df[high][i - 1]:
sar = df[high][i - 1]
if sar < df[high][i]:
sar = df[high][i]
sar_list.append(sar)
af = af_step
ep = df[low][i]
sar = sar + af * (ep - sar)
# sar = round(sar)
if sar < df[high][i - 1]:
sar = df[high][i - 1]
if sar < df[high][i]:
sar = df[high][i]
else:
sar_list.append(sar)
if df[high][i] > ep:
ep = df[high][i]
af += af_step
if af > af_max:
af = af_max
sar = sar + af * (ep - sar)
# sar = round(sar)
if sar > df[low][i - 1]:
sar = df[low][i - 1]
if sar > df[low][i]:
sar = df[low][i]
else:
if df[high][i] >= sar:
is_long = True
sar = ep
if sar > df[low][i - 1]:
sar = df[low][i - 1]
if sar > df[low][i]:
sar = df[low][i]
sar_list.append(sar)
af = af_step
ep = df[high][i]
sar = sar + af * (ep - sar)
# sar = round(sar)
if sar > df[low][i - 1]:
sar = df[low][i - 1]
if sar > df[low][i]:
sar = df[low][i]
else:
sar_list.append(sar)
if df[low][i] < ep:
ep = df[low][i]
af += af_step
if af > af_max:
af = af_max
sar = sar + af * (ep - sar)
# sar = round(sar)
if sar < df[high][i - 1]:
sar = df[high][i - 1]
if sar < df[high][i]:
sar = df[high][i]
return sar_list
def SAREXT(df):
"""
Parabolic SAR - Extended
"""
def SMA(df, n, price='Close'):
"""
Simple Moving Average
Returns: list of floats = jhta.SMA(df, n, price='Close')
Source: https://www.fmlabs.com/reference/default.htm?url=SimpleMA.htm
"""
sma_list = []
for i in range(len(df[price])):
if i + 1 < n:
sma = float('NaN')
else:
start = i + 1 - n
end = i + 1
sma = sum(df[price][start:end]) / n
sma_list.append(sma)
return sma_list
def T3(df, n, price='Close'):
"""
Triple Exponential Moving Average (T3)
"""
def TEMA(df, n, price='Close'):
"""
Triple Exponential Moving Average
"""
def TRIMA(df, n, price='Close'):
"""
Triangular Moving Average
Returns: list of floats = jhta.TRIMA(df, n, price='Close')
Source: https://www.fmlabs.com/reference/default.htm?url=TriangularMA.htm
"""
tma_list = []
sma_list = []
for i in range(len(df[price])):
if n % 2 == 0:
n_sma = n / 2 + 1
start = i + 1 - n_sma
end = i + 1
sma = sum(df[price][start:end]) / n_sma
sma_list.append(sma)
n_tma = n / 2
start = i + 1 - n_tma
end = i + 1
else:
n_sma = (n + 1) / 2
start = i + 1 - n_sma
end = i + 1
sma = sum(df[price][start:end]) / n_sma
sma_list.append(sma)
n_tma = (n + 1) / 2
start = i + 1 - n_tma
end = i + 1
if i + 1 < n:
tma = float('NaN')
else:
tma = sum(sma_list[start:end]) / n_tma
tma_list.append(tma)
return tma_list
def VAMA(df, n, price='Close', volume='Volume'):
"""
Volume Adjusted Moving Average
Returns: list of floats = jhta.VAMA(df, n, price='Close', volume='Volume')
Source: https://www.fmlabs.com/reference/default.htm?url=VolAdjustedMA.htm
"""
vama_list = []
pv_list = []
for i in range(len(df[price])):
if i + 1 < n:
vama = float('NaN')
pv = float('NaN')
pv_list.append(pv)
else:
start = i + 1 - n
end = i + 1
pv = df[price][i] * df[volume][i]
pv_list.append(pv)
vama = sum(pv_list[start:end]) / sum(df[volume][start:end])
vama_list.append(vama)
return vama_list
def WMA(df, n, price='Close'):
"""
Weighted Moving Average
"""
def WWMA(df, n, price='Close'):
"""
Welles Wilder Moving Average
Returns: list of floats = jhta.WWMA(df, n, price='Close')
Source: https://www.fmlabs.com/reference/default.htm?url=WellesMA.htm
"""
wwma_list = []
for i in range(len(df[price])):
if i + 1 < n:
wwma = float('NaN')
wwma_list.append(wwma)
wwma = df[price][i]
else:
wwma = (wwma * (n - 1) + df[price][i]) / n
wwma_list.append(wwma)
return wwma_list
def WWS(df, n, price='Close'):
"""
Welles Wilder Summation
Returns: list of floats = jhta.WWS(df, n, price='Close')
Source: https://www.fmlabs.com/reference/default.htm?url=WellesSum.htm
"""
wws_list = []
for i in range(len(df[price])):
if i + 1 < n:
wws = float('NaN')
wws_list.append(wws)
wws = df[price][i]
else:
start = i + 1 - n
end = i + 1
wws = wws - (sum(df[price][start:end]) / n) + df[price][i]
wws_list.append(wws)
return wws_list