-
Notifications
You must be signed in to change notification settings - Fork 0
/
dqn_2stock_trading.py
490 lines (241 loc) · 7.99 KB
/
dqn_2stock_trading.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import time; start_time = time.perf_counter();
import os, dotenv;
import numpy as np;
from tensorflow.keras.models import Sequential;
from tensorflow.keras.layers import Dense;
from tensorflow.keras.optimizers import Adam;
#
# Define relevant constants
#
# EPISODE_COUNT = 100;
# DAYS_PER_EPISODE = 10;
EPISODE_COUNT = 150;
DAYS_PER_EPISODE = 15;
ACCT_BAL_0 = 1000;
SHARES_PER_TRADE = 10;
actions = ('hold', 'buy', 'sell');
ACTIONS = [(a,b) for a in actions for b in actions];
#
# Define greek hyperparameters: random action rate, learn rate, discount rate
#
EPSILON = .10;
ALPHA = .1250;
GAMMA = .9750;
#
# Define state-space/action-space size
#
# state_count_A, state_count_B = 10, 10;
state_count_A, state_count_B = 15, 15;
action_countA, action_count_B = 3, 3;
#
# Simulate daily closing price by evaluating f(theta) = 4sin(theta) + 25 for a specified theta (e.g. day)
# Stock XYZ
#
# def closing_price_A (theta):
# return 12*np.sin(.3*theta) + 25;
def closing_price_A (theta):
noise = 0;
random_value = np.random.rand();
if random_value <= .250:
noise = 2*np.random.standard_t(1,1)[0];
elif random_value > .250 and random_value <= .750:
noise = np.random.standard_cauchy(1)[0];
elif random_value > .750:
noise = np.random.gamma(4.25,1,1)[0];
if np.abs(noise) > 25:
noise *= .1250;
return (12*np.sin(.3*theta)+25) + noise;
#
# Simulate daily closing price by evaluating f(theta) = 25cos(.5*theta) + 35 for specified theta (e.g day)
#
# def closing_price_B (theta):
# return 25*np.cos(.5*theta) + 35;
def closing_price_B (theta):
noise = 0;
random_value = np.random.rand();
if random_value <= .50:
noise = np.random.poisson(8,1)[0];
else:
noise = np.random.rayleigh(5);
if np.random.rand() <= .725:
noise *= -1;
if np.abs(noise) > 15:
noise *= .250;
return (25*np.cos(.5*theta)+35) + noise;
#
# Define Feed-Forward ANN containing a single hidden layer with nonlinear ReLU activation functions
#
def q_ann (input_size, output_size, hidden_layer_size=64):
model = Sequential();
model.add(Dense(hidden_layer_size, input_dim=input_size, activation='relu'));
model.add(Dense(output_size, activation='linear'));
model.compile(loss='mse', optimizer=Adam(lr=ALPHA));
return model;
#
# One-hot encode states
#
def one_hot_states (state_A, state_B):
state = np.zeros(state_count_A+state_count_B);
state[state_A] = 1;
state[state_count_A + state_B] = 1;
return state;
#
# Define function to determine the (next state, reward) when given (current state, action)
#
def execute_action (state_A, state_B, action_idx, shares_A, shares_B, bal):
action_A, action_B = ACTIONS[action_idx];
#
# Define action for Stock A
#
next_day_price_A = closing_price_A(state_A + 1);
next_day_price_B = closing_price_B(state_B + 1);
if action_A == 'buy':
if bal >= next_day_price_A * SHARES_PER_TRADE:
shares_A += SHARES_PER_TRADE;
bal -= next_day_price_A * SHARES_PER_TRADE;
elif action_A == 'sell':
if shares_A >= SHARES_PER_TRADE:
shares_A -= SHARES_PER_TRADE;
bal += next_day_price_A * SHARES_PER_TRADE;
#
# Define action for Stock B
#
if action_B == 'buy':
if bal >= next_day_price_B * SHARES_PER_TRADE:
shares_B += SHARES_PER_TRADE;
bal -= next_day_price_B * SHARES_PER_TRADE;
elif action_B == 'sell':
if shares_B >= SHARES_PER_TRADE:
shares_B -= SHARES_PER_TRADE;
bal += next_day_price_B * SHARES_PER_TRADE;
#
# Define next state for Stocks A, B
#
next_state_A = (state_A +1) % state_count_A;
next_state_B = (state_B +1) % state_count_B;
#
# Account value is weighted combination of shares and price
#
next_acct_value = bal + shares_A*next_day_price_A + shares_B*next_day_price_B;
#
# Compute reward relative to starting balance
#
reward = next_acct_value - ACCT_BAL_0;
if action_A == 'sell' and action_B == 'sell':
reward *= 1.05;
if action_A == 'hold' and action_B == 'hold':
reward *= .975;
return {
'next_state_A' : next_state_A,
'next_state_B' : next_state_B,
'shares_A' : shares_A,
'shares_B' : shares_B,
'bal' : bal,
'reward' : reward
};
#
# Define neural network I/O dimensions
#
ann_input_size = state_count_A + state_count_B;
ann_output_size = len(ACTIONS);
q_network = q_ann(ann_input_size, ann_output_size);
#
# Define epsilon-greedy policy implementation (e.g. map: states -> actions)
#
def choose_action (state_A, state_B, q_net):
if np.random.rand() < EPSILON:
return np.random.choice(ann_output_size);
else:
state = one_hot_states(state_A, state_B);
q_vals = q_net.predict(state.reshape(1, -1));
return np.argmax(q_vals[0]);
for ep in range(EPISODE_COUNT):
state_A = ep * DAYS_PER_EPISODE % state_count_A;
state_B = ep * DAYS_PER_EPISODE % state_count_B;
acct_bal = ACCT_BAL_0;
shares_held_A = 0;
shares_held_B = 0;
for day in range(DAYS_PER_EPISODE):
#
# Represent state with one-hot encoded value
#
state = one_hot_states(state_A, state_B);
#
# Choose action per epsilon-greedy policy
#
action_index = choose_action(state_A, state_B, q_network);
action_outcome = execute_action(state_A, state_B, action_index, shares_held_A, shares_held_B, acct_bal);
print('priceA = ', closing_price_A(state_A));
print('priceB = ', closing_price_B(state_B));
next_state_A = action_outcome['next_state_A']; print('nextA = ', next_state_A);
next_state_B = action_outcome['next_state_B']; print('nextB = ', next_state_B);
shares_held_A = action_outcome['shares_A']; print('sharesA = ', shares_held_A);
shares_held_B = action_outcome['shares_B']; print('sharesB = ', shares_held_B);
reward = action_outcome['reward']; print('reward = ', reward);
acct_bal = action_outcome['bal']; print('account balance = ', acct_bal);
#
# Compute target weights
#
next_state = one_hot_states(next_state_A, next_state_B);
q_vals_next = q_network.predict(next_state.reshape(1,-1));
target = reward + GAMMA * np.amax(q_vals_next[0]);
#
# Update target weights
#
q_vals = q_network.predict(state.reshape(1, -1));
q_vals[0][action_index] = target;
q_network.fit(state.reshape(1,-1), q_vals, epochs=1, verbose=0);
#
# Define terminal condition (e.g. ran out of money)
#
if acct_bal <= 0:
print('!!!!!! BANKRUPT !!!!!!!!');
break;
#
# Update Q-table per Q-learning update rule
#
state_A, state_B = next_state_A, next_state_B;
print('Q VALUES\n', q_vals);
print('-------------');
print('\n');
#
# Define testing environment for learned agent
#
def test_agent (q_net, state0_A, state0_B, shares0_A, shares0_B, bal0, max_iterations=1e4):
state_A, state_B = state0_A, state0_B;
shares_A, shares_B = shares0_A, shares0_B;
bal = bal0;
total_reward = 0;
iter = 0;
while True:
state = one_hot_states(state_A, state_B);
q_vals = q_net.predict(state.reshape(1,-1));
action = np.argmax(q_vals[0]);
action_outcome = execute_action(state_A, state_B, action, shares_A, shares_B, bal);
next_state_A = action_outcome['next_state_A'];
shares_A = action_outcome['shares_A'];
next_state_B = action_outcome['next_state_B'];
shares_B = action_outcome['shares_B'];
bal = action_outcome['bal'];
reward = action_outcome['reward'];
total_reward += reward;
state_A, state_B = next_state_A, next_state_B;
#
# Terminal condition for intra-episode iterations
#
if bal <= 0 or iter == int(max_iterations):
break;
iter += 1;
return {'total_reward':total_reward, 'acct_bal':bal, 'shares':(shares_A, shares_B)};
#
# Test agent
# • Iterate over states: state0,...,state_terminal
# • At each state, determine action by evaluating the ANN-approximated q-function
# • Take action -> observe reward and the next state
#
Q_test = test_agent(q_net=q_network, state0_A=0, state0_B=0, shares0_A=0, shares0_B=0, bal0=ACCT_BAL_0);
print('----------');
print('TESTING\n'); print(Q_test, '\n');
end_time = time.perf_counter();
elapsed_time = end_time - start_time;
print(f"Program took {elapsed_time} seconds.");