-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktest.py
223 lines (159 loc) · 4.94 KB
/
backtest.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import numpy as np
import time
def percentChange(startPoint, currentPoint):
realmin = 0.00000000001
try:
x = ((float(currentPoint)-startPoint)/abs(startPoint))*100.0
if x == 0.0:
return realmin
else:
return x
except:
return realmin
def patternStorage():
# patStartTime = time.time()
x = len(avgLine)-(int(numPointsInPattern)*2)
y = int(numPointsInPattern)+1
while y < x:
pattern = []
for i in reversed(range(0,int(numPointsInPattern))):
pattern.append(percentChange(avgLine[y-int(numPointsInPattern)], avgLine[y-i]))
outcomeRange = avgLine[y+20:y+30]
currentPoint = avgLine[y]
try:
avgOutcome = reduce(lambda x, y: x+y, outcomeRange) / len(outcomeRange)
except Exception, e:
print str(e)
avgOutcome=0
futureOutcome = percentChange(currentPoint, avgOutcome)
patternAr.append(pattern)
performanceAr.append(futureOutcome)
y += 1
# patEndTime = time.time()
def currentPattern():
num = int(numPointsInPattern)+1
for i in reversed(range(1,num)):
cp = percentChange(avgLine[-num], avgLine[-i])
patForRec.append(cp)
# print patForRec
def patternRecognition():
predictedOutcomesAr = []
global patFound
patFound = 0
plotPatAr = []
plt.clf()
for eachPattern in patternAr[:-5]:
simSum = 0
for i in range(0,int(numPointsInPattern)):
sim = 100.00 - abs(percentChange(eachPattern[i], patForRec[i]))
if sim < 50:
break
simSum += sim
howSim = simSum / numPointsInPattern
if howSim > similarityThreshold:
patdex = patternAr.index(eachPattern)
patFound = 1
plotPatAr.append(eachPattern)
predArray = []
if patFound == 1:
xp = range(0,int(numPointsInPattern))
# fig = plt.figure(figsize=(10,6))
lastIdx = int(numPointsInPattern)-1
for eachPatt in plotPatAr:
futurePoints = patternAr.index(eachPatt)
if performanceAr[futurePoints] > patForRec[lastIdx]:
pcolor = '#00cc00'
predArray.append(1.000)
else:
pcolor = '#d44000'
predArray.append(-1.000)
plt.plot(xp, eachPatt)
predictedOutcomesAr.append(performanceAr[futurePoints])
plt.scatter(lastIdx+5, performanceAr[futurePoints], c=pcolor,alpha=.3)
realOutcomeRange = allData[toWhat+20:toWhat+30]
realAvgOutcome = reduce(lambda x, y: x+y, realOutcomeRange) / len(realOutcomeRange)
realMovement = percentChange(allData[toWhat], realAvgOutcome)
predictedAvgOutcome = reduce(lambda x, y: x+y, predictedOutcomesAr) / len(predictedOutcomesAr)
# print predArray
predictionAverage = reduce(lambda x, y: x+y, predArray) / len(predArray)
# print predictionAverage
if predictionAverage < 0:
print 'drop predicted'
print patForRec[lastIdx]
print realMovement
if realMovement < patForRec[lastIdx]:
accuracyArray.append(100)
else:
accuracyArray.append(0)
if predictionAverage > 0:
print 'rise predicted'
print patForRec[lastIdx]
print realMovement
if realMovement > patForRec[lastIdx]:
accuracyArray.append(100)
else:
accuracyArray.append(0)
plt.scatter(lastIdx+10, realMovement, c='#54fff7', s=25)
plt.scatter(lastIdx+10, predictedAvgOutcome, c='b', s=25)
plt.plot(xp, patForRec, '#54fff7', linewidth=3)
plt.grid(True)
plt.title('Pattern Recognition')
plt.draw()
# def graphRawFX():
# '''
# plot raw forex data
# '''
# fig = plt.figure(figsize=(10,7))
# ax1 = plt.subplot2grid((40,40), (0,0), rowspan=40, colspan=40)
# ax1.plot(date,bid)
# ax1.plot(date,ask)
# plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
# ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))
# for label in ax1.xaxis.get_ticklabels():
# label.set_rotation(45)
# ax1_2 = ax1.twinx()
# ax1_2.fill_between(date, 0, (ask-bid), facecolor='g', alpha=.3)
# plt.subplots_adjust(bottom=.23)
# plt.grid(True)
# plt.show()
totalStart = time.time()
date, bid, ask = np.loadtxt('GBPUSD1d.txt', unpack=True, delimiter=',',
converters={0:mdates.strpdate2num('%Y%m%d%H%M%S')})
dataLength = int(bid.shape[0])
print 'data length is',dataLength
toWhat = 3700
allData = ((bid+ask)/2)
accuracyArray = []
samps = 0
numPointsInPattern = 30.00
similarityThreshold = 70
fig = plt.figure(figsize=(10,6))
plt.ion()
plt.show()
# Problem as data gets large
# patternAr = []
# performanceAr = []
# patForRec = []
while toWhat < dataLength:
# avgLine = ((bid+ask)/2)
avgLine = allData[:toWhat]
# Problem as data gets large
patternAr = []
performanceAr = []
patForRec = []
# print 'Starting processing ...'
patternStorage()
currentPattern()
patternRecognition()
totalTime = time.time() - totalStart
# print 'Total processing time took:',totalTime,'seconds'
# moveOn = raw_input('press ENTER to continue...')
samps += 1
toWhat += 1
if len(accuracyArray) > 0:
accuracyAverage = reduce(lambda x, y: x+y, accuracyArray) / len(accuracyArray)
print 'Backtested Accuracy is',str(accuracyAverage)+'% after',samps,'samples'