forked from quantylab/rltrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_manager.py
149 lines (129 loc) · 5.82 KB
/
data_manager.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
import pandas as pd
import numpy as np
COLUMNS_CHART_DATA = ['date', 'open', 'high', 'low', 'close', 'volume']
COLUMNS_TRAINING_DATA_V1 = [
'open_lastclose_ratio', 'high_close_ratio', 'low_close_ratio',
'close_lastclose_ratio', 'volume_lastvolume_ratio',
'close_ma5_ratio', 'volume_ma5_ratio',
'close_ma10_ratio', 'volume_ma10_ratio',
'close_ma20_ratio', 'volume_ma20_ratio',
'close_ma60_ratio', 'volume_ma60_ratio',
'close_ma120_ratio', 'volume_ma120_ratio',
]
COLUMNS_TRAINING_DATA_V1_RICH = [
'open_lastclose_ratio', 'high_close_ratio', 'low_close_ratio',
'close_lastclose_ratio', 'volume_lastvolume_ratio',
'close_ma5_ratio', 'volume_ma5_ratio',
'close_ma10_ratio', 'volume_ma10_ratio',
'close_ma20_ratio', 'volume_ma20_ratio',
'close_ma60_ratio', 'volume_ma60_ratio',
'close_ma120_ratio', 'volume_ma120_ratio',
'inst_lastinst_ratio', 'frgn_lastfrgn_ratio',
'inst_ma5_ratio', 'frgn_ma5_ratio',
'inst_ma10_ratio', 'frgn_ma10_ratio',
'inst_ma20_ratio', 'frgn_ma20_ratio',
'inst_ma60_ratio', 'frgn_ma60_ratio',
'inst_ma120_ratio', 'frgn_ma120_ratio',
]
COLUMNS_TRAINING_DATA_V2 = [
'per', 'pbr', 'roe',
'open_lastclose_ratio', 'high_close_ratio', 'low_close_ratio',
'close_lastclose_ratio', 'volume_lastvolume_ratio',
'close_ma5_ratio', 'volume_ma5_ratio',
'close_ma10_ratio', 'volume_ma10_ratio',
'close_ma20_ratio', 'volume_ma20_ratio',
'close_ma60_ratio', 'volume_ma60_ratio',
'close_ma120_ratio', 'volume_ma120_ratio',
'market_kospi_ma5_ratio', 'market_kospi_ma20_ratio',
'market_kospi_ma60_ratio', 'market_kospi_ma120_ratio',
'bond_k3y_ma5_ratio', 'bond_k3y_ma20_ratio',
'bond_k3y_ma60_ratio', 'bond_k3y_ma120_ratio'
]
def preprocess(data, ver='v1'):
windows = [5, 10, 20, 60, 120]
for window in windows:
data['close_ma{}'.format(window)] = \
data['close'].rolling(window).mean()
data['volume_ma{}'.format(window)] = \
data['volume'].rolling(window).mean()
data['close_ma%d_ratio' % window] = \
(data['close'] - data['close_ma%d' % window]) \
/ data['close_ma%d' % window]
data['volume_ma%d_ratio' % window] = \
(data['volume'] - data['volume_ma%d' % window]) \
/ data['volume_ma%d' % window]
if ver == 'v1.rich':
data['inst_ma{}'.format(window)] = \
data['close'].rolling(window).mean()
data['frgn_ma{}'.format(window)] = \
data['volume'].rolling(window).mean()
data['inst_ma%d_ratio' % window] = \
(data['close'] - data['inst_ma%d' % window]) \
/ data['inst_ma%d' % window]
data['frgn_ma%d_ratio' % window] = \
(data['volume'] - data['frgn_ma%d' % window]) \
/ data['frgn_ma%d' % window]
data['open_lastclose_ratio'] = np.zeros(len(data))
data.loc[1:, 'open_lastclose_ratio'] = \
(data['open'][1:].values - data['close'][:-1].values) \
/ data['close'][:-1].values
data['high_close_ratio'] = \
(data['high'].values - data['close'].values) \
/ data['close'].values
data['low_close_ratio'] = \
(data['low'].values - data['close'].values) \
/ data['close'].values
data['close_lastclose_ratio'] = np.zeros(len(data))
data.loc[1:, 'close_lastclose_ratio'] = \
(data['close'][1:].values - data['close'][:-1].values) \
/ data['close'][:-1].values
data['volume_lastvolume_ratio'] = np.zeros(len(data))
data.loc[1:, 'volume_lastvolume_ratio'] = \
(data['volume'][1:].values - data['volume'][:-1].values) \
/ data['volume'][:-1] \
.replace(to_replace=0, method='ffill') \
.replace(to_replace=0, method='bfill').values
if ver == 'v1.rich':
data['inst_lastinst_ratio'] = np.zeros(len(data))
data.loc[1:, 'inst_lastinst_ratio'] = \
(data['inst'][1:].values - data['inst'][:-1].values) \
/ data['inst'][:-1] \
.replace(to_replace=0, method='ffill') \
.replace(to_replace=0, method='bfill').values
data['frgn_lastfrgn_ratio'] = np.zeros(len(data))
data.loc[1:, 'frgn_lastfrgn_ratio'] = \
(data['frgn'][1:].values - data['frgn'][:-1].values) \
/ data['frgn'][:-1] \
.replace(to_replace=0, method='ffill') \
.replace(to_replace=0, method='bfill').values
return data
def load_data(fpath, date_from, date_to, ver='v2'):
header = None if ver == 'v1' else 0
data = pd.read_csv(fpath, thousands=',', header=header,
converters={'date': lambda x: str(x)})
if ver == 'v1':
data.columns = ['date', 'open', 'high', 'low', 'close', 'volume']
# 날짜 오름차순 정렬
data = data.sort_values(by='date').reset_index()
# 데이터 전처리
data = preprocess(data)
# 기간 필터링
data['date'] = data['date'].str.replace('-', '')
data = data[(data['date'] >= date_from) & (data['date'] <= date_to)]
data = data.dropna()
# 차트 데이터 분리
chart_data = data[COLUMNS_CHART_DATA]
# 학습 데이터 분리
training_data = None
if ver == 'v1':
training_data = data[COLUMNS_TRAINING_DATA_V1]
elif ver == 'v1.rich':
training_data = data[COLUMNS_TRAINING_DATA_V1_RICH]
elif ver == 'v2':
data.loc[:, ['per', 'pbr', 'roe']] = \
data[['per', 'pbr', 'roe']].apply(lambda x: x / 100)
training_data = data[COLUMNS_TRAINING_DATA_V2]
training_data = training_data.apply(np.tanh)
else:
raise Exception('Invalid version.')
return chart_data, training_data