python股票量化交易(15)---使用pyqt5构建股票交易北向资金

目录

  • 获取北向资金数据
  • pyqt5构建北向资金界面

获取北向资金数据

如果我们需要构建北向资金界面,首先也是需要获取到北向资金的交易数据。我们直接上代码:

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

class NchThread(QtCore.QThread):
    _signal = pyqtSignal(DataFrame)
    def __init__(self):
        super(NchThread, self).__init__()

    def run(self):
        df = ak.stock_em_hsgt_board_rank(symbol="北向资金增持行业板块排行", indicator="今日")
        self._signal.emit(df)

这里,获取网络数据同样也需要在线程中获取,并返回给界面。

pyqt5构建北向资金界面

同之前的龙虎榜数据一样,我们通过QTableWidget组件进行构建,代码与上篇类似,我们直接上代码:

class MyFrom(QMainWindow):
	# 北向资金
    def init_nch(self):
        self.nchGrid = QGridLayout()
        self.nchGrid.setSpacing(5)
        self.nchTab.setLayout(self.nchGrid)
        self.nchThread = NchThread()
        self.nchThread._signal.connect(self.nchThead_callbacklog)
        self.nchThread.start()

    def nchThead_callbacklog(self, df):
        ft = QFont()
        ft.setPointSize(10)
        ft.setBold(True)
        nchtableWidget = QTableWidget(len(df), 6)
        nchtableWidget.setHorizontalHeaderLabels(
            ['名称', '最新涨跌幅', '北向资金今日持股-股票只数', "北向资金今日增持估计-市值", "今日增持最大股-市值", "今日减持最大股-市值"])
        nchtableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)  # 不可编辑
        nchtableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Fixed)  # 禁止拖拽
        nchtableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)  # 只能选中一行
        nchtableWidget.itemClicked.connect(self.tableWidget_connect)
        nchtableWidget.verticalHeader().setVisible(False)
        nchtableWidget.setShowGrid(False)  # 不显示子线条
        nchtableWidget.setColumnWidth(0, 150)  # 设置第一列宽
        nchtableWidget.setColumnWidth(1, 100)  # 设置第二列宽
        nchtableWidget.setColumnWidth(2, 200)  # 设置第三列宽
        nchtableWidget.setColumnWidth(3, 210)  # 设置第三列宽
        nchtableWidget.setColumnWidth(4, 200)  # 设置第三列宽
        nchtableWidget.setColumnWidth(5, 200)  # 设置第三列宽
        for idx, row in df.iterrows():
            if float(row["最新涨跌幅"]) < 0:
                m_color = QColor(0, 255, 0)
            elif float(row["最新涨跌幅"]) > 0:
                m_color = QColor(255, 0, 0)
            else:
                m_color = QColor(255, 255, 255)
            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))
            newItem0.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            newItem1.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            newItem2.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            newItem3.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            newItem4.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            newItem5.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            nchtableWidget.setItem(idx, 0, newItem0)
            nchtableWidget.setItem(idx, 1, newItem1)
            nchtableWidget.setItem(idx, 2, newItem2)
            nchtableWidget.setItem(idx, 3, newItem3)
            nchtableWidget.setItem(idx, 4, newItem4)
            nchtableWidget.setItem(idx, 5, newItem5)
        self.nchGrid.addWidget(nchtableWidget, 0, 0, 16, 16)

运行之后,显示的效果如下图所示:
python股票量化交易(15)---使用pyqt5构建股票交易北向资金_第1张图片

你可能感兴趣的:(股票量化交易,Python,python,量化交易,QTableWidget,pyqt5,QThread)