-
Notifications
You must be signed in to change notification settings - Fork 167
/
perceptually_important.py
70 lines (50 loc) · 2.12 KB
/
perceptually_important.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mplfinance as mpf
def find_pips(data: np.array, n_pips: int, dist_measure: int):
# dist_measure
# 1 = Euclidean Distance
# 2 = Perpindicular Distance
# 3 = Vertical Distance
pips_x = [0, len(data) - 1] # Index
pips_y = [data[0], data[-1]] # Price
for curr_point in range(2, n_pips):
md = 0.0 # Max distance
md_i = -1 # Max distance index
insert_index = -1
for k in range(0, curr_point - 1):
# Left adjacent, right adjacent indices
left_adj = k
right_adj = k + 1
time_diff = pips_x[right_adj] - pips_x[left_adj]
price_diff = pips_y[right_adj] - pips_y[left_adj]
slope = price_diff / time_diff
intercept = pips_y[left_adj] - pips_x[left_adj] * slope;
for i in range(pips_x[left_adj] + 1, pips_x[right_adj]):
d = 0.0 # Distance
if dist_measure == 1: # Euclidean distance
d = ( (pips_x[left_adj] - i) ** 2 + (pips_y[left_adj] - data[i]) ** 2 ) ** 0.5
d += ( (pips_x[right_adj] - i) ** 2 + (pips_y[right_adj] - data[i]) ** 2 ) ** 0.5
elif dist_measure == 2: # Perpindicular distance
d = abs( (slope * i + intercept) - data[i] ) / (slope ** 2 + 1) ** 0.5
else: # Vertical distance
d = abs( (slope * i + intercept) - data[i] )
if d > md:
md = d
md_i = i
insert_index = right_adj
pips_x.insert(insert_index, md_i)
pips_y.insert(insert_index, data[md_i])
return pips_x, pips_y
if __name__ == "__main__":
data = pd.read_csv('BTCUSDT86400.csv')
data['date'] = data['date'].astype('datetime64[s]')
data = data.set_index('date')
i = 1198
x = data['close'].iloc[i-40:i].to_numpy()
pips_x, pips_y = find_pips(x, 5, 2)
pd.Series(x).plot()
for i in range(5):
plt.plot(pips_x[i], pips_y[i], marker='o', color='red')
plt.show()