2023年7月19日 星期三

【Python交易程式開發現場53】接收商品報價(1) | 將訂閱商品資訊存入記憶體

使用QTableWidget訂閱商品報價(1) | 將訂閱商品資訊填入QtTableWidget


結論: 修改了Part V. 第670行後, 即搞定

##############################################################################
# V - View | 主視窗類別 | 按鈕送出訊號所對應之槽函數
##############################################################################

MyMainWindow():

    def twTest3(self):  #ctw slot function
        print("3-查詢")    
        self.listWidget.addItem("3-查詢")
        self.listWidget.addItem("m_nCode: " + str(twQueryStocks("0", STOCK_ID)) )  
        #def twQueryStocks(twPageNo, twStocks):  #(ctw-3-2. 查詢)
        # 3-【查詢】    
        # twPageNo: self.txtPageNo.get().replace(' ','')
        # twStocks: self.txtStocks.get().replace(' ','')      



1. 選擇訂閱的商品:


##############################################################################
# III. 群益API 按鈕事件所對應之相閞 tw Slot槽函數 (於V.槽函數所會用到的子工具函數區)
##############################################################################

#-----------------------------------------------------------------------------
#  Part1. (tw函數1) 呼叫API函數區,tw呼叫類函數(連線,查報價,下單,查倉位)【III.1】
#-----------------------------------------------------------------------------

twQueryStock()

def twQueryStocks(twPageNo, twStocks):  #(ctw-3-2. 查詢)   # skq3
# 3-【查詢】    
# twPageNo: self.txtPageNo.get().replace(' ','')
# twStocks: self.txtStocks.get().replace(' ','')    
    try:
        if(twPageNo == ''):
            pn = 0
        else:
            pn = int(twPageNo) #?
        m_nCode = skQ.SKQuoteLib_RequestStocks(pn,twStocks)
        return m_nCode
    except Exception as e:
            messagebox.showerror("error!",e)


第670行

##############################################################################
# V - View | 主視窗類別 | 按鈕送出訊號所對應之槽函數
##############################################################################

MyMainWindow():

    def twTest3(self):  #ctw slot function
        print("3-查詢")    
        self.listWidget.addItem("3-查詢")
        self.listWidget.addItem("m_nCode: " + str(twQueryStocks("0", "TX00")) )  
        #def twQueryStocks(twPageNo, twStocks):  #(ctw-3-2. 查詢)
        # 3-【查詢】    
        # twPageNo: self.txtPageNo.get().replace(' ','')
        # twStocks: self.txtStocks.get().replace(' ','')    


2. 訂閱商品報價回傳:

第500行

##############################################################################
#  IV - 群益API事件
##############################################################################
class SKQuoteLibEvents:

    def OnNotifyQuoteLONG(self, sMarketNo, nStockidx):    #ctw # 3-【查詢】回報    
        pStock = sk.SKSTOCKLONG()
        m_nCode = skQ.SKQuoteLib_GetStockByIndexLONG(sMarketNo, nStockidx, pStock)
        strMsg = '代碼:',pStock.bstrStockNo,'--名稱:',pStock.bstrStockName,'--開盤價:',str(pStock.nOpen/math.pow(10,pStock.sDecimal)),'--最高:',\
            str(pStock.nHigh/math.pow(10,pStock.sDecimal)),'--最低:',\
            str(pStock.nLow/math.pow(10,pStock.sDecimal)),'--成交價:',str(pStock.nClose/math.pow(10,pStock.sDecimal))\
            ,'--總量:',str(pStock.nTQty)
               
        twStrMsg=twTuple_2_Str(strMsg)
        send.run(twStrMsg)  #發射Signal訊號
       
        """
        strMsg = Gobal_ServerTime_Information["text"], \
            '--1代碼:',pStock.bstrStockNo,\
            '--2成交價:',pStock.nClose/math.pow(10,pStock.sDecimal),\
            '--3成交量:',pStock.nTickQty,\
            '--4總量:',pStock.nTQty,\
            '--5買量:',pStock.nBc,\
            '--6賣量:',pStock.nAc,\
            '--7最高:',pStock.nHigh/math.pow(10,pStock.sDecimal),\
            '--8最低:',pStock.nLow/math.pow(10,pStock.sDecimal)
        WriteMessage(strMsg,Gobal_Quote_ListInformation)
        """
        #twSaveQuoteToFile(pStock)






2023年7月8日 星期六

【Python交易程式開發現場42】產生交易訊號 (5) | 用pyqtgraph畫動態均線圖 | 均線Color設定 (twMASignal.py)

均線Color設定:

先說結論: pg.plot(xdata, ydata, pen='r')



1. 參考資訊1: 

https://www.geeksforgeeks.org/pyqtgraph-setting-pen-of-line-in-line-graph/


    # method for components
    def UiComponents(self):
 
        # creating a widget object
        widget = QWidget()
 
        # creating a new label
        label = QLabel("GeeksforGeeks Line Plot")
 
        # making it multiline
        label.setWordWrap(True)
 
        # y values to plot by line 1
        y = [2, 8, 6, 8, 6, 11, 14, 13, 18, 19]
 
        # y values to plot by line 2
        y2 = [3, 1, 5, 8, 9, 11, 16, 17, 14, 16]
        x = range(0, 10)
 
        # create plot window object
        plt = pg.plot()
 
        # showing x and y grids
        plt.showGrid(x = True, y = True)
 
        # adding legend
        plt.addLegend()
 
        # set properties of the label for y axis
        plt.setLabel('left', 'Vertical Values', units ='y')
 
        # set properties of the label for x axis
        plt.setLabel('bottom', 'Horizontal Values', units ='s')
 
        # setting horizontal range
        plt.setXRange(0, 10)
 
        # setting vertical range
        plt.setYRange(0, 20)
 
        # plotting line in green color
        # with dot symbol as x, not a mandatory field
        line1 = plt.plot(x, y, pen ='g', symbol ='x', symbolPen ='g', symbolBrush = 0.2, name ='green')
 
        # plotting line2 with blue color
        # with dot symbol as o
        line2 = plt.plot(x, y2, pen ='b', symbol ='o', symbolPen ='b', symbolBrush = 0.2, name ='blue')
 
        # setting pen of the line 1
        line1.setPen(QColor(168, 34, 3))



2. 參考資訊2: 




Line, Fill, and Color

Qt relies on its QColor, QPen and QBrush classes for specifying line and fill styles for all of its drawing. 

Internally, pyqtgraph uses the same system but also allows many shorthand methods of specifying the same style options.

Many functions and methods in pyqtgraph accept arguments specifying the line style (pen), fill style (brush), or color. For most of these function arguments, the following values may be used:

  • single-character string representing color (b, g, r, c, m, y, k, w)

  • (r, g, b) or (r, g, b, a) tuple

  • single greyscale value (0.0 - 1.0)

  • (index, maximum) tuple for automatically iterating through colors (see intColor)

  • QColor

  • QPen / QBrush where appropriate

Notably, more complex pens and brushes can be easily built using the mkPen() / mkBrush() functions or with Qt’s QPen and QBrush classes:

mkPen('y', width=3, style=QtCore.Qt.DashLine)          ## Make a dashed yellow line 2px wide
mkPen(0.5)                                             ## solid grey line 1px wide
mkPen(color=(200, 200, 255), style=QtCore.Qt.DotLine)  ## Dotted pale-blue line

See the Qt documentation for ‘QPen’ and ‘PenStyle’ for more line-style options and ‘QBrush’ for more fill options. Colors can also be built using mkColor(), intColor(), hsvColor(), or Qt’s QColor class.

Default Background and Foreground Colors

By default, pyqtgraph uses a black background for its plots and grey for axes, text, and plot lines. These defaults can be changed using pyqtgraph.setConfigOption():

import pyqtgraph as pg

## Switch to using white background and black foreground
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')

## The following plot has inverted colors
pg.plot([1,4,2,3,5])
(Note that this must be set before creating any widgets)

3. 參考資訊3: 

https://www.geeksforgeeks.org/pyqtgraph-setting-background-of-plot-window/
# create pyqt5graph bar graph item 
# with width = 0.6
# with bar colors = green
bargraph1 = pg.BarGraphItem(x = x, height = y1, width = 0.6, brush ='g')
  
# add item to plot window
# adding bargraph item to the window
window.addItem(bargraph1)
  
  
# setting plot window background color to yellow
window.setBackground('y')

4. 參考資訊4: (成功)
https://pyqtgraph.readthedocs.io/en/latest/api_reference/functions.html#pyqtgraph.mkPen

Color, Pen, and Brush Functions

Qt uses the classes QColor, QPen, and QBrush to determine how to draw lines and fill shapes. These classes are highly capable but somewhat awkward to use. PyQtGraph offers the functions mkColor(), mkPen(), and mkBrush() to simplify the process of creating these classes. In most cases, however, it will be unnecessary to call these functions directly–any function or method that accepts pen or brush arguments will make use of these functions for you. 

For example, the following three lines all have the same effect:

pg.plot(xdata, ydata, pen='r')
pg.plot(xdata, ydata, pen=pg.mkPen('r'))
pg.plot(xdata, ydata, pen=QPen(QColor(255, 0, 0)))





prog-0318

test test1 Written with StackEdit .