-
Notifications
You must be signed in to change notification settings - Fork 14
/
create_data.py
143 lines (117 loc) · 5.38 KB
/
create_data.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
import pandas as pd
from stock_env.apps import config
from preprocessor.yahoodownloader import YahooDownloader
from preprocessor.preprocessors import FeatureEngineer, data_split
import itertools
import argparse
from preprocessor.process_traj import trajectory
import numpy as np
import pickle
import os
def create_data(variant):
#Create datasets
# DOW (2009-01-01 ~ 2020-09-24),
# HIGHECH (2006-10-20 ~ 2013-11-21),
# S&P (2009-01-01 ~ 2021-12-31),
# MDAX (2009-01-01 ~ 2021-12-31),
# HSI (2009-01-01 ~ 2021-12-31),
# CSI (2009-01-01 ~ 2021-12-31)
if variant['dataset']=="dow":
df = YahooDownloader(start_date = '2009-01-01',
end_date = '2020-09-24',
ticker_list = config.DOW_TICKER).fetch_data()
elif variant['dataset']=="hightech":
df = YahooDownloader(start_date = '2006-10-20',
end_date = '2013-11-21',
ticker_list = config.HighTech_TICKER).fetch_data()
elif variant['dataset'] == "ndx":
df = YahooDownloader(start_date = '2009-01-01',
end_date = '2021-12-31',
ticker_list = config.NDX_TICKER).fetch_data()
elif variant['dataset'] == "mdax":
df = YahooDownloader(start_date = '2009-01-01',
end_date = '2021-12-31',
ticker_list = config.MDAX_TICKER).fetch_data()
elif variant['dataset'] == "csi":
df = YahooDownloader(start_date = '2009-01-01',
end_date = '2021-12-31',
ticker_list = config.CSI_TICKER).fetch_data()
df.sort_values(['date','tic'],ignore_index=True).head()
# Add technical indicator (macd, boll_ub, boll_lb, rsi_30, cci_30, dx_30, close_30_sma, close_60_sma)
fe = FeatureEngineer(
use_technical_indicator=True,
tech_indicator_list = config.TECHNICAL_INDICATORS_LIST,
use_turbulence=True
)
processed = fe.preprocess_data(df)
list_ticker = processed["tic"].unique().tolist()
list_date = list(pd.date_range(processed['date'].min(),processed['date'].max()).astype(str))
combination = list(itertools.product(list_date,list_ticker))
processed_full = pd.DataFrame(combination,columns=["date","tic"]).merge(processed,on=["date","tic"],how="left")
processed_full = processed_full[processed_full['date'].isin(processed['date'])]
processed_full = processed_full.sort_values(['date','tic'])
processed_full = processed_full.fillna(0)
processed_full.sort_values(['date','tic'],ignore_index=True).head(10)
# Split train and test datasets
if variant['dataset'] == "dow":
train = data_split(processed_full, '2009-01-01','2019-01-01')
trade = data_split(processed_full, '2019-01-01','2020-09-24')
elif variant['dataset'] == "hightech":
train = data_split(processed_full, '2006-10-20','2012-11-16')
trade = data_split(processed_full, '2012-11-16','2013-11-21')
else:
train = data_split(processed_full, '2009-01-01','2020-09-01')
trade = data_split(processed_full, '2020-09-01','2021-12-31')
if not os.path.exists("datasets"):
os.makedirs("datasets")
train.to_csv("datasets/"+variant['dataset']+"_train.csv")
trade.to_csv("datasets/"+variant['dataset']+"_trade.csv")
###################Create suboptimal trajectories########################
train = pd.read_csv("datasets/"+variant['dataset']+"_train.csv", index_col=[0])
stock_dimension = len(train.tic.unique())
state_space = 4 * stock_dimension + len(config.TECHNICAL_INDICATORS_LIST) * stock_dimension
print(f"Stock Dimension: {stock_dimension}, State Space: {state_space}")
env_kwargs = {
"state_space": state_space,
"stock_dim": stock_dimension,
"tech_indicator_list": config.TECHNICAL_INDICATORS_LIST,
"action_space": stock_dimension
}
env = trajectory(df=train, dataset=variant['dataset'], **env_kwargs)
def traj_generator(env, episode):
ob = env.reset()
obs = []
rews = []
term = []
acs = []
while True:
next_state, rew, new, ac = env.step(episode)
obs.append(ob)
term.append(new)
acs.append(ac)
rews.append(rew)
ob = next_state
if new:
break
obs = np.array(obs)
rews = np.array(rews)
term = np.array(term)
acs = np.array(acs)
print(np.sum(rews))
traj = {"observations": obs, "rewards": rews, "dones": term, "actions": acs}
return traj
paths = []
for i in range(5):
traj = traj_generator(env, i)
paths.append(traj)
if not os.path.exists("trajectory"):
os.makedirs("trajectory")
name = f'{"trajectory/"+variant["dataset"]+"_traj"}'
with open(f'{name}.pkl', 'wb') as f:
pickle.dump(paths, f)
print("Created trajectories")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default='hightech') #dow, hightech, ndx, mdax, csi (kdd was already given)
args = parser.parse_args()
create_data(variant=vars(args))