-
Notifications
You must be signed in to change notification settings - Fork 21
/
TD_plotter.py
executable file
·436 lines (387 loc) · 15.7 KB
/
TD_plotter.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env python3
import os
import requests
import json
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.finance import candlestick_ohlc as candlestick
from matplotlib import style
import re
import pandas as pd
import numpy as np
#import historical price data
dat = pd.DataFrame(pd.read_csv('price_data.csv'))
#rename columns
dat.columns = ['Date','Open','High','Low',
'Close','Volume','MarketCap']
#reverse dataFrame order for all cols along index axis
data = dat.iloc[::-1]
#fetch current price
def getPrice():
global btcPrice
#bitstamp = 'https://bitstamp.net/api/ticker/'
cmc = 'https://api.coinmarketcap.com/v1/ticker/bitcoin/'
try:
req = requests.get(cmc)
price = float(json.loads(req.text)[0]['price_usd'])
btcPrice = "%.2f" % price
#if unable to connect, use latest data from spreadsheet
except requests.ConnectionError:
btcPrice = data.iloc[-1]['Close']
def main():
#prompt user for number of candles to display
print('\n\nYou have ' + str(len(data)-51) +
' candles available to view.\nEnter "0" to view all.\n')
while True:
toShow = raw_input('How many candles to display? ')
if toShow == "0":
toShow = int(len(data)-51)
print("\tGenerating plot now...")
break
elif not re.match('^[0-9]+$',toShow):
print("\tPositive integers only please and thank you.\n")
elif int(toShow) < 52 and int(toShow) > 0:
print("\tCannot display less than 52 candles.\n")
elif int(toShow) > int(len(data)-51):
print("\tCannot display more than " +
str(len(data)-51) + " candles.\n")
else:
toShow = int(toShow)
print("\tGenerating plot now...")
break
#most recent price from data
last = data.iloc[-1]['Close']
#fetch current price
getPrice()
#make arrays of open, high, low, close, date, and volume
o=[data.iloc[j]['Open'] for j in range(0, len(data))]
h=[data.iloc[j]['High'] for j in range(0, len(data))]
l=[data.iloc[j]['Low'] for j in range(0, len(data))]
c=[data.iloc[j]['Close'] for j in range(0, len(data))]
t=[data.iloc[j]['Date'] for j in range(0, len(data))]
v=[data.iloc[j]['Volume'] for j in range(0, len(data))]
#scale down volume array values so they will
#fit nicely on a graph without too much fuss.
vol=[]
for i in range(0,len(data)):
vol.append(float(v[i]) / (9**7))
vol[i] = float(round(vol[i],3))
#replace the last close price with current price
c[-1] = float(btcPrice)
#Below various arrays are generated for TD indicator values.
#They will (and must) be the same length as any data column
#(e.g., open, low, volume, etc.).
#In order for the long/short count to progress beyond 0,
#It must be preceeded by a bullish/bearish price flip.
#In the absence of such a flip, the count, if 0,
#remains at 0 regardless of other conditions.
#If count > 0, a price flip is not required for the count to progress.
shortVal=[]
longVal =[]
shortCount = 0
longCount = 0
for k in range(0,len(c)): #generate TD buy/sell setups
if k <= 3:
longCount = 0
shortCount = 0
if shortCount > 8:
shortCount = 0
if longCount > 8:
longCount = 0
if k > 3:
if c[k] < c[k-4]:
longCount+=1
else:
longCount = 0
if c[k] > c[k-4]:
shortCount+=1
else:
shortCount = 0
longVal.append(longCount)
shortVal.append(shortCount)
buyVal =[]
sellVal=[]
buyCount=0
sellCount=0
for y in range(0,len(c)): #generate TD buy countdown
if y < 11:
buyCount = 0
if y>= 11:
if buyCount == 0:
if h[y] >= l[y-3] or h[y] >= l[y-4] or h[y] >= l[y-5] or h[y] >= l[y-6] or h[y] >= l[y-7]:
if 8 in longVal[y-16:y] or 9 in longVal[y-15:y]:
if c[y] < l[y-2]:
buyCount += 1
if buyVal[-1] == 13 or shortVal[y] > 8:
buyCount = 0
if buyCount != 0:
if c[y] < l[y-2]:
buyCount += 1
if longVal[y] == 9:
buyCount = 0
buyVal.append(buyCount)
for y in range(0,len(c)): #generate TD sell countdown
if y < 11:
sellCount = 0
if y>= 11:
if sellCount == 0:
if l[y] <= h[y-3] or l[y] <= h[y-4] or l[y] <= h[y-5] or l[y] <= h[y-6] or l[y] <= h[y-7]:
if 8 in shortVal[y-16:y] or 9 in shortVal[y-15:y]:
if c[y] > h[y-2]:
sellCount = 1
if sellVal[-1] == 13 or longVal[y] > 8:
sellCount = 0
if sellCount != 0:
if c[y] > h[y-2]:
sellCount += 1
if shortVal[y] == 9:
sellCount = 0
sellVal.append(sellCount)
agbuyVal =[]
agsellVal=[]
agbuyCount=0
agsellCount=0
for y in range(0,len(c)): #generate aggressive TD buy countdown
if y < 11:
agbuyCount = 0
if y>= 11:
if agbuyCount == 0:
if h[y] >= l[y-3] or h[y] >= l[y-4] or h[y] >= l[y-5] or h[y] >= l[y-6] or h[y] >= l[y-7]:
if 8 in longVal[y-16:y] or 9 in longVal[y-15:y]:
if l[y] < l[y-2]:
agbuyCount += 1
if agbuyVal[-1] == 13 or shortVal[y] > 8:
agbuyCount = 0
if agbuyCount != 0:
if l[y] < l[y-2]:
agbuyCount += 1
if longVal[y] == 9:
agbuyCount = 0
agbuyVal.append(agbuyCount)
for y in range(0,len(c)): #generate aggressive TD sell countdown
if y < 11:
agsellCount = 0
if y>= 11:
if agsellCount == 0:
if l[y] <= h[y-3] or l[y] <= h[y-4] or l[y] <= h[y-5] or l[y] <= h[y-6] or l[y] <= h[y-7]:
if 8 in shortVal[y-16:y] or 9 in shortVal[y-15:y]:
if h[y] > h[y-2]:
agsellCount = 1
if agsellVal[-1] == 13 or longVal[y] > 8:
agsellCount = 0
if agsellCount != 0:
if h[y] > h[y-2]:
agsellCount += 1
if shortVal[y] == 9:
agsellCount = 0
agsellVal.append(agsellCount)
#read in the output of script "other_indicators.py"
try:
os.system("./other_indicators.py") #Linux
except:
os.system("python other_indicators.py") #Windows
#the csv files below are generated by the script 'other_indicators.py'
mas = pd.read_csv("mas.csv")
emas = pd.read_csv("emas.csv")
#get Moving Average values
tenDay =[mas.iloc[i]["ten"] for i in range(0,len(mas))]
twelveDay =[mas.iloc[i]["twelve"] for i in range(0,len(mas))]
twentysixDay =[mas.iloc[i]["twentysix"] for i in range(0,len(mas))]
thirtyDay =[mas.iloc[i]["thirty"] for i in range(0,len(mas))]
fiftyDay =[mas.iloc[i]["fifty"] for i in range(0,len(mas))]
sixtyDay =[mas.iloc[i]["sixty"] for i in range(0,len(mas))]
onetwentyDay =[mas.iloc[i]["onetwenty"] for i in range(0,len(mas))]
#get Exponential Moving Average values
tenEMA =[emas.iloc[i]["ten"] for i in range(0,len(mas))]
twelveEMA =[emas.iloc[i]["twelve"] for i in range(0,len(mas))]
twentysixEMA =[emas.iloc[i]["twentysix"] for i in range(0,len(mas))]
thirtyEMA =[emas.iloc[i]["thirty"] for i in range(0,len(mas))]
fiftyEMA =[emas.iloc[i]["fifty"] for i in range(0,len(mas))]
sixtyEMA =[emas.iloc[i]["sixty"] for i in range(0,len(mas))]
onetwentyEMA =[emas.iloc[i]["onetwenty"] for i in range(0,len(mas))]
#get macd + macd histo values
#scale values up to make them more easily visible in plot
macd = [emas.iloc[i]["macdHisto"] * 2.5 for i in range(0,len(emas))]
twentyeightDayVol=[emas.iloc[i]["twentyeightDayVol"] for i in range(0,len(emas))]
#truncate data sets based on user input for number of candles
d = len(c) - toShow
if d > 1:
del o[:d]
del h[:d]
del l[:d]
del c[:d]
del t[:d]
del vol[:d]
del longVal[:d]
del shortVal[:d]
del buyVal[:d]
del agbuyVal[:d]
del sellVal[:d]
del agsellVal[:d]
del tenDay[:d]
del twelveDay[:d]
del twentysixDay[:d]
del thirtyDay[:d]
del fiftyDay[:d]
del sixtyDay[:d]
del onetwentyDay[:d]
del tenEMA[:d]
del twelveEMA[:d]
del twentysixEMA[:d]
del thirtyEMA[:d]
del fiftyEMA[:d]
del sixtyEMA[:d]
del onetwentyEMA[:d]
del macd[:d]
del twentyeightDayVol[:d]
idx_min = int(np.argmin(l)) #find local low
idx_max = int(np.argmax(h)) #find local high
#create open,high,low,close array to be used in candlestick below
x=0
ohlc=[]
while x < len(l):
append_it = x, o[x], h[x], l[x], c[x]
ohlc.append(append_it)
x+=1
#begin plot code...
style.use('fivethirtyeight')
fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0)) #((dimensions), (define origin))
#display only every 3rd x-axis label
ax1.xaxis.set_ticks(np.arange(0,len(o),3.0))
a = ax1.get_xticks().tolist()
for p in range(0, len(a)):
a[p] = t[p * 3]
ax1.set_xticklabels(a)
#rotate x-axis labels to make them fit on screen
ticks = ax1.get_xticklabels()
for label in ticks:
label.set_rotation(60) #units here are degrees
label.set_fontsize(8)
#plot title, axes labels, axes scales
plt.title("BTC/USD, Daily")
plt.xlim(-0.55, len(o) + 0.01)
plt.ylim(-1200, h[idx_max] * 1.14)
#show a grid on plot
ax1.grid(True,color='#2E2E2E',linestyle='--')
#generate candlesticks
lines, patches = candlestick(ax1,ohlc,width=0.6,colorup='g',colordown='r')
for line, patch in zip(lines, patches):
line.set_color('#848484')
line.set_linewidth(1.1)
patch.set_edgecolor('#1C1C1C')
patch.set_linewidth(0)
patch.set_antialiased(False)
patch.set_zorder(3) #put candles behind TD indicator values
#display td countdown values
for z in range(0,len(c)):
if int(sellVal[z]) > 10 and int(sellVal[z]) != int(sellVal[z-1]):
ax1.annotate(str(sellVal[z]), xy = (z-0.48, h[z]*1.01),
xytext = (z-0.48, h[z]*1.01), size=11,
color='#FE642E', weight='bold')
if int(buyVal[z]) > 10 and int(buyVal[z]) != int(buyVal[z-1]):
ax1.annotate(str(buyVal[z]), xy = (z-0.4, l[z]/1.005),
xytext = (z-0.4, l[z]/1.005), size=11,
color='#A9F5A9', weight='bold')
#insert arrows when countdown completes
if sellVal[z] == 13:
plt.arrow(z, h[z]*1.07, 0.0, -150, fc="#FE642E",#light red
ec="#FE642E", head_width=0.8, head_length=150)
if buyVal[z] == 13:
plt.arrow(z, l[z]/1.07, 0.0, 150, fc="#A9F5A9",#light green
ec="#A9F5A9", head_width=0.8, head_length=150)
#display td aggressive countdown values
for z in range(0,len(c)):
if int(agsellVal[z]) > 10 and int(agsellVal[z]) != int(agsellVal[z-1]):
ax1.annotate(str(agsellVal[z]), xy = (z-0.45, l[z]/1.01),
xytext = (z-0.45, l[z]/1.02), size=12,
color='#DF0101', weight='bold')
if int(agbuyVal[z]) > 10 and int(agbuyVal[z]) != int(agbuyVal[z-1]):
ax1.annotate(str(agbuyVal[z]), xy = (z-0.4, l[z]*1.0089),
xytext = (z-0.4, h[z]*1.02), size=12,
color='#01DF01', weight='bold')
#insert arrows when countdown completes
if agsellVal[z] == 13:
plt.arrow(z, l[z]/1.07, 0.0, -100, fc="red",
ec="red", head_width=0.8, head_length=150)
if agbuyVal[z] == 13:
plt.arrow(z, h[z]*1.07, 0.0, 100, fc="green",
ec="green", head_width=0.8, head_length=150)
#display td setup values
for r in range(0,len(c)):
if int(shortVal[r]) > 0:
ax1.annotate(str(shortVal[r]), xy = (r, h[r]/1.0004),
xytext = (r-0.13, h[r]/1.0004), color='#FA5858',
size=10,weight='bold')
if int(longVal[r]) > 0:
ax1.annotate(str(longVal[r]), xy = (r, l[r]*1.0004),
xytext = (r-0.13, l[r]*1.0004), color='#2EFE64',
size=10,weight='bold')
#insert arrows when setup completes
if shortVal[r] == 9:
plt.arrow(r, h[r]*1.04, 0.0, -100, fc="#FA5858",
ec="#FA5858", head_width=0.8, head_length=150)
if longVal[r] == 9:
plt.arrow(r, l[r]/1.08, 0.0, 100, fc="#58FA58",
ec="#58FA58", head_width=0.8, head_length=150)
#display the latest price next to final candle
if btcPrice < last:
ax1.annotate(btcPrice, xy = (x, float(btcPrice)),
xytext= (x-0.1, float(btcPrice)), color='#FE2E2E',
size=10, weight='bold')
elif btcPrice >= last:
ax1.annotate(btcPrice, xy = (x, float(btcPrice)),
xytext= (x-0.1, float(btcPrice)), color='#088A29',
size=10, weight='bold')
#display the highest and lowest prices for the time interval specified
#ax1.annotate("L: $" + str(l[idx_min]), xy = (0, h[idx_max]*0.9),
# xytext= (0, h[idx_max]*0.9), color='#FE2E2E', size=10, weight='bold')
#ax1.annotate("H: $" + str(h[idx_max]), xy = (0, h[idx_max]*0.98),
# xytext= (0, h[idx_max]*0.98), color='#2EFE2E', size=10, weight='bold')
r = np.arange(len(macd))
"""
COMMENT/UNCOMMENT ANNOTATE-PLOT PAIRS BELOW TO ADD/REMOVE
MOVING AVERAGES AND EXPONENTIAL MOVING AVERAGES
"""
ax1.annotate("10 day EMA", xy = (r[-1]/1.1, h[idx_max]*1.02),
xytext= (r[-1]/1.1, h[idx_max]*1.02),
color='purple', size=10, weight='bold')
ax1.plot(r,tenEMA, color="purple", linewidth="1.5")
ax1.annotate("30 day EMA", xy = (r[-1]/1.1, h[idx_max]*0.98),
xytext= (r[-1]/1.1, h[idx_max]*0.98),
color='blue', size=10, weight='bold')
ax1.plot(r,thirtyEMA, color="blue", linewidth="1.5")
#ax1.annotate("50 day EMA", xy = (r[-1]/1.1, h[idx_max]*0.94),
# xytext= (r[-1]/1.1, h[idx_max]*0.94),
# color='green', size=10, weight='bold')
#ax1.plot(r,fiftyEMA, color="green", linewidth="1.5")
#ax1.annotate("10 day MA", xy = (r[-1]/1.1, h[idx_max]*0.90),
# xytext= (r[-1]/1.1, h[idx_max]*0.90),
# color='red', size=10, weight='bold')
#ax1.plot(r,tenDay, color="red",linewidth="1.5")
#ax1.annotate("30 day MA", xy = (r[-1]/1.1, h[idx_max]*0.86),
# xytext= (r[-1]/1.1, h[idx_max]*0.96),
# color='orange', size=10, weight='bold')
#ax1.plot(r,thirtyDay, color="orange",linewidth="1.5")
#ax1.annotate("50 day MA", xy = (r[-1]/1.1, h[idx_max]*0.82),
# xytext= (r[-1]/1.1, h[idx_max]*0.82),
# color='yellow', size=10, weight='bold')
#ax1.plot(r,fiftyDay, color="yellow",linewidth="1.5")
"""
COMMENT/UNCOMMENT NEXT 2 LINES TO TOGGLE VOLUME INDICATOR ON/OFF
"""
plt.plot(r,twentyeightDayVol, color="r", linewidth="1.5")
ax1.bar(r, vol, color="white")
"""
COMMENT/UNCOMMENT TO TOGGLE MACD INDICATOR ON/OFF
"""
ax1.bar(r, macd, color="blue")
ax1.set_facecolor('#000000')
#adjust plot size to fit screen
plt.subplots_adjust(left=0.06,right=0.94,bottom=0.15,
top=0.94,wspace=0.20,hspace=0.20)
#make chart full screen by default
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
plt.show()
main()