Pythonの定量的株取引(14)--- pyqt5を使用して株取引リーダーボードを構築する

データを取得する

株式市場にとって、比較的大きな増減のある株は、ほとんどがホットマネーの集まる場所であり、これらの集まる場所の可能性は、ドラゴンとタイガーのリストのトップにあることがよくあります。短期間のプレーを好む個人投資家、特にリストをめぐって戦うことを好む投資家にとって、特にドラゴンとタイガーのリストが大好きです。したがって、当社のトレーディングソフトウェアにそのようなリストを提供することができます。

無料のドラゴンとタイガーのリストを入手する方法は次のとおりです。

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%的证券")

最初のパラメーターは日付で、2番目のパラメーターは取得したドラゴンとタイガーのリストのタイプです。ここでは、日付を手動で設定していますが、後で到着した場合は、営業日を判断して置き換えることができます。

pyqt5はドラゴンとタイガーのリストのデータを表示します

まず、スレッドを介してドラゴンとタイガーのリストのデータを取得する必要があります。具体的なコードは次のとおりです。

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)

データ収集が完了すると、インターフェースに直接表示できます。main.pyのコードは次のとおりです。

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)

ここでは、怠惰になるために、ブロガーはpyqt5コントロールを生成するためにforループを使用しませんでした。興味のある人は、定量的取引の第11章を模倣し、forループを使用してQLabelを生成し、QTableWidgetItemを生成できます。

実行後、表示される効果は次の図のようになります。
ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/liyuanjinglyj/article/details/113755531
おすすめ