-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacd.py
326 lines (256 loc) · 9.3 KB
/
macd.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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)
num_day = input("Number of days to consider = ")
prices = []
macds = []
ema9 = []
for i in range(num_day):
prices.append([float(ni[i]["price"]),ni[i]["date"]])
macds.append([float(ni[i]["macd"]),ni[i]["date"]])
ema9.append([float(ni[i]["ema9"]),ni[i]["date"]])
#checking and storing minimas
prices_minimas = []
macd_minimas = []
for k in range(len(prices)):
try:
if prices[k][0] < prices[k-1][0] and prices[k][0] < prices[k+1][0]:
prices_minimas.append(prices[k])
else:
pass
except:
pass
for t in range(len(macds)):
try:
if macds[t][0] < macds[t-1][0] and macds[t][0] < macds[t+1][0]:
macd_minimas.append(prices[t])
else:
pass
except:
pass
print "Price minimas are: " + str(prices_minimas)
print "MACD minimas are: " + str(macd_minimas)
buy_con = []
sel_con = []
for amin in range(len(prices_minimas)):
try:
if prices_minimas[amin] < prices_minimas[amin + 1]:
pass
else:
buy_con.append(0)
print "Price analysis failed for Buy condition."
break
except:
pass
for dmin in range(len(macd_minimas)):
try:
if macd_minimas[dmin] > macd_minimas[dmin + 1]:
pass
else:
buy_con.append(0)
print "MACD analysis failed for Buy condition"
break
except:
pass
for hmin in range(len(prices_minimas)):
try:
if prices_minimas[hmin] > prices_minimas[hmin + 1]:
pass
else:
sel_con.append(0)
print "Price analysis failed for Sell condition."
break
except:
pass
for nmin in range(len(macd_minimas)):
try:
if macd_minimas[nmin] < macd_minimas[nmin + 1]:
pass
else:
sel_con.append(0)
print "MACD analysis failed for Sell condition."
break
except:
pass
if float(ni[0]["macd"]) == float(ni[0]["ema9"]):
main_var = "CONTINUE"
if float(ni[0]["macd"]) < float(ni[0]["ema9"]):
main_var = "less"
if float(ni[0]["macd"]) > float(ni[0]["ema9"]):
main_var = "more"
cross_list = []
for aval in range(num_day):
#if float(aval["macd"]) == float(aval["ema9"])... This condition has to be developed for future.
if float(ni[aval]["macd"]) == float(ni[aval]["ema9"]):
print "OOPS! The unexpected happened!"
break
if float(ni[aval]["macd"]) < float(ni[aval]["ema9"]):
new_con = "less"
elif float(ni[aval]["macd"]) > float(ni[aval]["ema9"]):
new_con = "more"
if main_var == "more" and new_con == "less":
cross_list.append("cross_macd_now_less")
main_var = "less"
print "Cross Date: " + ni[aval]["date"]
else:
pass
if main_var == "less" and new_con == "more":
cross_list.append("cross_macd_now_high")
main_var = "more"
print "Cross Date: " + ni[aval]["date"]
else:
pass
if len(cross_list) == 0:
print "No cross occurs"
elif len(cross_list) == 1:
if cross_list[0] == "cross_macd_now_less":
print "You must SELL as per line-cross analysis."
if cross_list[0] == "cross_macd_now_high":
print "You must BUY as per line-cross analysis."
elif len(cross_list) > 1:
print "More than one cross. We will figure it out soon! :P"
else:
pass
print cross_list
with open('quotes-1.json') as json_data_stck:
data_stck = json.load(json_data_stck)
#for changing format of data. Yeah taking the longer route only for testing purposes.
di_stck = []
for adate in dates:
for againgroup in data_stck:
for againdate in againgroup.values():
if adate == againdate[0]:
ki = {}
ki["date"] = againdate[0]
ki["slow_k"] = againdate[1]
ki["slow_d"] = againdate[2]
di_stck.append(ki)
#the D-line analysis
print "Checking D-Line Analysis..."
if float(di_stck[0]["slow_d"]) < 20:
main_val = "less_than_20"
if 20 < float(di_stck[0]["slow_d"]) < 80:
main_val = "but_less_than_80"
if float(di_stck[0]["slow_d"]) > 80:
main_val = "less_than_none"
cross_check_list = []
for aval in range(num_day):
if float(di_stck[aval]["slow_d"]) < 20:
check_val = "less_than_20"
if 20 < float(di_stck[aval]["slow_d"]) < 80:
check_val = "but_less_than_80"
if float(di_stck[aval]["slow_d"]) > 80:
check_val = "less_than_none"
if main_val != check_val:
cross_check_list.append([di_stck[aval]["date"], main_val, check_val])
main_val = check_val
#print cross_check_list
#End of D-line Analysis.
print "Analysing SMA-20 Slope..."
#Analysing SMA-20's Slope on the present day. Here it will be 1/02/2017 as per the database available.
####Calculating SMA20 for Today####
sum = 0
for ani in range(20):
sum += float(ni[ani]["price"])
sma_today = sum/20
####Calculating SMA20 for a day before####
sum = 0
for ani in range(21):
if ani == 0:
continue
sum += float(ni[ani]["price"])
sma_adaybefore = sum/20
diff = sma_adaybefore - sma_today
if diff > 0:
print "Output of SMA20-slope analysis: BUY"
elif diff < 0:
print "Output of SMA20-slope analysis: SELL"
else:
print "SAME SMA20! SMA20-slope test is inconclusive"
print "Starting Bollinger's Analysis..."
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)
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"]
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"]
break
except:
pass