forked from zawecha1/DeepTrade_keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature.py
102 lines (92 loc) · 4.13 KB
/
feature.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
# -*- coding: utf-8 -*-
# Copyright 2017 The Xiaoyu Fang. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import os
from rawdata import RawData, read_sample_data
from dataset import DataSet
from chart import extract_feature
import numpy
def extract_from_file(filepath, output_prefix):
days_for_test = 700
input_shape = [30, 61] # [length of time series, length of feature]
window = input_shape[0]
fp = open("%s_feature.%s" % (output_prefix, window), "w")
lp = open("%s_label.%s" % (output_prefix, window), "w")
fpt = open("%s_feature.test.%s" % (output_prefix, window), "w")
lpt = open("%s_label.test.%s" % (output_prefix, window), "w")
selector = ["ROCP", "OROCP", "HROCP", "LROCP", "MACD", "RSI", "VROCP", "BOLL", "MA", "VMA", "PRICE_VOLUME"]
raw_data = read_sample_data(filepath)
moving_features, moving_labels = extract_feature(raw_data=raw_data, selector=selector, window=input_shape[0],
with_label=True, flatten=True)
print("feature extraction done, start writing to file...")
train_end_test_begin = moving_features.shape[0] - days_for_test
if train_end_test_begin < 0:
train_end_test_begin = 0
for i in range(0, train_end_test_begin):
for item in moving_features[i]:
fp.write("%s\t" % item)
fp.write("\n")
for i in range(0, train_end_test_begin):
lp.write("%s\n" % moving_labels[i])
# test set
for i in range(train_end_test_begin, moving_features.shape[0]):
for item in moving_features[i]:
fpt.write("%s\t" % item)
fpt.write("\n")
for i in range(train_end_test_begin, moving_features.shape[0]):
lpt.write("%s\n" % moving_labels[i])
fp.close()
lp.close()
fpt.close()
lpt.close()
if __name__ == '__main__':
days_for_test = 700
input_shape = [30, 61] # [length of time series, length of feature]
window = input_shape[0]
fp = open("ultimate_feature.%s" % window, "w")
lp = open("ultimate_label.%s" % window, "w")
fpt = open("ultimate_feature.test.%s" % window, "w")
lpt = open("ultimate_label.test.%s" % window, "w")
selector = ["ROCP", "OROCP", "HROCP", "LROCP", "MACD", "RSI", "VROCP", "BOLL", "MA", "VMA", "PRICE_VOLUME"]
dataset_dir = "./dataset"
for filename in os.listdir(dataset_dir):
#if filename != '000001.csv':
# continue
print("processing file: " + filename)
filepath = dataset_dir + "/" + filename
raw_data = read_sample_data(filepath)
moving_features, moving_labels = extract_feature(raw_data=raw_data, selector=selector, window=input_shape[0],
with_label=True, flatten=True)
print("feature extraction done, start writing to file...")
train_end_test_begin = moving_features.shape[0] - days_for_test
if train_end_test_begin < 0:
train_end_test_begin = 0
for i in range(0, train_end_test_begin):
for item in moving_features[i]:
fp.write("%s\t" % item)
fp.write("\n")
for i in range(0, train_end_test_begin):
lp.write("%s\n" % moving_labels[i])
# test set
for i in range(train_end_test_begin, moving_features.shape[0]):
for item in moving_features[i]:
fpt.write("%s\t" % item)
fpt.write("\n")
for i in range(train_end_test_begin, moving_features.shape[0]):
lpt.write("%s\n" % moving_labels[i])
fp.close()
lp.close()
fpt.close()
lpt.close()