-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbollinger_backtest.py
146 lines (116 loc) · 5.15 KB
/
bollinger_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
import json
import csv
print "Starting SIMULATION..."
with open('quotes.json') as json_data:
data = json.load(json_data)
dates = []
for agroup in data:
for thedate in agroup.keys():
dates.append(thedate)
#get ordered dates
dates = sorted(dates, reverse=True)
#for sanitizing data. Yeah taking the longer route only for testing purposes.
di = []
for adate in dates:
for againgroup in data:
for againdate in againgroup.values():
if adate == againdate[0]:
ki = {}
ki["date"] = againdate[0]
ki["second"] = againdate[1]
ki["third"] = againdate[2]
ki["fourth"] = againdate[3]
di.append(ki)
print "Loading PRICES..."
with open('prices.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
ni = []
for row in readCSV:
for anelement in di:
if anelement["date"] == row[0]:
ki = {}
ki["date"] = anelement["date"]
ki["price"] = row[1]
ki["macd"] = anelement["second"]
ki["ema9"] = anelement["third"]
ni.append(ki)
leli = ni
num_day = 30
results = []
popo = 0
with open('quotes-bollinger.json') as json_data_bollinger:
data_bollinger = json.load(json_data_bollinger)
# for changing format of data. Yeah taking the longer route only for testing purposes.
di_bollinger = []
for adate in dates:
for againgroup in data_bollinger:
for againdate in againgroup.values():
if adate == againdate[0]:
ki = {}
ki["date"] = againdate[0]
ki["upper_band"] = againdate[2]
ki["middle_band"] = againdate[3]
ki["lower_band"] = againdate[4]
for aele in ni:
avar = 0
if aele["date"] == adate:
ki["price"] = aele["price"]
di_bollinger.append(ki)
leli = di_bollinger
for popo in range(1720):
this_date = leli[(1719 - popo)]["date"]
di_bollinger = leli[(1720 - popo):(1750 - popo)]
boll_array = []
for anele in range(num_day):
boll_array.append(di_bollinger[anele])
boll_array = [x for x in boll_array[::-1]]
# print boll_array
boll_list = []
for k in range(len(boll_array)):
try:
if boll_array[k]["price"] < boll_array[k + 1]["price"] and boll_array[k + 1]["price"] > boll_array[k + 2][
"price"]:
boll_list.append({"MAX": boll_array[k + 1]})
if boll_array[k]["price"] > boll_array[k + 1]["price"] and boll_array[k + 1]["price"] < boll_array[k + 2][
"price"]:
boll_list.append({"MIN": boll_array[k + 1]})
except:
continue
# print boll_list
for k in range(len(boll_list)):
try:
if boll_list[k].keys()[0] == "MAX" and boll_list[k + 1].keys()[0] == "MIN" and boll_list[k + 2].keys()[
0] == "MAX":
if float(boll_list[k].values()[0]["price"]) > float(boll_list[k + 2].values()[0]["price"]):
print "Finding sell condition after date: " + boll_list[k + 1].values()[0]["date"]
compare_value = float(boll_list[k + 1].values()[0]["price"])
for athing in boll_list[k + 1::]:
if float(athing.values()[0]["price"]) < compare_value:
print "Sell condition on date: " + athing.values()[0]["date"]
results.append({"backtest_date": this_date, "test_name": "BOLLINGER", "result": "SELL",
"operate_on": athing.values()[0]["date"]})
break
if boll_list[k].keys()[0] == "MIN" and boll_list[k + 1].keys()[0] == "MAX" and boll_list[k + 2].keys()[
0] == "MIN":
if float(boll_list[k].values()[0]["price"]) < float(boll_list[k + 2].values()[0]["price"]):
print "Finding buy condition after date: " + boll_list[k + 1].values()[0]["date"]
compare_value = float(boll_list[k + 1].values()[0]["price"])
for athing in boll_list[k + 1::]:
if float(athing.values()[0]["price"]) > compare_value:
print "Buy condition on date: " + athing.values()[0]["date"]
results.append({"backtest_date": this_date, "test_name": "BOLLINGER", "result": "BUY",
"operate_on": athing.values()[0]["date"]})
break
except:
pass
print results
ofile = open('bollinger_results.csv', "wb")
writer = csv.writer(ofile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
for aresult in results:
row_list = []
row_list.append(aresult["backtest_date"])
row_list.append(aresult["result"])
row_list.append(aresult["operate_on"])
row_list.append(aresult["test_name"])
writer.writerow(row_list)
ofile.close()