-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtrain_ppg.py
186 lines (150 loc) · 4.75 KB
/
train_ppg.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
import numpy as np
import pandas as pd
import torch
import os
import json
from torch.distributions import Categorical
from torch.utils.tensorboard import SummaryWriter
from collections import deque, namedtuple
from tqdm import tqdm
import joblib
import pathlib
from sklearn.preprocessing import MinMaxScaler
from model.ppg import PPG, Memory
from utils.trader import Trader
import arrow
signals = ["buy", "sell"]
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def main(
encodings,
df,
num_episodes=1000,
actor_hidden_dim=32,
critic_hidden_dim=256,
minibatch_size=64,
lr=0.0005,
betas=(0.9, 0.999),
lam=0.95,
gamma=0.99,
eps_clip=0.2,
value_clip=0.4,
beta_s=0.01,
update_timesteps=10000,
num_policy_updates_per_aux=32,
epochs=1,
epochs_aux=6,
seed=None,
save_every=5,
load=False,
):
state_dim = encodings.shape[1]
num_actions = len(signals)
memories = deque([])
aux_memories = deque([])
agent = PPG(
state_dim,
num_actions,
actor_hidden_dim,
critic_hidden_dim,
epochs,
epochs_aux,
minibatch_size,
lr,
betas,
lam,
gamma,
beta_s,
eps_clip,
value_clip,
)
if load:
agent.load()
if seed is not None:
torch.manual_seed(seed)
np.random.seed(seed)
time = 0
num_policy_updates = 0
reward = 0
writer = SummaryWriter()
for eps in tqdm(range(num_episodes), desc="episodes"):
trader = Trader()
day = arrow.get(df["Time"].iloc[0].format("YYYY-MM-DD"))
done = False
pbar = tqdm(range(len(encodings) - 1), total=len(encodings))
for idx in pbar:
# get row for price and date
row = df.iloc[idx]
current_day = arrow.get(row["Time"].format("YYYY-MM-DD"))
time += 1
# new day, check expiries
if current_day != day:
trader.eod(day.format("YYYY-MM-DD"))
day = current_day
state = encodings[idx]
state = torch.from_numpy(state).to(device)
action_probs, _ = agent.actor(state)
value = agent.critic(state)
dist = Categorical(action_probs)
action = dist.sample()
action_log_prob = dist.log_prob(action)
action = action.item()
if action == 0:
current_price = row["Spot"]
expiry = row["Expiry"].format("YYYY-MM-DD")
ticker = row["Ticker"]
trader.trade_on_signal(ticker, "BULLISH", current_price, expiry)
reward = trader.current_reward
if idx % 1000 == 0:
pbar.set_description(f"current return {reward:.2f}%")
if reward < -60:
done = True
next_state = encodings[idx + 1]
memory = Memory(state, action, action_log_prob, reward, done, value)
memories.append(memory)
state = next_state
if time % update_timesteps == 0:
agent.learn(memories, aux_memories, next_state)
num_policy_updates += 1
memories.clear()
if num_policy_updates % num_policy_updates_per_aux == 0:
agent.learn_aux(aux_memories)
aux_memories.clear()
if done:
break
if eps % save_every == 0:
agent.save()
writer.add_scalar("reward", reward, eps)
writer.flush()
writer.close()
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)
# only use subset of data
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")
main(encoded, df)
# train dqn model
main(encoded, df, lr=0.001, critic_hidden_dim=128, gamma=0.999)