forked from shenrunzhang/forex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreshold.py
More file actions
68 lines (50 loc) · 1.62 KB
/
Threshold.py
File metadata and controls
68 lines (50 loc) · 1.62 KB
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
import pandas as pd
import numpy as np
import math
# gets upper bound for the threshold
def get_upper_threshold(close):
difference = close.diff()
difference[0] = 0
difference = difference.abs()
bins = pd.cut(difference, bins=10)
bins = bins.value_counts().to_frame().reset_index()
bins["index"] = bins["index"].apply(lambda x: x.right)
bins = bins.to_numpy()
percentile_count = len(difference) * 0.85
count = 0
for i in range(10):
count += bins[i, 1]
if count > percentile_count:
return bins[i, 0]
# calculate entropy
def get_entropy(labels, base=None):
vc = pd.Series(labels).value_counts(normalize=True, sort=False)
base = math.e if base is None else base
return -(vc * np.log(vc)/np.log(base)).sum()
# get best threshold
def get_threshold(close):
difference = close.diff()
difference = difference.drop(0)
difference = difference.tolist()
threshold = 0
thres_upper_bound = get_upper_threshold(close)
temp_thres = 0
best_entropy = -float('inf')
while temp_thres < thres_upper_bound:
labels = []
for diff in difference:
if diff > temp_thres:
labels.append(2)
elif -diff > temp_thres:
labels.append(1)
else:
labels.append(0)
entropy = get_entropy(labels)
if entropy > best_entropy:
best_entropy = entropy
threshold = temp_thres
temp_thres = temp_thres + 0.00001
return threshold
if __name__ == "main":
dataframe = pd.read_csv("data.csv")
close = dataframe["Close"]