forked from oldJonny/Candle_sticks_shape_Cluster_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataPreparation.py
94 lines (82 loc) · 2.81 KB
/
dataPreparation.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
import os
import pickle
class SingleWick():
def __init__(self, data):
# all stock data
self.data = data
# open
self.open = data['open']
# close
self.close = data['close']
# high
self.high = data['high']
# low
self.low = data['low']
# data file path
# self.dirpath = dirpath
# middle stick
def get_middlewick_rate(self):
middlewick = []
for i in range(len(self.open) ):
para1 = (self.close[i] - self.open[i]) / self.low[i]
middlewick.append(para1)
return middlewick
# upper stick
def get_upperwick_rate(self):
upperwick = []
for i in range(len(self.open) ):
# ups
if self.close[i] > self.open[i]:
para2 = (self.high[i] - self.close[i]) / self.low[i]
upperwick.append(para2)
# downs
else:
para2 = (self.high[i] - self.open[i]) / self.low[i]
upperwick.append(para2)
return upperwick
# down stick
def get_lowerwick_rate(self):
lowerwick = []
for i in range(len(self.open)):
# ups
if self.close[i] > self.open[i]:
para3 = (self.open[i] - self.low[i]) / self.low[i]
lowerwick.append(para3)
# downs
else:
para3 = (self.close[i] - self.low[i]) / self.low[i]
lowerwick.append(para3)
return lowerwick
# the positive value of close_dif means ups tread while the negative value means downs trend
# def get_close_dif_rate(self):
# close_dif = []
# for i in range(len(self.open) - 1):
# para4 = (self.close[i + 1] - self.close[i]) / self.close[i]
# close_dif.append(para4)
# return close_dif
def get_singlewick(self):
middlewick = self.get_middlewick_rate()
upperwick = self.get_upperwick_rate()
lowerwick = self.get_lowerwick_rate()
# close_dif = self.get_close_dif_rate()
singlewick = list(zip(middlewick, upperwick, lowerwick))
# for i in singlewick:
# if i[3] >= 0.11 or i[3] <= -0.11:
# singlewick.remove(i)
return singlewick
if __name__ == '__main__':
global X
X = []
dirpath = r'C:\Users\lyzdsb\PycharmProjects\untitled3\data'
code_list = os.listdir(dirpath + r'\raw_data')
for code in code_list:
file_path = dirpath + r'\raw_data' + '\\' + code
pkl_file = open(file_path, 'rb')
data = pickle.load(pkl_file)
pkl_file.close()
a = SingleWick(data)
temp = a.get_singlewick()
X = X + temp
output = open(dirpath + r'\raw_data_integrate\raw_data_para3.pkl', 'wb')
pickle.dump(X, output, -1)
output.close()