-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
173 lines (152 loc) · 5.29 KB
/
helper.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
__author__ = 'Michyo'
import os
import time
import datetime
separate_symbol = os.sep
folder = "data"
def stringIntoTime(s):
return datetime.datetime.fromtimestamp(time.mktime(time.strptime(s,"%Y%m%d %H%M%S")))
def combineFolderWithFilename(file_name):
global folder, separate_symbol
return folder + separate_symbol + file_name
def filenameIntoDate(file_name):
file_name_separate = os.path.split(file_name)
file_name_type_separate = os.path.splitext(file_name_separate[1])
if file_name_type_separate[1] == ".csv":
file_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(file_name_type_separate[0],"%Y%m%d")))
return file_date
else:
return datetime.datetime.now()
def isTradingTime(now):
start_trading = "091500"
end_trading = "161500"
start_time = time.strptime(start_trading, "%H%M%S")
end_time = time.strptime(end_trading, "%H%M%S")
now_time = time.strptime(now, "%H%M%S")
if now_time <= end_time and now_time >= start_time:
return True
else:
return False
def getProductCode(file_name):
date = filenameIntoDate(file_name)
HSIX3_initial_date = filenameIntoDate("20131101.csv")
if (date - HSIX3_initial_date).days in range(0, 28):
return "HSIX3"
HSIZ3_initial_date = filenameIntoDate("20131129.csv")
if (date - HSIZ3_initial_date).days in range(0, 32):
return "HSIZ3"
HSIF4_initial_date = filenameIntoDate("20131231.csv")
if (date - HSIF4_initial_date).days in range(0, 30):
return "HSIF4"
HSIG4_initial_date = filenameIntoDate("20140130.csv")
if (date - HSIG4_initial_date).days in range(0, 29):
return "HSIG4"
HSIH4_initial_date = filenameIntoDate("20140228.csv")
if (date - HSIH4_initial_date).days in range(0, 31):
return "HSIH4"
def getOneDayPrices(file_name):
prices = []
for line in open(combineFolderWithFilename(file_name)):
data_line = line.split(",")
product_code = getProductCode(file_name)
if data_line[1] == product_code and data_line[2] != "999999" and isTradingTime(data_line[0]):
prices.append(float(data_line[2]))
return prices
def isCSV(file_name):
file_name_type_separate = os.path.splitext(file_name)
if file_name_type_separate[1] == ".csv":
return True
else:
return False
def findFilesInFolder(start, end=100):
count = 0
files = []
for f in os.listdir(folder):
if isCSV(f):
if count >= start and count < end:
files.append(f)
count += 1
if count >= end:
return files
return files
def getOneDayData(file):
time_and_prices = []
for line in open(combineFolderWithFilename(file)):
data_line = line.split(",")
product_code = getProductCode(file)
if data_line[1] == product_code and data_line[2] != "999999" and isTradingTime(data_line[0]):
# bids = data_line[5:14:2]
# asks = data_line[16:25:2]
couples = [stringIntoTime(file[:8] + " " + data_line[0]), float(data_line[2]), float(data_line[5]), float(data_line[16])]
time_and_prices.append(couples)
return time_and_prices
def getClosePrices(prices):
closePrices = []
for i in prices:
closePrices.append(i[-1])
return closePrices
# ---- # ---- # ---- # ---- # ---- # ---- # ---- # ---- # ---- # ----
# Calculate simple moving average.
def calcSMA(prices):
avg = 0.0
if len(prices) == 0:
return avg
for p in prices:
avg += p
return avg / len(prices)
# Calculate standard deviation.
def calcSD(avg, prices):
if len(prices) == 0:
return 0.0
dev = 0.0
for data in prices:
dev += (data - avg) ** 2
return (dev / len(prices)) ** 0.5
def calcBollinger(prices, bollinger_band_multiplier):
middle_bollinger = calcSMA(prices)
sd = calcSD(middle_bollinger, prices)
upper_bollinger = middle_bollinger + bollinger_band_multiplier * sd
lower_bollinger = middle_bollinger - bollinger_band_multiplier * sd
return [middle_bollinger, upper_bollinger, lower_bollinger]
# ---- # ---- # ---- # ---- # ---- # ---- # ---- # ---- # ---- # ----
def calcRSI(close_price, prices):
gain, loss = [0.0] * 2
for p in prices:
if p > close_price:
gain += p - close_price
print "GAIN: " + str(p-close_price) # test
if p < close_price:
loss += close_price - p
print "LOSS: " + str(close_price-p) # test
if loss == 0:
return 100
RS = gain / loss
# print "gain = " + str(gain) # test
# print "loss = " + str(loss) # test
# print "RS = " + str(RS) # test
return 100 - 100 / (1 + RS)
'''
files = findFilesInFolder(0, 5)
close_prices = []
for f in files:
prices_of_a_day = getOneDayPrices(f)
close_prices.append(prices_of_a_day[-1])
bollinger = calcBollinger(close_prices, 2)
print bollinger
'''
def getColumnData(a, start, end, column):
result = []
for i in range(start, end):
result.append(a[i][column])
return result
def next10Average(a, i):
avg = 0.0
if i + 10 < len(a):
for k in range(i+1, i+11):
avg += a[k][3]
return avg / 10.0
else:
for k in range(i+1, len(a)):
avg += a[k][3]
return avg / 10.0
return avg