import matplotlib as mpl
import matplotlib.pyplot as plt
from time import perf_counter
import numpy as np
import pyqtgraph as pg
import pickle
from twIndicator import twClosePriceModel
import twGlobal
tw_close = twGlobal.twClose #Ticks報價
tw_kbar1m = twGlobal.twKBar1M
with open('twclose.pickle', 'rb') as f:
tw_close = pickle.load(f) #Ticks報價
win = pg.GraphicsLayoutWidget(show=True)
win.setWindowTitle('pyqtgraph example: Scrolling Plots')
p1 = win.addPlot()
showLength = 100 # Tick圖總長度
leftLength = 500 # 動態看最後leftLength筆Ticks
tickLengh = len(tw_close.GetClose()) #取得之所有Tick數目
print("Tick總數: ", tickLengh)
# data1為顯示在Tick圖上之所有data
data1 = tw_close.GetClose()[(tickLengh-leftLength-showLength):(tickLengh-leftLength)]
data2 = tw_close.GetSMA()[(tickLengh-leftLength-showLength):(tickLengh-leftLength)]
print("Show Tick總數: ", len(data1))
curve1 = p1.plot(data1)
curve2 = p1.plot(data2)
ptr1 = tickLengh-leftLength+1 #為了取得顯示在Tick圖上的Tick之後的下一個Tick
def update1():
global data1, data2, ptr1
data1[:-1] = data1[1:] # shift data in the array one sample left
#[:-1]意思是->取全部,除了最後一個
#[1:]意思是->取全部,除了第一個
#[-1]意思是->取最後一個
# (see also: np.roll)
#data1[-1] = np.random.normal()
data1[-1] = tw_close.GetClose()[ptr1]
curve1.setData(data1)
data2[:-1] = data2[1:] # shift data in the array one sample left
data2[-1] = tw_close.GetSMA()[ptr1]
curve2.setData(data2)
ptr1 += 1
print("ptr1=", ptr1)
# update all plots
def update():
update1()
timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)
if __name__ == '__main__':
pg.exec()