Python quantitative stock trading (14) --- Use pyqt5 to build a stock trading leaderboard

Get the data

For the stock market, those stocks with relatively large gains and declines are mostly hot money gathering places, and these gathering places often have a very high probability of being on the top of the list. For retail investors who like to play short-term, especially investors who like to fight through the list, especially love the Dragon and Tiger List. Therefore, we can provide such a list for our trading software.

The way to get the free Dragon and Tiger List is as follows:

df_rise = ak.stock_sina_lhb_detail_daily(trade_date="20210205", symbol="涨幅偏离值达7%的证券")
df_fall = ak.stock_sina_lhb_detail_daily(trade_date="20210205", symbol="跌幅偏离值达7%的证券")

The first parameter is the date, and the second parameter is the type of dragon and tiger list obtained. Here, our dates are set manually. When we arrive later, we can replace them by judging the working days.

pyqt5 displays the data of the dragon and tiger list

First of all, we need to get the data of the Dragon and Tiger List through threads, the specific code is as follows:

import akshare as ak
from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSignal
from pandas import DataFrame


class OtherThread(QtCore.QThread):
    _signalRise = pyqtSignal(DataFrame)
    _signalFall = pyqtSignal(DataFrame)

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

    def run(self):
        df_rise = ak.stock_sina_lhb_detail_daily(trade_date="20210205", symbol="涨幅偏离值达7%的证券")
        df_fall = ak.stock_sina_lhb_detail_daily(trade_date="20210205", symbol="跌幅偏离值达7%的证券")
        self._signalRise.emit(df_rise)
        self._signalFall.emit(df_fall)

After the data acquisition is completed, it can be directly displayed on the interface. The code of main.py is as follows:

class MyFrom(QMainWindow):
	# 龙虎榜
    def init_otherTab(self):
        self.otherGrid = QGridLayout()
        self.otherGrid.setSpacing(5)
        ft = QFont()
        ft.setPointSize(26)
        ft.setBold(True)
        rise_label = QLabel("涨幅偏离值达7%的股票")
        rise_label.setFont(ft)
        rise_label.setStyleSheet("color:red")
        fall_label = QLabel("跌幅偏离值达7%的股票")
        fall_label.setFont(ft)
        self.otherGrid.addWidget(rise_label, 0, 0, 1, 8)
        self.otherGrid.addWidget(fall_label, 0, 8, 1, 8)
        self.otherTab.setLayout(self.otherGrid)
        self.otherThread = OtherThread()
        self.otherThread._signalRise.connect(self.otherRise_callbacklog)
        self.otherThread._signalFall.connect(self.otherFall_callbacklog)
        self.otherThread.start()

    def otherFall_callbacklog(self, df):
        ft = QFont()
        ft.setPointSize(10)
        ft.setBold(True)
        m_color = QColor(0, 255, 0)
        otherFalltableWidget = QTableWidget(len(df), 6)
        otherFalltableWidget.setHorizontalHeaderLabels(['股票名称', '股票代码', '收盘价', "对应值", "成交量", "成交额"])
        otherFalltableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)  # 不可编辑
        otherFalltableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Fixed)  # 禁止拖拽
        otherFalltableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)  # 只能选中一行
        otherFalltableWidget.itemClicked.connect(self.tableWidget_connect)
        otherFalltableWidget.verticalHeader().setVisible(False)
        otherFalltableWidget.setShowGrid(False)  # 不显示子线条
        otherFalltableWidget.setColumnWidth(0, 70)  # 设置第一列宽
        otherFalltableWidget.setColumnWidth(1, 70)  # 设置第二列宽
        otherFalltableWidget.setColumnWidth(2, 70)  # 设置第三列宽
        otherFalltableWidget.setColumnWidth(3, 70)  # 设置第三列宽
        otherFalltableWidget.setColumnWidth(4, 120)  # 设置第三列宽
        otherFalltableWidget.setColumnWidth(5, 120)  # 设置第三列宽
        for idx, row in df.iterrows():
            newItem0 = QTableWidgetItem(str(row["股票名称"]))
            newItem0.setFont(ft)
            newItem0.setForeground(QBrush(m_color))
            newItem1 = QTableWidgetItem(str(row["股票代码"]))
            newItem1.setFont(ft)
            newItem1.setForeground(QBrush(m_color))
            newItem2 = QTableWidgetItem(str(row["收盘价"]))
            newItem2.setFont(ft)
            newItem2.setForeground(QBrush(m_color))
            newItem3 = QTableWidgetItem(str(row["对应值"]))
            newItem3.setFont(ft)
            newItem3.setForeground(QBrush(m_color))
            newItem4 = QTableWidgetItem(str(row["成交量"]))
            newItem4.setFont(ft)
            newItem4.setForeground(QBrush(m_color))
            newItem5 = QTableWidgetItem(str(row["成交额"]))
            newItem5.setFont(ft)
            newItem5.setForeground(QBrush(m_color))
            otherFalltableWidget.setItem(idx, 0, newItem0)
            otherFalltableWidget.setItem(idx, 1, newItem1)
            otherFalltableWidget.setItem(idx, 2, newItem2)
            otherFalltableWidget.setItem(idx, 3, newItem3)
            otherFalltableWidget.setItem(idx, 4, newItem4)
            otherFalltableWidget.setItem(idx, 5, newItem5)
        self.otherGrid.addWidget(otherFalltableWidget, 1, 8, 10, 8)

    def otherRise_callbacklog(self, df):
        ft = QFont()
        ft.setPointSize(10)
        ft.setBold(True)
        m_color = QColor(255, 0, 0)
        otherRisetableWidget = QTableWidget(len(df), 6)
        otherRisetableWidget.setHorizontalHeaderLabels(['股票名称', '股票代码', '收盘价', "对应值", "成交量", "成交额"])
        otherRisetableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)  # 不可编辑
        otherRisetableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Fixed)  # 禁止拖拽
        otherRisetableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)  # 只能选中一行
        otherRisetableWidget.itemClicked.connect(self.tableWidget_connect)
        otherRisetableWidget.verticalHeader().setVisible(False)
        otherRisetableWidget.setShowGrid(False)  # 不显示子线条
        otherRisetableWidget.setColumnWidth(0, 70)  # 设置第一列宽
        otherRisetableWidget.setColumnWidth(1, 70)  # 设置第二列宽
        otherRisetableWidget.setColumnWidth(2, 70)  # 设置第三列宽
        otherRisetableWidget.setColumnWidth(3, 70)  # 设置第三列宽
        otherRisetableWidget.setColumnWidth(4, 120)  # 设置第三列宽
        otherRisetableWidget.setColumnWidth(5, 120)  # 设置第三列宽
        for idx, row in df.iterrows():
            newItem0 = QTableWidgetItem(str(row["股票名称"]))
            newItem0.setFont(ft)
            newItem0.setForeground(QBrush(m_color))
            newItem1 = QTableWidgetItem(str(row["股票代码"]))
            newItem1.setFont(ft)
            newItem1.setForeground(QBrush(m_color))
            newItem2 = QTableWidgetItem(str(row["收盘价"]))
            newItem2.setFont(ft)
            newItem2.setForeground(QBrush(m_color))
            newItem3 = QTableWidgetItem(str(row["对应值"]))
            newItem3.setFont(ft)
            newItem3.setForeground(QBrush(m_color))
            newItem4 = QTableWidgetItem(str(row["成交量"]))
            newItem4.setFont(ft)
            newItem4.setForeground(QBrush(m_color))
            newItem5 = QTableWidgetItem(str(row["成交额"]))
            newItem5.setFont(ft)
            newItem5.setForeground(QBrush(m_color))
            otherRisetableWidget.setItem(idx, 0, newItem0)
            otherRisetableWidget.setItem(idx, 1, newItem1)
            otherRisetableWidget.setItem(idx, 2, newItem2)
            otherRisetableWidget.setItem(idx, 3, newItem3)
            otherRisetableWidget.setItem(idx, 4, newItem4)
            otherRisetableWidget.setItem(idx, 5, newItem5)
        self.otherGrid.addWidget(otherRisetableWidget, 1, 0, 10, 8)

Here, in order to be lazy, the blogger did not use the for loop to generate pyqt5 controls. Those who are interested can imitate Chapter 11 of Quantitative Trading and use the for loop to generate QLabel to generate QTableWidgetItem.

After running, the displayed effect is as shown in the figure below:
Insert picture description here

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113755531