-
Notifications
You must be signed in to change notification settings - Fork 10
/
stock-viewer.py
executable file
·519 lines (455 loc) · 16.1 KB
/
stock-viewer.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#!/usr/bin/env python3
# append to a dataframe a.append(pd.DataFrame({'close':99.99},index=[datetime.datetime.now()])
import matplotlib.pyplot as plt
import sys
import os
import argparse
import datetime
from matplotlib import gridspec
from core.CountryInfo import CountryInfo
from helpers.DataOperations import *
from core.ReportSignals import *
from core.TimeInterval import *
from core.assets import StockAssets
from indicators.StockData import StockData
from helpers.data import toNumIndex
import matplotlib.dates as mdates
from indicators.WilliamsAlligator import CreateWilliamsAlligator
from indicators.macd import CreateMACD
from indicators.rsi import RSI
from indicators.cci import CreateCCI
from indicators.stoch import CreateStoch
from indicators.bollinger import CreateBollinger
from indicators.atr import CreateATR
from indicators.dmi import CreateDMI
from indicators.ichimoku import Ichimoku
from indicators.zigzag import ZigZag
from indicators.cmf import CreateChaikinMoneyFlow
from indicators.moneyflowindex import CreateMoneyFlowIndex
from indicators.trend import trend
# Create plot figures file
def PlotSave(fig):
global graphsCreated
# emf, eps, pdf, png, ps, raw, rgba, svg, svgz
# for ext in [ ".png", ".jpeg", ".svg", ".eps", ".raw"]:
# filePath = outputFilepath + str(fig.number) + ext
# plt.figure(fig.number)
# plt.savefig(filePath)
filePath = outputFilepath + str(fig.number) + outputExtension
plt.figure(fig.number)
plt.savefig(filePath)
graphsCreated.append(filePath)
print('Created plot %s.' % (filePath))
# remove all created plots
def PlotsRemove():
global graphsCreated
for filepath in graphsCreated:
os.system('rm -rf %s' % (filepath))
print('Removed %s.' % (filepath))
def ReportGraphs(f):
# Insert all created graphs
f.write('## Graphs\n\n')
for path in graphsCreated:
f.write('![Graph](%s)\n\n' % (os.path.basename(path)))
f.write('\n')
# Const objects
# #####################################################
lockTimeout = 5 * 60
executionIntervals = ['monthly', 'weekly', 'daily']
reportFile = 'output/report.md'
plotsPath = 'output/'
outputExtension = '.svg'
# Varaables
# #####################################################
# Arguments and config
# #####################################################
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--stockCode', type=str,
required=True, help='Stock name code')
parser.add_argument('-d', '--beginDate', type=str,
required=False, help='Begin date')
parser.add_argument('-Y', '--lastYear', action='store_true',
required=False, help='Last Year')
parser.add_argument('-9M', '--last9Months', action='store_true',
required=False, help='Last 9 Months')
parser.add_argument('-6M', '--last6Months', action='store_true',
required=False, help='Last 6 Months')
parser.add_argument('-3M', '--last3Months', action='store_true',
required=False, help='Last 3 Months')
parser.add_argument('-M', '--lastMonth', action='store_true',
required=False, help='Last Month')
parser.add_argument('-W', '--lastWeek', action='store_true',
required=False, help='Last Week')
parser.add_argument('-g', '--plotToFile', action='store_true',
required=False, help='Plot to file')
parser.add_argument('-r', '--reports', action='store_true',
required=False, help='Generate extra reports')
parser.add_argument('-ri', '--reportsInterval', type=str,
required=False, help='Interval of extra reports')
args = parser.parse_args()
# Assert
if (not args.stockCode):
print('No stockCode!')
sys.exit(1)
# Assert
if (args.reportsInterval is not None):
if (args.reportsInterval not in executionIntervals):
print('Wrong execution interval!')
sys.exit(1)
# Create Country Info
info = CountryInfo(args.stockCode)
# Use non-interactive backend when plot to file used
if (args.plotToFile):
import matplotlib
matplotlib.use('Agg')
# Dates
today = datetime.datetime.now()
# End date
end_date = today.strftime('%Y-%m-%d')
# Start date
if (args.beginDate):
start_date = args.beginDate
else:
start_date = '2010-01-01'
# Check last year
if (args.lastYear):
tmpDate = datetime.datetime.now() - datetime.timedelta(days=365)
start_date = tmpDate.strftime('%Y-%m-%d')
# Check last 9 month
if (args.last9Months):
tmpDate = datetime.datetime.now() - datetime.timedelta(days=30 * 9)
start_date = tmpDate.strftime('%Y-%m-%d')
# Check last 6 month
if (args.last6Months):
tmpDate = datetime.datetime.now() - datetime.timedelta(days=30 * 6)
start_date = tmpDate.strftime('%Y-%m-%d')
# Check last 3 month
if (args.last3Months):
tmpDate = datetime.datetime.now() - datetime.timedelta(days=30 * 3)
start_date = tmpDate.strftime('%Y-%m-%d')
# Check last month
if (args.lastMonth):
tmpDate = datetime.datetime.now() - datetime.timedelta(days=30)
start_date = tmpDate.strftime('%Y-%m-%d')
# Check last Week
if (args.lastWeek):
tmpDate = datetime.datetime.now() - datetime.timedelta(days=7)
start_date = tmpDate.strftime('%Y-%m-%d')
# Dynamic variables
# #####################################################
outputFilename = args.stockCode + '_' + end_date + '_'
outputFilepath = plotsPath + outputFilename
graphsCreated = []
reportSignals = CreateReportSignals()
executionInterval = 'weekly'
# Update - execution Interval
if (args.reportsInterval is not None):
executionInterval = args.reportsInterval
reportSignals.SetBeginTimestamp(GetIntervalBegin(executionInterval))
reportSignals.SetStockCode(args.stockCode)
# Get stock data
# #####################################################
stockAssets = StockAssets()
stockData = StockData(args.stockCode, start_date, end_date)
stockData.SetAssets(stockAssets)
stockData.SetCurrencySymbol(info.GetCurrency())
# Create oscillators/indicators
# #####################################################
closePriceTotal = stockData.GetAllData('Close')
closePrice = stockData.GetData('Close')
alligator = CreateWilliamsAlligator(closePrice)
macd = CreateMACD(closePrice)
rsi = RSI(closePrice)
cci = CreateCCI(stockData.GetData('High'), stockData.GetData(
'Low'), stockData.GetData('Close'))
stoch = CreateStoch(stockData.GetData('High'), stockData.GetData(
'Low'), stockData.GetData('Close'))
bollinger = CreateBollinger(closePrice)
atr = CreateATR(stockData.GetData('High'), stockData.GetData(
'Low'), stockData.GetData('Close'))
dmi = CreateDMI(stockData.GetData('High'),
stockData.GetData('Low'), atr.GetAtr())
ichimoku = Ichimoku(stockData.GetData('Open'),
stockData.GetData('High'),
stockData.GetData('Low'),
stockData.GetData('Close')
)
zigzag = ZigZag(stockData.GetData('Open'),
stockData.GetData('High'),
stockData.GetData('Low'),
stockData.GetData('Close')
)
if (stockData.hasVolume()):
mfi = CreateMoneyFlowIndex(stockData.GetData('High'), stockData.GetData(
'Low'), stockData.GetData('Close'), stockData.GetData('Volume'), info)
cmf = CreateChaikinMoneyFlow(stockData.GetData('High'), stockData.GetData(
'Low'), stockData.GetData('Close'), stockData.GetData('Volume'), info)
# Export all signals to report
alligator.ExportSignals(reportSignals)
macd.ExportSignals(reportSignals)
rsi.ExportSignals(reportSignals)
cci.ExportSignals(reportSignals)
stoch.ExportSignals(reportSignals)
bollinger.ExportSignals(reportSignals)
dmi.ExportSignals(reportSignals)
if (stockData.hasVolume()):
mfi.ExportSignals(reportSignals)
cmf.ExportSignals(reportSignals)
# Add momentum inidicators to stock data
stockData.AddIndicator(rsi)
stockData.AddIndicator(cci)
stockData.AddIndicator(stoch)
if (stockData.hasVolume()):
stockData.AddIndicator(mfi)
# Add trend inidicators to stock data
stockData.AddIndicator(macd)
stockData.AddIndicator(dmi)
if (stockData.hasVolume()):
stockData.AddIndicator(cmf)
# Volume
if (stockData.hasVolume()):
obvTotal = stockData.GetAllData('OBV')
volumeTotal = stockData.GetAllData('Volume')
volume = stockData.GetData('Volume')
obv = stockData.GetData('OBV')
# PLOTS
# #####################################################
fig = plt.figure(figsize=(16.0, 9.0))
Rows = 6
Cols = 5
gs = gridspec.GridSpec(Rows, Cols)
# PLOT 1
# #####################################################
# #####################################################
plot2 = plt.subplot(gs[0:2, :])
stockData.PlotAll(plot2)
stockData.PlotAllAssets(plot2)
plt.ylabel('Price (%s)' % (info.GetCurrency()))
plt.grid()
plt.title('%s - History' % stockData.GetStockCode())
plt.legend(loc='upper left')
# OBV - ALL
if (stockData.hasVolume()):
plot2A = plot2.twinx()
plot2A.plot(toNumIndex(obvTotal.index, obvTotal),
obvTotal, label='OBV total')
plot2A.legend(loc='upper left')
plot2A.tick_params(axis='y', labelcolor='tab:blue')
# Money on market - ALL
if (stockData.hasVolume()):
plot2B = plot2.twinx()
stockData.PlotMoneyOnMarketAll(plot2B)
plot2B.tick_params(axis='y', labelcolor='tab:red')
plt.legend(loc='upper left')
period = 2
upTrends = trend(stockData.GetData('Low'), 'rising')
downTrends = trend(stockData.GetData('High'), 'falling')
# Price OHLC
plot3 = plt.subplot(gs[2:5, :])
bollinger.Plot()
stockData.PlotCandle(plot3)
stockData.PlotAssets()
plt.ylabel('Price (%s)' % (info.GetCurrency()))
plt.grid()
plt.legend(loc='upper left')
# Plot trend lines
upTrends.Plot('green', 'rising', annotate=True)
downTrends.Plot('red', 'falling', annotate=True)
# Add return rates axle
stockData.AddReturnRatesAxle(plot3)
# Stoch
plot4 = plt.subplot(gs[5:6, :], sharex=plot3)
stoch.Plot()
plt.ylabel('Stoch')
plt.grid()
plt.legend(loc='upper left')
plt.xticks(rotation=90)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))
plt.gca().xaxis.set_minor_formatter(mdates.DateFormatter('%d'))
# Plot to file
if (args.plotToFile):
PlotSave(fig)
# PLOT 2
# #####################################################
# #####################################################
fig = plt.figure(figsize=(16.0, 9.0))
Rows = 6
gs = gridspec.GridSpec(Rows, 1)
# Total close price
plot5 = plt.subplot(gs[0:4])
bollinger.Plot()
stockData.PlotCandle(plot5)
# stockData.PlotAssets()
plt.ylabel('Price (%s)' % (info.GetCurrency()))
plt.title('%s - page 1' % stockData.GetStockCode())
plt.legend(loc='upper left')
plt.minorticks_on()
plt.grid(b=True, which='major', axis='both', color='k')
plt.grid(b=True, which='minor', axis='both')
plt.tick_params(axis='x', which='both', bottom=False,
top=False, labelbottom=False)
# Plot trend lines
upTrends.Plot('green', 'rising', 0.6, annotate=True)
downTrends.Plot('red', 'falling', 0.6, annotate=True)
# Add return rates axle
stockData.AddReturnRatesAxle(plot5)
# MACD
plot6 = plt.subplot(gs[Rows - 2], sharex=plot5)
macd.Plot()
macd.Histogram()
plt.ylabel('Value')
plt.grid()
plt.legend(loc='upper left')
plt.tick_params(axis='x', which='both', bottom=False,
top=False, labelbottom=False)
# RSI
plot8 = plt.subplot(gs[Rows - 1], sharex=plot5)
rsi.Plot()
plt.ylabel('RSI')
plt.grid()
plt.legend(loc='upper left')
plt.xticks(rotation=90)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))
plt.gca().xaxis.set_minor_formatter(mdates.DateFormatter('%d'))
# Plot to file or show
if (args.plotToFile):
PlotSave(fig)
# PLOT 3
# #####################################################
# #####################################################
fig = plt.figure(figsize=(16.0, 9.0))
# Bollinger with candleplot
Rows = 6
gs = gridspec.GridSpec(Rows, 1)
plot9 = plt.subplot(gs[0:4])
bollinger.Plot()
stockData.PlotCandle(plot9)
stockData.PlotAssets()
plt.ylabel('Price (%s)' % (info.GetCurrency()))
plt.title('%s - page 2' % stockData.GetStockCode())
plt.legend(loc='upper left')
plt.minorticks_on()
plt.grid(b=True, which='major', axis='both', color='k')
plt.grid(b=True, which='minor', axis='both')
plt.tick_params(axis='x', which='both', bottom=False,
top=False, labelbottom=False)
# Plot trend lines
upTrends.Plot('green', 'rising', 0.6, annotate=True)
downTrends.Plot('red', 'falling', 0.6, annotate=True)
# Add return rates axle
stockData.AddReturnRatesAxle(plot9)
# ATR
plot10 = plt.subplot(gs[Rows - 2], sharex=plot9)
# atr.Plot()
dmi.Plot()
plt.legend(loc='upper left')
plt.grid()
plt.tick_params(axis='x', which='both', bottom=False,
top=False, labelbottom=False)
# CCI
plot11 = plt.subplot(gs[Rows - 1], sharex=plot9)
cci.Plot()
plt.legend(loc='upper left')
plt.grid()
plt.xticks(rotation=90)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))
plt.gca().xaxis.set_minor_formatter(mdates.DateFormatter('%d'))
# bollinger.PlotAbsDeviation()
# plt.minorticks_on()
# plt.grid(b=True, which='major', axis='both',color='k')
# plt.grid(b=True, which='minor', axis='both')
# Plot to file or show
if (args.plotToFile):
PlotSave(fig)
if (stockData.hasVolume()):
# PLOT 4
# #####################################################
# #####################################################
fig = plt.figure(figsize=(16.0, 9.0))
# Create rows
Rows = 6
gs = gridspec.GridSpec(Rows, 1)
plot9 = plt.subplot(gs[0:4])
# Bollinger with candleplot
bollinger.Plot()
stockData.PlotCandle(plot9)
stockData.PlotAssets()
plt.ylabel('Price (%s)' % (info.GetCurrency()))
plt.title('%s - page 3' % stockData.GetStockCode())
plt.legend(loc='upper left')
plt.minorticks_on()
plt.grid(b=True, which='major', axis='both', color='k')
plt.grid(b=True, which='minor', axis='both')
plt.tick_params(axis='x', which='both', bottom=False,
top=False, labelbottom=False)
# MoM
plot9A = plot9.twinx()
stockData.PlotMoneyOnMarket(plot9A)
plot9.tick_params(axis='y', labelcolor='tab:red')
plt.ylabel('Money on the market (%s)' % (info.GetCurrency()))
plt.legend(loc='upper left')
# CMF
plot10 = plt.subplot(gs[Rows - 2], sharex=plot9)
# cmf.PlotChaikinMoneyFlow()
cmf.PlotChaikinOscillator()
plt.legend(loc='upper left')
plt.grid()
plt.tick_params(axis='x', which='both', bottom=False,
top=False, labelbottom=False)
# MFI
plot11 = plt.subplot(gs[Rows - 1], sharex=plot9)
mfi.Plot()
plt.legend(loc='upper left')
plt.grid()
# Plot to file or show
if (args.plotToFile):
PlotSave(fig)
# PLOT 5
# #####################################################
# #####################################################
fig = plt.figure(figsize=(16.0, 9.0))
plot9 = plt.subplot(1, 1, 1)
stockData.PlotAssets()
ichimoku.Plot(plot9)
stockData.PlotCandle(plot9)
# zigzag.Plot(plot9)
plt.ylabel('Price (%s)' % (info.GetCurrency()))
plt.title('%s - page 2' % stockData.GetStockCode())
plt.legend(loc='upper left')
plt.minorticks_on()
plt.grid(b=True, which='major', axis='both', color='k')
plt.grid(b=True, which='minor', axis='both')
# Add return rates axle
stockData.AddReturnRatesAxle(plot9)
# Plot to file or show
if (args.plotToFile):
PlotSave(fig)
# Create reports
if (args.reports):
reportAllSignalTypes = False
# If there are opened assets then report all signals
if (len(stockAssets.GetAssetsForStockCode(args.stockCode, onlyOpened=True)) != 0):
reportAllSignalTypes = True
# Daily report
if (executionInterval == 'daily'):
reportSignals.Report(reportFile, reportAllSignalTypes)
# If signals reported
if (reportSignals.reportedAnything is True) or (True):
with open(reportFile, 'a+') as f:
stockData.Report(f, executionInterval)
stockData.ReportAssets(f)
ReportGraphs(f)
# remove plots if nothing reported
else:
PlotsRemove()
# Weekly report
else:
with open(reportFile, 'a+') as f:
stockData.Report(f, executionInterval)
reportSignals.Report(reportFile, reportAllSignalTypes)
with open(reportFile, 'a+') as f:
stockData.ReportAssets(f)
ReportGraphs(f)
# Show all plots
if (not args.plotToFile):
plt.show()