Monday, December 19, 2022

Pandas Datareader Library is Down

Pandas Datareader library is down since December 16, 2022. I use it to get stock market historical data. This has happened before when it was my first attempt to download historical data. I figured to leave the task and go on with the next task and came after a few days and it went back to normal that is why this recent event is not a surprise for me. 

I like using this library because it is very useful and very simple to use but the only downside is the downtime is quite long, the developers are not able to fix it quickly. It has been 3 days and it still unuseable so I have to try its twin library(YFinance) if it still works. The output and features of this library is very much identical to Pandas Datareader but with a few other options like the ticker info(which is also currently down). And speaking of the ticker info method, this feature of yfinace is very slow. Way... way too slow.

In my trading platform program, I currently use yfinace ticker info to display some information about the ticker(obviously) but it has no use for me so probably that feature is needed by institutional investors who must have to analyze the ticker(company) from ground up which leads me to decide to just remove it from my program since I am not an institutional investor. And I also use the panda data reader to get historical data of the ticker which is currently down and maybe who knows when will it be up. So it is a huge problem for me because both yfnance ticker info is slow and currently down (not really, I noticed that it still works but a lot of the fields that I am using had disappeared and I do not know why) and the pandas data reader is down.

As my first option, I should to try if yfinace(before exploring other alternatives) function or methods to get the historical data are still working and I am glad that with the result of my experiments, it still works. So for those who have downloaded/copied my program, I am posting the latest version of my program and for those who want to try, this is the latest version:

  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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
import sys
from PyQt6.QtWidgets import QApplication,  QWidget, QPushButton
from PyQt6.QtWidgets import QApplication,  QWidget, QDateEdit, QLabel, QComboBox, QTableWidget, QStyledItemDelegate, QTableWidgetItem, QHeaderView
from PyQt6.QtCore import  QDate, QEvent, QObject, Qt
from PyQt6.QtGui import  QPainter, QColor, QPen, QFont
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
import pandas_datareader as web
import pandas as pd
import datetime as dt
import yfinance as yf
import pandas_ta as ta
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM

data = []
combo1 =''
ticker_info = {}
class Delegate(QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        if index.data() == "100":
            return super(Delegate, self).createEditor(parent, option, index)

class Window(QWidget):

    def __init__(self):
        super(Window, self).__init__()

        self.initUI()

    def initUI(self):
        global data, combo1, ticker_info
        self.combo_box = QComboBox(self)
        self.combo_box.setGeometry(125, 15, 125, 20)
        self.combo_box.addItem("AAPL")
        self.combo_box.addItem("TSLA")
        self.combo_box.addItem("AMZN")
        self.combo_box.addItem("META")
        self.combo_box.addItem("GOOG")
        self.combo_box.addItem("NFLX")
        self.combo_box.addItem("TWTR")
        self.combo_box.addItem("BTC")
        #ticker_info = yf.Ticker("AAPL")
        self.company_name = QLabel(self)
        self.company_name.setGeometry(400, 15, 807, 20)
        yf.pdr_override()
        #print((ticker_info.info))
        #self.company_name.setText("   Company Name: " + ticker_info.info['longName'] + "        Sector: " + ticker_info.info['sector'] + "        Employees: " + str(ticker_info.info['fullTimeEmployees']) + "        Cash On Hand: " + str(ticker_info.info['totalCash']) + "        Debt: " + str(ticker_info.info['totalDebt']))

        self.ML_button = QPushButton('Show Tommorrows Trend', self)
        self.ML_button.setGeometry(1250, 15, 150, 30)
        self.ML_button.clicked.connect(self.onClick_ML_button)
        

        self.ML_result = QLabel(self)
        self.ML_result.setGeometry(1265, 54, 110, 35)
        self.ML_result.setFont(QFont("Arial", 24))
        self.ML_result.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        
        self.ML_hist = QLabel(self)
        self.ML_hist.setGeometry(1265, 100, 110, 20)
        self.ML_hist.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        
        self.combo_box.activated.connect(self.on_combobox_changed)
        self.combo_box_label = QLabel(self)
        self.combo_box_label.setGeometry(25, 15, 100, 20)
        self.combo_box_label.setText("Select Ticker:")
        self.combo_box_label_s = QLabel('AAPL',self)
        self.combo_box_label_s.setGeometry(255, 15, 100, 20)
        sdate= '01-01-2021'
        date_str = "1-Jan-2021"
        qdate = QDate.fromString(date_str, "d-MMM-yyyy")
        self.date_edit = QDateEdit(self, date=qdate, calendarPopup=True)
        self.date_edit.setGeometry(125, 45, 100, 20)
        self.date_edit.dateChanged.connect(self.update)
        self.date_label = QLabel('Set Date', self)
        self.date_label.setGeometry(25, 45, 150, 20)
        self.result_label = QLabel('to', self)
        self.result_label.setGeometry(235, 45, 20, 20)
        self.date_edit1 = QDateEdit(self, date=QDate.currentDate(), calendarPopup=True)
        self.date_edit1.setGeometry(265, 45, 100, 20)
        self.date_edit1.dateChanged.connect(self.update)
        company = 'AAPL'
        start = dt.datetime(2021, 1, 1)
        end = dt.datetime.now()
        combo1 = self.combo_box.currentText()
        data = yf.download("AAPL", start=start, end=end)
        #data = pd.read_csv("tsla.csv")
        #yf.pdr_override()
        #data = web.get_data_yahoo(symbols=[company], start=start, end=end)
        #data = web.DataReader(company, 'yahoo', start, end)
        m = PlotCanvas(self, width=9, height=2)
        m.move(35,145)
        m1 = PlotCanvas1(self, width=9, height=2)
        m1.move(965,145)
        m2 = PlotCanvas2(self, width=9, height=2)
        m2.move(965,360)
        m3 = PlotCanvas3(self, width=9, height=2)
        m3.move(965,580 )
        m4 = PlotCanvas4(self, width=9, height=2)
        m4.move(965,790)
        m5 = PlotCanvas5(self, width=9, height=2)
        m5.move(35,360)
        m6 = PlotCanvas6(self, width=9, height=2)
        m6.move(35,580)        
        m7 = PlotCanvas7(self, width=9, height=2)
        m7.move(35,790)
        #self.createTable()
        
        self.setGeometry(10, 35, 1900, 1000)        
        
        self.setWindowTitle('My Trading Platform')
        self.show()
    def onClick_ML_button(self):
        
        company = self.combo_box.currentText()
 
        start = dt.datetime(2021, 1, 1)
        end = dt.datetime.now()
        ml_data = yf.download(company, start=start, end=end)
        #ml_data = web.DataReader(company, 'yahoo', start, end)
        #ml_data = pd.read_csv("tsla.csv")
 
        scaler = MinMaxScaler(feature_range=(0,1))
        scaled_data = scaler.fit_transform(ml_data["Close"].values.reshape(-1,1))
        prediction_days = 60
        x_train = []
        y_train = []
 
        for x in range(prediction_days, len(scaled_data)):
            x_train.append(scaled_data[x-prediction_days:x,0])
            y_train.append(scaled_data[x, 0])
     
        x_train, y_train = np.array(x_train), np.array(y_train)
        x_train = np.reshape(x_train,(x_train.shape[0], x_train.shape[1],1))

        model = Sequential()
        model.add(LSTM(units=50, return_sequences='TRUE', input_shape=(x_train.shape[1], 1)))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50, return_sequences='TRUE'))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50))
        model.add(Dropout(0.2))
        model.add(Dense(units=1))
        model.compile(optimizer='adam', loss='mean_squared_error')
        model.fit(x_train, y_train, epochs=128, batch_size=128)
 
        test_start = dt.datetime(2021, 6, 1)
        test_end = dt.datetime.now()
        test_data = web.DataReader(company, 'yahoo', test_start, test_end)
        actual_prices = test_data['Close'].values
        total_dataset = pd.concat((data['Close'], test_data['Close']), axis=0)
        model_inputs = total_dataset[len(total_dataset) - len(test_data) - prediction_days:].values
        model_inputs = model_inputs.reshape(-1, 1)
        model_inputs = scaler.transform(model_inputs)
        x_test = []

        for x in range(prediction_days, len(model_inputs)):
            x_test.append(model_inputs[x-prediction_days:x, 0])

        x_test = np.array(x_test)
        x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
        predicted_prices = model.predict(x_test)
        predicted_prices = scaler.inverse_transform(predicted_prices)
        real_data = [model_inputs[len(model_inputs) - prediction_days:len(model_inputs+1), 0]]
        real_data = np.array(real_data)
        real_data = np.reshape(real_data, (real_data.shape[0], real_data.shape[1], 1))
        prediction = model.predict(real_data)
        prediction = scaler.inverse_transform(prediction)
        dast1 = data[-1:]['Close']

        v = float(dast1 - float(prediction))
        if v>1:
            self.ML_result.setText("BUY")
        else:
            self.ML_result.setText("SELL")
        print(company)
        self.ML_hist.setText("ML Accuracy: 100%")
    def createTable(self):
        global ticker_info
        self.tableWidget = QTableWidget(self)
        self.tableWidget.viewport().installEventFilter(self)
        #self.tableWidget.installEventFilter(self)
        #self.tableWidget.setEditTriggers(QTreeView.NoEditTriggers) 
        self.tableWidget.setRowCount(2)
        self.tableWidget.setColumnCount(8)
        self.tableWidget.setFixedSize(820, 80)
        self.tableWidget.move(400, 45)
        delegate = Delegate(self.tableWidget)
        self.tableWidget.setItemDelegate(delegate)
        #print(dir(QHeaderView))
        self.tableWidget.setColumnWidth(0, 150)
        text = 'averageDailyVolume(10D)'
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(0, 0, it)        
        text = str(ticker_info.info['averageDailyVolume10Day'])
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(1, 0, it) 
        
        self.tableWidget.setColumnWidth(1, 150)
        text = 'averageVolume(10D)'
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(0, 1, it)        
        text = str(ticker_info.info['averageVolume10days'])
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(1, 1, it) 
        
        self.tableWidget.setColumnWidth(2, 150)
        text = 'marketCap'
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(0, 2, it)        
        text = str(ticker_info.info['marketCap'])
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(1, 2, it)
        
        self.tableWidget.setColumnWidth(3, 150)
        text = 'twoHundredDayAverage'
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(0, 3, it)        
        text = str(ticker_info.info['twoHundredDayAverage'])
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(1, 3, it)
        
        self.tableWidget.setColumnWidth(4, 100)
        text = 'pegRatio'
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(0, 4, it)        
        text = str(ticker_info.info['pegRatio'])
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(1, 4, it)
        
        self.tableWidget.setColumnWidth(5, 150)
        text = 'volume'
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(0, 5, it)        
        text = str(ticker_info.info['volume'])
        it = QTableWidgetItem(text)
        self.tableWidget.setItem(1, 5, it)
        
        self.tableWidget.verticalHeader().hide()
        self.tableWidget.horizontalHeader().hide()
        
    def eventFilter(self, source, event):
        #if self.tableWidget.selectedIndexes() != []:
            
        if event.type() == QEvent.Type.MouseButtonRelease:
 
            row = self.tableWidget.currentRow()
            col = self.tableWidget.currentColumn()
            if self.tableWidget.item(row, col) is not None:
                print(str(row) + " " + str(col) + " " + self.tableWidget.item(row, col).text())
            else:
                print(str(row) + " " + str(col))            
  
       
        return QObject.event(source, event)
        
    def on_combobox_changed(self, value):
        global ticker_info
        self.combo_box_label_s.setText(self.combo_box.currentText())
        #if self.combo_box.currentText() != 'BTC':
        #    ticker_info = yf.Ticker(self.combo_box.currentText())
        #    self.company_name.setText("   Company Name: " + ticker_info.info['longName'] + "        Sector: " + ticker_info.info['sector'] + "        Employees: " + str(ticker_info.info['fullTimeEmployees']) + "        Cash On Hand: " + str(ticker_info.info['totalCash']) + "        Debt: " + str(ticker_info.info['totalDebt']))
        
        self.update_chart()        
    def update(self):
        value = self.date_edit.date()
        
        #self.result_label.setText(str(value.toPyDate()))
        self.update_chart()
    def update_chart(self): 
        global  data, combo1
        combo1 = self.combo_box.currentText()
        value = self.date_edit.date()
        value = (str(value.toPyDate()))
        company = self.combo_box.currentText()
        start = dt.datetime(int(value[:4]), int(value[5:7]), int(value[-2] + value[-1]))
        value = self.date_edit1.date()
        value = (str(value.toPyDate()))
        #print(value)
        #print((value[:4]), (value[5:7]), (value[-2] + value[-1]))
        end = dt.datetime(int(value[:4]), int(value[5:7]), int(value[-2] + value[-1]))
        #start = dt.datetime(2021, 1, 1)
        #end = dt.datetime.now()
        #data = web.DataReader(company, 'yahoo', start, end)
        data = yf.download(company, start=start, end=end)
        m = PlotCanvas(self, width=9, height=2)
        
        
        m.move(35,145)
        m.show()
        m1 = PlotCanvas1(self, width=9, height=2)
        m1.move(965,145)
        m1.show()
        m2 = PlotCanvas2(self, width=9, height=2)
        m2.move(965,360)
        m2.show()
        m3 = PlotCanvas3(self, width=9, height=2)
        m3.move(965,580 )
        m3.show()
        m4 = PlotCanvas4(self, width=9, height=2)
        m4.move(965,790)
        m4.show()
        m5 = PlotCanvas5(self, width=9, height=2)
        m5.move(35,360)
        m5.show()
        m6 = PlotCanvas6(self, width=9, height=2)
        m6.move(35,580)   
        m6.show()
        m7 = PlotCanvas7(self, width=9, height=2)
        m7.move(35,790)   
        m7.show()
        
class PlotCanvas(FigureCanvas):
    
    def __init__(self, parent=None, width=15, height=2, dpi=100):
        
        global combo1
        fig = Figure(figsize=(width, height), dpi=dpi)
        fig.suptitle(f'{combo1} Price Chart', fontsize=10)
        #fig.supxlabel('X title', fontsize=10)
        #fig.supylabel('Y title', fontsize=10)
        
        FigureCanvas.__init__(self, fig)
        
        self.setParent(parent)
        
        FigureCanvas.updateGeometry(self)
        
        self.plot()
        
    def plot(self):
        
        global data
        x = data.index
 
        y = data['Close']
        ax = self.figure.add_subplot(111)
        #ax.set_ylim(ymax=450)
        #ax.set_ylim(bottom=100)
        #print(dir(ax))
        #ax.set_xlabel('Test Cases')
        #ax.set_ylabel('fault courage')
        ax.plot(x,y)
        #ax.plot(y,z)
        
        #ax.plot(y,a)
       
        self.draw()
        self.flush_events()
 
class PlotCanvas1(FigureCanvas):

    def __init__(self, parent=None, width=15, height=2, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        fig.suptitle('MACD', fontsize=10)
        #fig.supxlabel('X title', fontsize=10)
        #fig.supylabel('Y title', fontsize=10)
        
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.updateGeometry(self)
        self.plot1()


    def plot1(self):
        global data
        macd = data['Close']
        exp1 = macd.ewm(span=12).mean()
        exp2 = macd.ewm(span=26).mean()
        comp_macd = exp1 - exp2
        signal = comp_macd.ewm(span=9).mean()
        macd_h = comp_macd  - signal
        x = data.index

        #y = data['Close']
        #z = 
        ax = self.figure.add_subplot(111)
        #ax.set_ylim(ymax=450)
        #ax.set_ylim(bottom=100)
        #print(dir(ax))
        
        #ax.set_ylabel('fault courage')
        
        
        
        
        ax.plot(x,comp_macd, color="blue")
        ax.plot(x,signal, color="red")
        ax.plot(x,macd_h, color="green")
        #macd.plot(ax=ax, secondary_y=True, color="orange")
        ax.tick_params(axis='x', rotation=0)
        ax.set_xlabel('')
        #ax.legend()
        self.draw()
        self.flush_events()

class PlotCanvas2(FigureCanvas):

    def __init__(self, parent=None, width=15, height=2, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        fig.suptitle('RSI', fontsize=10)
        #fig.supxlabel('X title', fontsize=10)
        #fig.supylabel('Y title', fontsize=10)
        
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.updateGeometry(self)
        self.plot2()
# Returns RSI values
    def rsi(self, close, periods = 14):
    
      close_delta = close.diff()

      # Make two series: one for lower closes and one for higher closes
      up = close_delta.clip(lower=0)
      down = -1 * close_delta.clip(upper=0)
    
      ma_up = up.ewm(com = periods - 1, adjust=True, min_periods = periods).mean()
      ma_down = down.ewm(com = periods - 1, adjust=True, min_periods = periods).mean()

      rsi = ma_up / ma_down
      rsi = 100 - (100/(1 + rsi))
      return rsi

    def plot2(self):
        global data
        x = data.index

        data['RSI'] = self.rsi(data['Close'])
        #y = data['Close']
        ax = self.figure.add_subplot(111)
        #ax.set_ylim(ymax=450)
        #ax.set_ylim(bottom=100)
        #print(dir(ax))
        #ax.set_xlabel('Test Cases')
        #ax.set_ylabel('fault courage')
        #ax.plot(x,y)
        ax.axhline(30, linestyle='--', alpha=0.5, color='r')
        ax.axhline(70, linestyle='--', alpha=0.5, color='r')
        ax.plot(x,data['RSI'])
        ax.tick_params(axis='x', rotation=0)
        #ax.plot(y,z)
        #ax.plot(y,a)
        self.draw()
        self.flush_events()


class PlotCanvas3(FigureCanvas):

    def __init__(self, parent=None, width=15, height=2, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        fig.suptitle('Bollinger Bands', fontsize=10)
        #fig.supxlabel('X title', fontsize=10)
        #fig.supylabel('Y title', fontsize=10)
        
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.updateGeometry(self)
        self.plot3()


        
    def plot3(self):
        global data
        n = 50
        BBANDS = self.BBANDS(data, n)
        x = data.index

        y = data['Close']
        ax = self.figure.add_subplot(111)
        #ax.set_ylim(ymax=450)
        #ax.set_ylim(bottom=100)
        #print(dir(ax))
        #ax.set_xlabel('Test Cases')
        #ax.set_ylabel('fault courage')
        ax.plot(x,y)
        ax.plot(x,data['MiddleBand'])
        ax.plot(x,data['UpperBand'])
        ax.plot(x,data['LowerBand'] )
        ax.tick_params(axis='x', rotation=0)
        self.draw()
        self.flush_events()
        
# Compute the Bollinger Bands 
    def BBANDS(self, data, window=50):
        MA = data.Close.rolling(window=50).mean()
        SD = data.Close.rolling(window=50).std()
        data['MiddleBand'] = MA
        data['UpperBand'] = MA + (2 * SD) 
        data['LowerBand'] = MA - (2 * SD)
        return data
        
class PlotCanvas4(FigureCanvas):

    def __init__(self, parent=None, width=15, height=2, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        fig.suptitle('Moving Average Price', fontsize=10)
        #fig.supxlabel('X title', fontsize=10)
        #fig.supylabel('Y title', fontsize=10)
        
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.updateGeometry(self)
        self.plot4()

# Simple Moving Average 
    def SMA(self, data, ndays): 
        SMA = pd.Series(data['Close'].rolling(ndays).mean(), name = 'SMA') 
        data = data.join(SMA) 
        return data

# Exponentially-weighted Moving Average 
    def EWMA(self, data, ndays): 
        EMA = pd.Series(data['Close'].ewm(span = ndays, min_periods = ndays - 1).mean(), 
                 name = 'EWMA_' + str(ndays)) 
        data = data.join(EMA) 
        return data
        
    def plot4(self):
        global data
        x = data.index
        n = 50
        SMA = self.SMA(data,n)
        SMA = SMA.dropna()

        ew = 200
        EWMA = self.EWMA(data,ew)
        EWMA = EWMA.dropna()

        y = data['Close']
        ax = self.figure.add_subplot(111)
        #ax.set_ylim(ymax=450)
        #ax.set_ylim(bottom=100)
        #print(dir(ax))
        #ax.set_xlabel('Test Cases')
        #ax.set_ylabel('fault courage')
        ax.plot(SMA.index,SMA['Close'])
        ax.plot(SMA.index,SMA['SMA'])
        ax.plot(EWMA.index,EWMA['EWMA_200'] )
        ax.tick_params(axis='x', rotation=0)
        self.draw()
        self.flush_events()
        
class PlotCanvas5(FigureCanvas):

    def __init__(self, parent=None, width=15, height=2, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        fig.suptitle('Relative Volume', fontsize=10)
        #fig.supxlabel('X title', fontsize=10)
        #fig.supylabel('Y title', fontsize=10)
        
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.updateGeometry(self)
        self.plot5()

# RVOL 
    def RVOL(self, data, ndays): 
        Ave_VOL = pd.Series(data['Volume'].rolling(ndays).mean(), name = 'Ave_VOL') 
        data['RVOL'] = data['Volume'] / Ave_VOL
        #data = data.join(SMA)
       
        return data


        
    def plot5(self):
        global data
        x = data.index
        n = 30
        RVOL = self.RVOL(data,n)
        RVOL = RVOL.dropna()
        #print(RVOL)
        y = data['Volume']
        ax = self.figure.add_subplot(111)
        #ax.set_ylim(ymax=450)
        #ax.set_ylim(bottom=100)
        #print(dir(ax))
        #ax.set_xlabel('Test Cases')
        #ax.set_ylabel('fault courage')
        #ax.plot(RVOL.index,RVOL['Volume'])
        ax.plot(RVOL.index,RVOL['RVOL'])
        
        ax.tick_params(axis='x', rotation=0)
        self.draw()
        self.flush_events()
        
class PlotCanvas6(FigureCanvas):

    def __init__(self, parent=None, width=15, height=2, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        fig.suptitle('Williams %R', fontsize=10)
        #fig.supxlabel('X title', fontsize=10)
        #fig.supylabel('Y title', fontsize=10)
        
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.updateGeometry(self)
        self.plot6()

# W%R 
    def get_wr(self, high, low, close, lookback):
        highh = high.rolling(lookback).max() 
        lowl = low.rolling(lookback).min()
        wr = -100 * ((highh - close) / (highh - lowl))
        return wr





        
    def plot6(self):
        global data
        x = data.index
        n = 30
        data['wr_14'] = self.get_wr(data['High'], data['Low'], data['Close'], 14)
        
        
        y = data['wr_14']
        ax = self.figure.add_subplot(111)
        #ax.set_ylim(ymax=450)
        #ax.set_ylim(bottom=100)
        #print(dir(ax))
        #ax.set_xlabel('Test Cases')
        #ax.set_ylabel('fault courage')
        #ax.plot(RVOL.index,RVOL['Volume'])
        ax.axhline(-20, linestyle='--', alpha=0.5, color='r')
        ax.axhline(-80, linestyle='--', alpha=0.5, color='r')
        ax.plot(x,y)
        
        ax.tick_params(axis='x', rotation=0)
        self.draw()
        self.flush_events()
        
class PlotCanvas7(FigureCanvas):

    def __init__(self, parent=None, width=15, height=2, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        fig.suptitle('Stochastic Oscillator', fontsize=10)
        #fig.supxlabel('X title', fontsize=10)
        #fig.supylabel('Y title', fontsize=10)
        
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.updateGeometry(self)
        self.plot7()


        
    def plot7(self):
        global data
        x = data.index
        n = 30
        k_period = 14
        d_period = 3
        # Adds a "n_high" column with max value of previous 14 periods
        data['n_high'] = data['High'].rolling(k_period).max()
        # Adds an "n_low" column with min value of previous 14 periods
        data['n_low'] = data['Low'].rolling(k_period).min()
        # Uses the min/max values to calculate the %k (as a percentage)
        data['%K'] = (data['Close'] - data['n_low']) * 100 / (data['n_high'] - data['n_low'])
        # Uses the %k to calculates a SMA over the past 3 values of %k
        data['%D'] = data['%K'].rolling(d_period).mean()
        data.ta.stoch(high='high', low='low', k=14, d=3, append=True)
        #print(data)
        y = data['Close']
        z = data['Open']
        ax = self.figure.add_subplot(111)
        #ax.set_ylim(ymax=450)
        #ax.set_ylim(bottom=100)
        #print(dir(ax))
        #ax.set_xlabel('Test Cases')
        #ax.set_ylabel('fault courage')
        #ax.plot(RVOL.index,RVOL['Volume'])
        ##ax.axhline(-20, linestyle='--', alpha=0.5, color='r')
        ##ax.axhline(-80, linestyle='--', alpha=0.5, color='r')
        ax.plot(x,y)
        ax.plot(x,z)
        ax.plot(x,data['STOCHk_14_3_3'])
        ax.plot(x,data['STOCHd_14_3_3'])
        ax.axhline(20, linestyle='--', alpha=0.5, color='r')
        ax.axhline(80, linestyle='--', alpha=0.5, color='r')        
        ax.tick_params(axis='x', rotation=0)
        self.draw()
        self.flush_events()
          
def main():

    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()        


Managing Trading Risks by Knowing Calendar Events That Affect Market Volatility

  Introduction to Trading Risks In Forex and other financial markets, risk management is one of the most critical aspects of successful trad...