forked from neurotrader888/TechnicalAnalysisAutomation
-
Notifications
You must be signed in to change notification settings - Fork 2
/
retracement_ratios.py
38 lines (29 loc) · 1.26 KB
/
retracement_ratios.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mplfinance as mpf
import scipy
import math
import pandas_ta as ta
from directional_change import directional_change, get_extremes
data = pd.read_csv('BTCUSDT3600.csv')
data['date'] = data['date'].astype('datetime64[s]')
data = data.set_index('date')
plt.style.use('dark_background')
for sigma in [0.01, 0.02, 0.03, 0.04]:
extremes = get_extremes(data, sigma)
# Find segment heights, retracement ratios
extremes['seg_height'] = (extremes['ext_p'] - extremes['ext_p'].shift(1)).abs()
extremes['retrace_ratio'] = extremes['seg_height'] / extremes['seg_height'].shift(1)
extremes['log_retrace_ratio'] = np.log(extremes['retrace_ratio'])
# Find kernal of log retrace ratios
kernal = scipy.stats.gaussian_kde(extremes['log_retrace_ratio'].dropna(), bw_method=0.01)
retrace_range = np.arange(-3, 3, 0.001)
retrace_pdf = kernal(retrace_range)
retrace_pdf = pd.Series(retrace_pdf, index=np.exp(retrace_range))
retrace_pdf.plot(color='orange', label='Retrace PDF')
plt.axvline(0.618, label='0.618', color='blue')
plt.axvline(1.618, label='1.618', color='green')
plt.title("Retracement Density (Sigma=" + str(sigma) + ")")
plt.legend()
plt.show()