今天實(shí)現(xiàn)用參數(shù)樹來(lái)呈現(xiàn)回測(cè)結(jié)果的小例子。 回測(cè)系統(tǒng)結(jié)果要展示的參數(shù)較多,使用參數(shù)樹來(lái)呈現(xiàn)會(huì)比較清晰。 如下圖所示: ![](http://image109.360doc.com/DownloadImg/2023/10/1215/273692334_1_2023101203380938_wm.jpeg)
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5 import QtWidgets,QtCore from pyqtgraph.parametertree import ParameterTree from pyqtgraph.parametertree import Parameter from qtpy import QtCore from qtpy.QtWebEngineWidgets import QWebEngineView
# 創(chuàng)建參數(shù)樹的數(shù)據(jù) params = [ {'name': '基本收益信息', 'type': 'group', 'children': [ {'name': '回測(cè)收益', 'type': 'float', 'value': 132.836, 'siPrefix': True, 'suffix': '%'}, {'name': '回測(cè)年化收益', 'type': 'float', 'value': 32.712, 'step': 0.1, 'siPrefix': True, 'suffix': '%'}, {'name': '基準(zhǔn)收益', 'type': 'float', 'value': 48.261}, {'name': '基準(zhǔn)年化收益', 'type': 'float', 'value': 14.096, 'step': 0.01},
]}, {'name': '風(fēng)險(xiǎn)指標(biāo)', 'type': 'group', 'children': [ {'name': 'Beta', 'type': 'float', 'value': 0.4580}, {'name': '夏普比', 'type': 'float', 'value': 1.2993}, {'name': '最大回撤', 'type': 'float', 'value': 16.295, 'step': 10, 'siPrefix': True, 'suffix': '%'}, ]}, ]
class WidgetParams(QWidget): def __init__(self): super(WidgetParams, self).__init__() self.initUI()
def initUI(self): t = ParameterTree() layout = QtWidgets.QHBoxLayout()
tree_widget = QtWidgets.QWidget() tree_widget.setMinimumWidth(300) layout.addWidget(tree_widget)
vLayout = QVBoxLayout() tree_widget.setLayout(vLayout) vLayout.addWidget(t) ## 創(chuàng)建參數(shù)對(duì)象樹 p = Parameter.create(name='params', type='group', children=params) t.setParameters(p, showTop=False) t.setHeaderLabels(["參數(shù)", "數(shù)值"]) self.web = QWebEngineView(self) layout.addWidget(self.web) #self.web.load( # QUrl.fromLocalFile(self.plotly_pyqt5.get_plotly_path_product_vs_hs300()))
self.setLayout(layout)
if __name__ == '__main__': app = QApplication(sys.argv) demo = WidgetParams() demo.show() sys.exit(app.exec_())
關(guān)于作者:魏佳斌,互聯(lián)網(wǎng)產(chǎn)品/技術(shù)總監(jiān),北京大學(xué)光華管理學(xué)院(MBA),特許金融分析師(CFA),資深產(chǎn)品經(jīng)理/碼農(nóng)。偏愛(ài)python,深度關(guān)注互聯(lián)網(wǎng)趨勢(shì),人工智能,AI金融量化。致力于使用最前沿的認(rèn)知技術(shù)去理解這個(gè)復(fù)雜的世界。
|