-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer2.py
162 lines (114 loc) · 4.18 KB
/
analyzer2.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
#Software Engineering Project - Group 14
import json, urllib2, time
from datetime import datetime
from time import mktime
import sys
from ann import ANN
## ================================================================
def normalizePrice(price, minimum, maximum):
return ((2*price - (maximum + minimum)) / (maximum - minimum))
def denormalizePrice(price, minimum, maximum):
return (((price*(maximum-minimum))/2) + (maximum + minimum))/2
## ================================================================
def rollingWindow(seq, wSize):
iteration = iter(seq)
win = [iteration.next() for cnt in xrange(wSize)] # First window
yield win
for e in iteration: # Subsequent windows
win[:-1] = win[1:]
win[-1] = e
yield win
def getMAverage(values, wSize): #moving average
mAverages = []
for i in rollingWindow(values, wSize):
mAverages.append(sum(i)/len(i))
return mAverages
def getMins(values, wSize):
mins = []
for i in rollingWindow(values, wSize):
mins.append(min(i))
return mins
def getMaxs(values, wSize):
maxs = []
for i in rollingWindow(values, wSize):
maxs.append(max(i))
return maxs
## ================================================================
def getTimeSeriesValues(values, window):
mAverages = getMAverage(values, window)
mins = getMins(values, window)
maxs = getMaxs(values, window)
returnData = []
# build items of the form [[average, minimum, maximum], normalized price]
for i in range(0, len(mAverages)):
inNode = [mAverages[i], mins[i], maxs[i]]
price = normalizePrice(values[len(mAverages) - (i + 1)], mins[i], maxs[i])
outNode = [price]
tempItem = [inNode, outNode]
returnData.append(tempItem)
return returnData
## ================================================================
def getHistData(symbol):
hist = []
# login to API
urllib2.urlopen("http://api.kibot.com/?action=login&user=guest&password=guest")
# get 1 year of data from API (business days only)
url = "http://api.kibot.com/?action=history&symbol=" + symbol + "&interval=daily&period=365&unadjusted=1®ularsession=1"
apiData = urllib2.urlopen(url).read().split("\n")
for line in apiData:
if(len(line) > 0):
temp = line.split(',')
price = float(temp[1])
hist.append(price)
return hist
## ================================================================
def getTrainingData(stockSymbol):
hist = getHistData(stockSymbol)
# reverse it so we're using the most recent data first, ensure we only have 9 data points
hist.reverse()
del hist[9:]
# get five 5-day moving averages, 5-day lows, and 5-day highs, associated with the closing price
tData = getTimeSeriesValues(hist, 5) #training data
return tData
def getPredictionData(symbol,flag):
#print "1st"
hist = getHistData(symbol)
hist.reverse()
#print historicalData
# reverse it so we're using the most recent data first, then ensure we only have 5 data points
global check
check=flag
if(check==0):
del hist[5:]
else:
del hist[5:]
hist[1]=hist[0]
hist[2]=hist[1]
hist[3]=hist[2]
hist[4]=hist[3]
hist[0]=new_value
predData = getTimeSeriesValues(hist, 5)
return predData[0][0]
## ================================================================
def analyzeSymbol(symbol):
startTime = time.time()
flag=0
trainingData = getTrainingData(symbol)
network = ANN(inNode = 3, hiddenNode = 3, outNode = 1)
network.training(trainingData)
# get rolling data for most recent day
#network.training(trainingData)
for i in range(0,5):
# get rolling data for most recent day
predictionData = getPredictionData(symbol,flag)
returnPrice = network.test(predictionData)
# de-normalize and return predicted stock price
predictedStockPrice = denormalizePrice(returnPrice, predictionData[1], predictionData[2])
print predictedStockPrice
flag+=1
global new_value
new_value=predictedStockPrice
return predictedStockPrice
## ================================================================
if __name__ == "__main__":
analyzeSymbol(sys.argv[1])