forked from fulifeng/Adv-ALSTM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.py
146 lines (133 loc) · 5.95 KB
/
load.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
from datetime import datetime
import numpy as np
import os
def load_cla_data(data_path, tra_date, val_date, tes_date, seq=2,
date_format='%Y-%m-%d'):
fnames = [fname for fname in os.listdir(data_path) if
os.path.isfile(os.path.join(data_path, fname))]
print(len(fnames), ' tickers selected')
data_EOD = []
for index, fname in enumerate(fnames):
# print(fname)
single_EOD = np.genfromtxt(
os.path.join(data_path, fname), dtype=float, delimiter=',',
skip_header=False
)
# print('data shape:', single_EOD.shape)
data_EOD.append(single_EOD)
fea_dim = data_EOD[0].shape[1] - 2
trading_dates = np.genfromtxt(
os.path.join(data_path, '..', 'trading_dates.csv'), dtype=str,
delimiter=',', skip_header=False
)
print(len(trading_dates), 'trading dates:')
# transform the trading dates into a dictionary with index, at the same
# time, transform the indices into a dictionary with weekdays
dates_index = {}
# indices_weekday = {}
data_wd = np.zeros([len(trading_dates), 5], dtype=float)
wd_encodings = np.identity(5, dtype=float)
for index, date in enumerate(trading_dates):
dates_index[date] = index
# indices_weekday[index] = datetime.strptime(date, date_format).weekday()
data_wd[index] = wd_encodings[datetime.strptime(date, date_format).weekday()]
tra_ind = dates_index[tra_date]
val_ind = dates_index[val_date]
tes_ind = dates_index[tes_date]
print(tra_ind, val_ind, tes_ind)
# count training, validation, and testing instances
tra_num = 0
val_num = 0
tes_num = 0
# training
for date_ind in range(tra_ind, val_ind):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if abs(data_EOD[tic_ind][date_ind][-2]) > 1e-8:
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
tra_num += 1
print(tra_num, ' training instances')
# validation
for date_ind in range(val_ind, tes_ind):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if abs(data_EOD[tic_ind][date_ind][-2]) > 1e-8:
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
val_num += 1
print(val_num, ' validation instances')
# testing
for date_ind in range(tes_ind, len(trading_dates)):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if abs(data_EOD[tic_ind][date_ind][-2]) > 1e-8:
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
tes_num += 1
print(tes_num, ' testing instances')
# generate training, validation, and testing instances
# training
tra_pv = np.zeros([tra_num, seq, fea_dim], dtype=float)
tra_wd = np.zeros([tra_num, seq, 5], dtype=float)
tra_gt = np.zeros([tra_num, 1], dtype=float)
ins_ind = 0
for date_ind in range(tra_ind, val_ind):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if abs(data_EOD[tic_ind][date_ind][-2]) > 1e-8 and \
data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
tra_pv[ins_ind] = data_EOD[tic_ind][date_ind - seq: date_ind, : -2]
tra_wd[ins_ind] = data_wd[date_ind - seq: date_ind, :]
tra_gt[ins_ind, 0] = (data_EOD[tic_ind][date_ind][-2] + 1) / 2
ins_ind += 1
# validation
val_pv = np.zeros([val_num, seq, fea_dim], dtype=float)
val_wd = np.zeros([val_num, seq, 5], dtype=float)
val_gt = np.zeros([val_num, 1], dtype=float)
ins_ind = 0
for date_ind in range(val_ind, tes_ind):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if abs(data_EOD[tic_ind][date_ind][-2]) > 1e-8 and \
data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
val_pv[ins_ind] = data_EOD[tic_ind][date_ind - seq: date_ind, :-2]
val_wd[ins_ind] = data_wd[date_ind - seq: date_ind, :]
val_gt[ins_ind, 0] = (data_EOD[tic_ind][date_ind][-2] + 1) / 2
ins_ind += 1
# testing
tes_pv = np.zeros([tes_num, seq, fea_dim], dtype=float)
tes_wd = np.zeros([tes_num, seq, 5], dtype=float)
tes_gt = np.zeros([tes_num, 1], dtype=float)
ins_ind = 0
for date_ind in range(tes_ind, len(trading_dates)):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if abs(data_EOD[tic_ind][date_ind][-2]) > 1e-8 and \
data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
tes_pv[ins_ind] = data_EOD[tic_ind][date_ind - seq: date_ind, :-2]
# # for the momentum indicator
# tes_pv[ins_ind, -1, -1] = data_EOD[tic_ind][date_ind - 1, -1] - data_EOD[tic_ind][date_ind - 11, -1]
tes_wd[ins_ind] = data_wd[date_ind - seq: date_ind, :]
tes_gt[ins_ind, 0] = (data_EOD[tic_ind][date_ind][-2] + 1) / 2
ins_ind += 1
return tra_pv, tra_wd, tra_gt, val_pv, val_wd, val_gt, tes_pv, tes_wd, tes_gt
if __name__ == '__main__':
# TEST
_, _, tra_gt, _, _, val_gt, _, _, tes_gt = load_cla_data(
'/home/ffl/nus/MM/fintech/tweet_stock/data/stocknet-dataset/price/ourpped',
'2014-01-02', '2015-08-03', '2015-10-01'
)
print(np.sum(tra_gt))
print(np.sum(val_gt))
print(np.sum(tes_gt))
print(np.sum(tes_gt) / 3720)