-
Notifications
You must be signed in to change notification settings - Fork 37
/
clustering.py
135 lines (102 loc) · 4.09 KB
/
clustering.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
import numpy as np
import pandas as pd
import arrow
from tqdm import tqdm
from sklearn.manifold import TSNE
from sklearn.cluster import KMeans, MeanShift, DBSCAN, AgglomerativeClustering
import matplotlib.pyplot as plt
from joblib import dump, load
from utils.trader import Trader
import joblib
from sklearn.preprocessing import MinMaxScaler, StandardScaler
random_state = 42
def visualize(encodings):
print("dimensionality reduction and visualization...")
x_2d = TSNE(n_components=2).fit_transform(encodings)
np.save("cache/tsne.npy", x_2d)
plt.scatter(x_2d[:, 0], x_2d[:, 1])
plt.show()
def clustering(encodings, df, method, params, topn=1):
clusters = method(**params).fit(encodings)
dump(clusters, "clustering.joblib")
# check each cluster for profitability
n_clusters = len(set(clusters.labels_))
# noise labeled as -1
if -1 in clusters.labels_:
n_clusters -= 1
scores = []
print(f"{n_clusters} number of clusters")
for i in range(n_clusters):
trader = Trader()
day = arrow.get(df["Time"].iloc[0].format("YYYY-MM-DD"))
for idx, row in df.iterrows():
current_day = arrow.get(row["Time"].format("YYYY-MM-DD"))
# new day, check expiries
if current_day > day:
trader.eod(day.format("YYYY-MM-DD"))
day = current_day
# if target cluster, buy
if clusters.labels_[idx] == i:
current_price = row["Spot"]
expiry = row["Expiry"].format("YYYY-MM-DD")
ticker = row["Ticker"]
trader.trade_on_signal(ticker, "BULLISH", current_price, expiry)
scores.append(trader.current_reward)
print(f"cluster {i}\treturn: {scores[-1]:.2f}%")
top = sorted(scores, reverse=True)[:topn]
return [scores.index(s) for s in top]
def test(encodings, df, target_clusters):
clusters = load("clustering.joblib")
trader = Trader()
day = arrow.get(df["Time"].iloc[0].format("YYYY-MM-DD"))
print(f"start {day}")
for idx, row in df.iterrows():
current_day = arrow.get(row["Time"].format("YYYY-MM-DD"))
# new day, check expiries
if current_day > day:
trader.eod(day.format("YYYY-MM-DD"))
day = current_day
# if target cluster, buy
if clusters.labels_[idx] in target_clusters:
current_price = row["Spot"]
expiry = row["Expiry"].format("YYYY-MM-DD")
ticker = row["Ticker"]
trader.trade_on_signal(ticker, "BULLISH", current_price, expiry)
print(f"end {current_day}")
print(f"cluster {target_clusters}\treturn: {trader.current_reward:.2f}%")
def main(encodings, df, topn):
return clustering(encodings, df, KMeans, {"n_clusters": 100}, topn)
# return clustering(encodings, df, DBSCAN, {"eps": 0.3}, topn)
# return clustering(encodings, df, AgglomerativeClustering, {"n_clusters": 75}, 1)
if __name__ == "__main__":
df = pd.read_pickle("cache/encoded_rows.pkl")
print(df.head())
encoded = np.load("cache/unscaled_data.npy").astype(np.float32)
assert len(encoded) == len(df)
trader = Trader()
valid_tickers = trader.quotes.valid_tickers
# filter valid tickers
valid_rows, valid_x = [], []
for idx, row in df.iterrows():
if row["Ticker"] in valid_tickers:
valid_rows.append(row)
valid_x.append(encoded[idx])
print(encoded.shape)
df = pd.DataFrame(valid_rows)
encoded = np.array(valid_x)
assert len(encoded) == len(df)
# REMOVE
split = int(0.4 * len(encoded))
df, encoded = df.iloc[split:], encoded[split:]
########
split = int(0.6 * len(encoded))
encoded, encoded_test = encoded[:split], encoded[split:]
df, df_test = df.iloc[:split], df.iloc[split:]
print(encoded.shape)
# scale
scaler = MinMaxScaler()
scaler.fit(encoded)
encoded, encoded_test = scaler.transform(encoded), scaler.transform(encoded_test)
joblib.dump(scaler, "cache/cluster_scaler.gz")
target_clusters = main(encoded, df, 1)
test(encoded_test, df_test, target_clusters)