-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathenv_stocktrading_llama_risk.py
599 lines (535 loc) · 23.6 KB
/
env_stocktrading_llama_risk.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
from __future__ import annotations
from typing import List
import gymnasium as gym
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from gymnasium import spaces
from gymnasium.utils import seeding
from stable_baselines3.common.vec_env import DummyVecEnv
matplotlib.use("Agg")
# from stable_baselines3.common.logger import Logger, KVWriter, CSVOutputFormat
class StockTradingEnv(gym.Env):
"""A stock trading environment for OpenAI gym"""
metadata = {"render.modes": ["human"]}
def __init__(
self,
df: pd.DataFrame,
stock_dim: int,
hmax: int,
initial_amount: int,
num_stock_shares: list[int],
buy_cost_pct: list[float],
sell_cost_pct: list[float],
reward_scaling: float,
state_space: int,
action_space: int,
tech_indicator_list: list[str],
turbulence_threshold=None,
risk_indicator_col="turbulence",
llm_sentiment_col="llm_sentiment", #added llm_sentiment
llm_risk_col="llm_risk",
make_plots: bool = False,
print_verbosity=10,
day=0,
initial=True,
previous_state=[],
model_name="",
mode="",
iteration="",
):
self.day = day
self.df = df
self.stock_dim = stock_dim
self.hmax = hmax
self.num_stock_shares = num_stock_shares
self.initial_amount = initial_amount # get the initial cash
self.buy_cost_pct = buy_cost_pct
self.sell_cost_pct = sell_cost_pct
self.reward_scaling = reward_scaling
self.state_space = state_space
self.action_space = action_space
self.tech_indicator_list = tech_indicator_list
self.action_space = spaces.Box(low=-1, high=1, shape=(self.action_space,))
self.observation_space = spaces.Box(
low=-np.inf, high=np.inf, shape=(self.state_space,)
)
self.data = self.df.loc[self.day, :]
self.terminal = False
self.make_plots = make_plots
self.print_verbosity = print_verbosity
self.turbulence_threshold = turbulence_threshold
self.risk_indicator_col = risk_indicator_col
self.llm_sentiment_col=llm_sentiment_col
self.llm_risk_col=llm_risk_col
self.initial = initial
self.previous_state = previous_state
self.model_name = model_name
self.mode = mode
self.iteration = iteration
# initalize state
self.state = self._initiate_state()
# initialize reward
self.reward = 0
self.turbulence = 0
self.cost = 0
self.trades = 0
self.episode = 0
# memorize all the total balance change
self.asset_memory = [
self.initial_amount
+ np.sum(
np.array(self.num_stock_shares)
* np.array(self.state[1 : 1 + self.stock_dim])
)
] # the initial total asset is calculated by cash + sum (num_share_stock_i * price_stock_i)
self.rewards_memory = []
self.actions_memory = []
self.state_memory = (
[]
) # we need sometimes to preserve the state in the middle of trading process
self.date_memory = [self._get_date()]
# self.logger = Logger('results',[CSVOutputFormat])
# self.reset()
self.seed()
def _sell_stock(self, index, action):
def _do_sell_normal():
if (
self.state[index + 2 * self.stock_dim + 1] != True
): # check if the stock is able to sell, for simlicity we just add it in techical index
# if self.state[index + 1] > 0: # if we use price<0 to denote a stock is unable to trade in that day, the total asset calculation may be wrong for the price is unreasonable
# Sell only if the price is > 0 (no missing data in this particular date)
# perform sell action based on the sign of the action
if self.state[index + self.stock_dim + 1] > 0:
# Sell only if current asset is > 0
sell_num_shares = min(
abs(action), self.state[index + self.stock_dim + 1]
)
sell_amount = (
self.state[index + 1]
* sell_num_shares
* (1 - self.sell_cost_pct[index])
)
# update balance
self.state[0] += sell_amount
self.state[index + self.stock_dim + 1] -= sell_num_shares
self.cost += (
self.state[index + 1]
* sell_num_shares
* self.sell_cost_pct[index]
)
self.trades += 1
else:
sell_num_shares = 0
else:
sell_num_shares = 0
return sell_num_shares
# perform sell action based on the sign of the action
if self.turbulence_threshold is not None:
if self.turbulence >= self.turbulence_threshold:
if self.state[index + 1] > 0:
# Sell only if the price is > 0 (no missing data in this particular date)
# if turbulence goes over threshold, just clear out all positions
if self.state[index + self.stock_dim + 1] > 0:
# Sell only if current asset is > 0
sell_num_shares = self.state[index + self.stock_dim + 1]
sell_amount = (
self.state[index + 1]
* sell_num_shares
* (1 - self.sell_cost_pct[index])
)
# update balance
self.state[0] += sell_amount
self.state[index + self.stock_dim + 1] = 0
self.cost += (
self.state[index + 1]
* sell_num_shares
* self.sell_cost_pct[index]
)
self.trades += 1
else:
sell_num_shares = 0
else:
sell_num_shares = 0
else:
sell_num_shares = _do_sell_normal()
else:
sell_num_shares = _do_sell_normal()
return sell_num_shares
def _buy_stock(self, index, action):
def _do_buy():
if (
self.state[index + 2 * self.stock_dim + 1] != True
): # check if the stock is able to buy
# if self.state[index + 1] >0:
# Buy only if the price is > 0 (no missing data in this particular date)
available_amount = self.state[0] // (
self.state[index + 1] * (1 + self.buy_cost_pct[index])
) # when buying stocks, we should consider the cost of trading when calculating available_amount, or we may be have cash<0
# print('available_amount:{}'.format(available_amount))
# update balance
buy_num_shares = min(available_amount, action)
buy_amount = (
self.state[index + 1]
* buy_num_shares
* (1 + self.buy_cost_pct[index])
)
self.state[0] -= buy_amount
self.state[index + self.stock_dim + 1] += buy_num_shares
self.cost += (
self.state[index + 1] * buy_num_shares * self.buy_cost_pct[index]
)
self.trades += 1
else:
buy_num_shares = 0
return buy_num_shares
# perform buy action based on the sign of the action
if self.turbulence_threshold is None:
buy_num_shares = _do_buy()
else:
if self.turbulence < self.turbulence_threshold:
buy_num_shares = _do_buy()
else:
buy_num_shares = 0
pass
return buy_num_shares
def _make_plot(self):
plt.plot(self.asset_memory, "r")
plt.savefig(f"results/account_value_trade_{self.episode}.png")
plt.close()
def step(self, actions):
self.terminal = self.day >= len(self.df.index.unique()) - 1
if self.terminal:
# print(f"Episode: {self.episode}")
if self.make_plots:
self._make_plot()
end_total_asset = self.state[0] + sum(
np.array(self.state[1 : (self.stock_dim + 1)])
* np.array(self.state[(self.stock_dim + 1) : (self.stock_dim * 2 + 1)])
)
df_total_value = pd.DataFrame(self.asset_memory)
tot_reward = (
self.state[0]
+ sum(
np.array(self.state[1 : (self.stock_dim + 1)])
* np.array(
self.state[(self.stock_dim + 1) : (self.stock_dim * 2 + 1)]
)
)
- self.asset_memory[0]
) # initial_amount is only cash part of our initial asset
df_total_value.columns = ["account_value"]
df_total_value["date"] = self.date_memory
df_total_value["daily_return"] = df_total_value["account_value"].pct_change(
1
)
if df_total_value["daily_return"].std() != 0:
sharpe = (
(252**0.5)
* df_total_value["daily_return"].mean()
/ df_total_value["daily_return"].std()
)
df_rewards = pd.DataFrame(self.rewards_memory)
df_rewards.columns = ["account_rewards"]
df_rewards["date"] = self.date_memory[:-1]
if self.episode % self.print_verbosity == 0:
print(f"day: {self.day}, episode: {self.episode}")
print(f"begin_total_asset: {self.asset_memory[0]:0.2f}")
print(f"end_total_asset: {end_total_asset:0.2f}")
print(f"total_reward: {tot_reward:0.2f}")
print(f"total_cost: {self.cost:0.2f}")
print(f"total_trades: {self.trades}")
if df_total_value["daily_return"].std() != 0:
print(f"Sharpe: {sharpe:0.3f}")
print("=================================")
if (self.model_name != "") and (self.mode != ""):
df_actions = self.save_action_memory()
df_actions.to_csv(
"results/actions_{}_{}_{}.csv".format(
self.mode, self.model_name, self.iteration
)
)
df_total_value.to_csv(
"results/account_value_{}_{}_{}.csv".format(
self.mode, self.model_name, self.iteration
),
index=False,
)
df_rewards.to_csv(
"results/account_rewards_{}_{}_{}.csv".format(
self.mode, self.model_name, self.iteration
),
index=False,
)
plt.plot(self.asset_memory, "r")
plt.savefig(
"results/account_value_{}_{}_{}.png".format(
self.mode, self.model_name, self.iteration
)
)
plt.close()
# Add outputs to logger interface
# logger.record("environment/portfolio_value", end_total_asset)
# logger.record("environment/total_reward", tot_reward)
# logger.record("environment/total_reward_pct", (tot_reward / (end_total_asset - tot_reward)) * 100)
# logger.record("environment/total_cost", self.cost)
# logger.record("environment/total_trades", self.trades)
return self.state, self.reward, self.terminal, False, {}
else:
# Apply LLM sentiment to influence actions
#llm_sentiments = self.data[self.llm_sentiment_col].values # Get LLM sentiment for all stocks
#actions = np.where(llm_sentiments > 0, actions, 0) # Example: Only execute actions where sentiment > 0
#RESUME HERE
# print("actions before: " + str(actions))
# Fetch LLM sentiments for the current day
llm_sentiments = self.data[self.llm_sentiment_col].values # values: [1, 2, 3, 4, 5]
# llm_risks = self.data[self.llm_risk_col].values # values: [1, 2, 3, 4, 5]
# Create masks for action types
buy_mask = (actions > 0)
sell_mask = (actions < 0)
# Create masks based on LLM sentiments
strong_sell_mask = (llm_sentiments == 1)
moderate_sell_mask = (llm_sentiments == 2)
hold_mask = (llm_sentiments == 3)
moderate_buy_mask = (llm_sentiments == 4)
strong_buy_mask = (llm_sentiments == 5)
# Adjust actions based on combined conditions
actions[(strong_sell_mask & buy_mask) | (strong_buy_mask & sell_mask)] *= 0.99 # Reduce mismatched strong actions
actions[(moderate_sell_mask & buy_mask) | (moderate_buy_mask & sell_mask)] *= 0.995 # Reduce mismatched moderate actions
# actions[hold_mask] *= 0.99 # Reduce all actions for neutral sentiment
actions[(strong_sell_mask & sell_mask) | (strong_buy_mask & buy_mask)] *= 1.01 # Amplify matched strong ac>
actions[(moderate_sell_mask & sell_mask) | (moderate_buy_mask & buy_mask)] *= 1.005 # Amplify matched mode>
# print("actions after: " + str(actions))
# print("actions after: " + str(actions))
actions = actions * self.hmax # actions initially is scaled between 0 to 1
actions = actions.astype(
int
) # convert into integer because we can't by fraction of shares
if self.turbulence_threshold is not None:
if self.turbulence >= self.turbulence_threshold:
actions = np.array([-self.hmax] * self.stock_dim)
begin_total_asset = self.state[0] + sum(
np.array(self.state[1 : (self.stock_dim + 1)])
* np.array(self.state[(self.stock_dim + 1) : (self.stock_dim * 2 + 1)])
)
# print("begin_total_asset:{}".format(begin_total_asset))
argsort_actions = np.argsort(actions)
sell_index = argsort_actions[: np.where(actions < 0)[0].shape[0]]
buy_index = argsort_actions[::-1][: np.where(actions > 0)[0].shape[0]]
for index in sell_index:
# print(f"Num shares before: {self.state[index+self.stock_dim+1]}")
# print(f'take sell action before : {actions[index]}')
actions[index] = self._sell_stock(index, actions[index]) * (-1)
# print(f'take sell action after : {actions[index]}')
# print(f"Num shares after: {self.state[index+self.stock_dim+1]}")
for index in buy_index:
# print('take buy action: {}'.format(actions[index]))
actions[index] = self._buy_stock(index, actions[index])
self.actions_memory.append(actions)
# state: s -> s+1
self.day += 1
self.data = self.df.loc[self.day, :]
if self.turbulence_threshold is not None:
if len(self.df.tic.unique()) == 1:
self.turbulence = self.data[self.risk_indicator_col]
elif len(self.df.tic.unique()) > 1:
self.turbulence = self.data[self.risk_indicator_col].values[0]
self.state = self._update_state()
end_total_asset = self.state[0] + sum(
np.array(self.state[1 : (self.stock_dim + 1)])
* np.array(self.state[(self.stock_dim + 1) : (self.stock_dim * 2 + 1)])
)
self.asset_memory.append(end_total_asset)
self.date_memory.append(self._get_date())
self.reward = end_total_asset - begin_total_asset
self.rewards_memory.append(self.reward)
self.reward = self.reward * self.reward_scaling
self.state_memory.append(
self.state
) # add current state in state_recorder for each step
return self.state, self.reward, self.terminal, False, {}
def reset(
self,
*,
seed=None,
options=None,
):
# initiate state
self.day = 0
self.data = self.df.loc[self.day, :]
self.state = self._initiate_state()
if self.initial:
self.asset_memory = [
self.initial_amount
+ np.sum(
np.array(self.num_stock_shares)
* np.array(self.state[1 : 1 + self.stock_dim])
)
]
else:
previous_total_asset = self.previous_state[0] + sum(
np.array(self.state[1 : (self.stock_dim + 1)])
* np.array(
self.previous_state[(self.stock_dim + 1) : (self.stock_dim * 2 + 1)]
)
)
self.asset_memory = [previous_total_asset]
self.turbulence = 0
self.cost = 0
self.trades = 0
self.terminal = False
# self.iteration=self.iteration
self.rewards_memory = []
self.actions_memory = []
self.date_memory = [self._get_date()]
self.episode += 1
return self.state, {}
def render(self, mode="human", close=False):
return self.state
def _initiate_state(self):
if self.initial:
# For Initial State
if len(self.df.tic.unique()) > 1:
# for multiple stock
# print("the type of self data is: " type(self.data.close))
# print("the llm sentiment is " + str(self.data[self.llm_sentiment_col].tolist()))
# print(' the closing vals are ' + str(self.data.close))
state = ([self.initial_amount]+ self.data.close.values.tolist()+ self.num_stock_shares+ sum(
(self.data[tech].values.tolist() for tech in self.tech_indicator_list),[],)
+ self.data[self.llm_sentiment_col].values.tolist() #add llm sentiment
+ self.data[self.llm_risk_col].values.tolist() #add llm sentiment
) # append initial stocks_share to initial state, instead of all zero
else:
# for single stock
state = (
[self.initial_amount]
+ [self.data.close]
+ [0] * self.stock_dim
+ sum(([self.data[tech]] for tech in self.tech_indicator_list), [])
+ [self.data[self.llm_sentiment_col]]
+ [self.data[self.llm_risk_col]]
)
else:
# Using Previous State
if len(self.df.tic.unique()) > 1:
# for multiple stock
state = (
[self.previous_state[0]]
+ self.data.close.values.tolist()
+ self.previous_state[
(self.stock_dim + 1) : (self.stock_dim * 2 + 1)
]
+ sum(
(
self.data[tech].values.tolist()
for tech in self.tech_indicator_list
),
[],
)
)
else:
# for single stock
state = (
[self.previous_state[0]]
+ [self.data.close]
+ self.previous_state[
(self.stock_dim + 1) : (self.stock_dim * 2 + 1)
]
+ sum(([self.data[tech]] for tech in self.tech_indicator_list), [])
)
return state
def _update_state(self):
if len(self.df.tic.unique()) > 1:
# for multiple stock
state = (
[self.state[0]]
+ self.data.close.values.tolist()
+ list(self.state[(self.stock_dim + 1) : (self.stock_dim * 2 + 1)])
+ sum(
(
self.data[tech].values.tolist()
for tech in self.tech_indicator_list
),
[],
)
+ self.data[self.llm_sentiment_col].values.tolist() # add LLM sentiment
+ self.data[self.llm_risk_col].values.tolist() # add LLM risk
)
else:
# for single stock
state = (
[self.state[0]]
+ [self.data.close]
+ list(self.state[(self.stock_dim + 1) : (self.stock_dim * 2 + 1)])
+ sum(([self.data[tech]] for tech in self.tech_indicator_list), [])
+ [self.data[self.llm_sentiment_col]] #add LLM sentiment
+ [self.data[self.llm_risk_col]] #add LLM risk
)
return state
def _get_date(self):
if len(self.df.tic.unique()) > 1:
date = self.data.date.unique()[0]
else:
date = self.data.date
return date
# add save_state_memory to preserve state in the trading process
def save_state_memory(self):
if len(self.df.tic.unique()) > 1:
# date and close price length must match actions length
date_list = self.date_memory[:-1]
df_date = pd.DataFrame(date_list)
df_date.columns = ["date"]
state_list = self.state_memory
df_states = pd.DataFrame(
state_list,
columns=[
"cash",
"Bitcoin_price",
"Gold_price",
"Bitcoin_num",
"Gold_num",
"Bitcoin_Disable",
"Gold_Disable",
],
)
df_states.index = df_date.date
# df_actions = pd.DataFrame({'date':date_list,'actions':action_list})
else:
date_list = self.date_memory[:-1]
state_list = self.state_memory
df_states = pd.DataFrame({"date": date_list, "states": state_list})
# print(df_states)
return df_states
def save_asset_memory(self):
date_list = self.date_memory
asset_list = self.asset_memory
# print(len(date_list))
# print(len(asset_list))
df_account_value = pd.DataFrame(
{"date": date_list, "account_value": asset_list}
)
return df_account_value
def save_action_memory(self):
if len(self.df.tic.unique()) > 1:
# date and close price length must match actions length
date_list = self.date_memory[:-1]
df_date = pd.DataFrame(date_list)
df_date.columns = ["date"]
action_list = self.actions_memory
df_actions = pd.DataFrame(action_list)
df_actions.columns = self.data.tic.values
df_actions.index = df_date.date
# df_actions = pd.DataFrame({'date':date_list,'actions':action_list})
else:
date_list = self.date_memory[:-1]
action_list = self.actions_memory
df_actions = pd.DataFrame({"date": date_list, "actions": action_list})
return df_actions
def seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def get_sb_env(self):
e = DummyVecEnv([lambda: self])
obs = e.reset()
return e, obs