首页 > 其他分享 >为 vnpy 回测增加指标显示

为 vnpy 回测增加指标显示

时间:2023-04-26 13:44:48浏览次数:29  
标签:vnpy QtGui ix self 指标 回测 dialog candle lineList

添加一个指标类:



from vnpy.trader.ui import QtCore, QtGui
from vnpy.trader.object import BarData
from vnpy.chart.base import BLACK_COLOR, UP_COLOR, DOWN_COLOR, PEN_WIDTH, BAR_WIDTH,WHITE_COLOR
from vnpy.chart.item import CandleItem
from vnpy.trader.object import BarData
from vnpy.chart.manager import BarManager

class LineItem(CandleItem):     """ LineItem 用于策略内线性标线图示 """     prompt = "Line"     def __init__(self, manager: BarManager) -> None:         """"""         super().__init__(manager)         self.lineList = []         self._color = WHITE_COLOR
    def setData(self, lineList : list, prompt:str, color : QtGui.QColor = WHITE_COLOR):         self.lineList = lineList         self._color = color         self.prompt = prompt
    def _draw_bar_picture(self, ix: int, _) -> QtGui.QPicture:         """"""         candle_picture: QtGui.QPicture = QtGui.QPicture()         painter: QtGui.QPainter = QtGui.QPainter(candle_picture)                 painter.setPen(QtGui.QPen(self._color))         if ix != 0:             if self.lineList[ix - 1] != None and self.lineList[ix] != None:                 painter.drawLine(                     QtCore.QPointF(ix - 1, self.lineList[ix - 1]),                     QtCore.QPointF(ix, self.lineList[ix])                 )           painter.end()         return candle_picture             def get_info_text(self, ix: int) -> str:         """"""                 if ix < len(self.lineList) and self.lineList[ix]:                         text = f"{self.prompt}:{self.lineList[ix]:.2f}"         else:             text = f"{self.prompt}:None"         return text
 

调用指标的代码:

    engine : BacktestingEngine = backtest(BreakStrategy)             while create_qapp():         # 建立K线图表         candle_dialog: CandleChartDialog = CandleChartDialog()         # 增加指标         candle_dialog.chart.add_item(LineItem, "mean", "candle")         candle_dialog.chart.add_item(LineItem, "up", "candle")         candle_dialog.chart.add_item(LineItem, "down", "candle")         # 设置指标数值         candle_dialog.chart._items["mean"].setData(engine.strategy.mean_list, "mean", QtGui.QColor(255, 0, 0))         candle_dialog.chart._items["up"].setData(engine.strategy.bollup_list, "up", QtGui.QColor(155, 155, 155))         candle_dialog.chart._items["down"].setData(engine.strategy.bolldown_list, "down", QtGui.QColor(155, 155, 155))
        # 设置K线图表的数据         history: list = engine.history_data         # 更新K线图表的数据         candle_dialog.update_history(history)         # 更新K线图表的成交记录         trades: List[TradeData] = engine.get_all_trades()                 candle_dialog.update_trades(trades)                 candle_dialog.exec()         break
    print("task finished")

要点:

在update_history 前 additem 和 setData,  因为 CandleChartDialog 里的 update_history 里直接更新了画图内容。 

策略代码里,要生成指标list, 用于测试时取出。 

 

标签:vnpy,QtGui,ix,self,指标,回测,dialog,candle,lineList
From: https://www.cnblogs.com/kingkaixuan/p/17355647.html

相关文章

  • 磁盘I/O性能监控的指标
    服务器硬盘是磁盘资源中最慢的组件,在企业数据中心,注意仔细设计存储子系统,不要让它成为主要性能瓶颈,而最理想的办法是使用SAN,即使预算不允许,也要想办法确保磁盘资源争用不会导致虚拟机(VM)瘫痪。首先应将宿主操作系统安装到专用硬盘上,注意不是专用卷,确保宿主操作系统不会与虚拟机抢夺......
  • 广州app软件开发公司要评估什么指标?四个指标至关重要
      在找一个开发公司帮助自己开发app软件的时候,不少企业都会挑花了眼,不知道从何下手。针对这种情况,我们就要评估一些指标。那么广州app软件开发公司要评估什么指标?下面名锐讯动为大家讲述四个指标至关重要。 1.技术团队。衡量一个开发公司实力的重要指标毫无疑问就是技术团队,......
  • [交易策略]海龟交易法在上证日线历史数据回测
    TR:=MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));//真实波幅ATR:=MA(TR,26);//求26个周期内真实波幅的简单移动平均TC..INTPART((MONEYTOT*0.01/(UNIT*ATR)));//根据权益的1%计算下单手数MTC..4*TC;//总的持仓头寸HH:=HV(H,20);LL:=LV(L,20);CROSS......
  • [交易策略]周线四进两出策略回测
    四周期最高线:HV(H,4);两周期最低线:LV(L,2);四进:=C>四周期最高线;两出:=C<两周期最低线;四进=1,BK;两出=1,SP;AUTOFILTER;......
  • [交易策略]单均线模型回测
    文华财经过滤模型代码:MA20:MA(C,20);C>MA20,BK;C<MA20,SP;AUTOFILTER;......
  • 快速识别 SLI 指标的方法:VALET
    SLI,ServiceLevelIndicator,服务等级指标,其实就是我们选择哪些指标来衡量我们的稳定性。而SLO,ServiceLevelObjective,服务等级目标,指的就是我们设定的稳定性目标,比如“几个9”这样的目标。VALET是5个单词的首字母,分别是Volume、Availability、Latency、Error和Ticket。这......
  • Prometheus 四大度量指标的了解和应用
    Prometheus四大度量指标的了解和应用什么是度量指标Prometheus的指标格式对外提供metrics服务Prometheus四大度量指标的了解和应用Counter(计数器)实战演练Gauge(仪表盘)实战演练Histogram(累积直方图)实战演练Summary(摘要)实战演练小结Prometh......
  • 模型评估指标——sklearn.metrics模块
    sklearn.metrics模块该模块主要包含分数函数、性能指标、成对指标、距离计算1.分类性能指标1.1.accuracy_score()计算所有样本中分类正确样本所占的比例语法##语法sklearn.metrics.accuracy_score(y_true,y_pred,*,normalize=True,sample_weight=None)y_tru......
  • 【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.u
    问题描述在Azure的SpringCloud服务(官名为:SpringApps)中,在Metrics页面中查看AppMemoryUsage和jvm.memory.use,发现两则在下图中出现巨大差距。AppMemoryUsage还是在逐渐上升jvm.memory.use却断崖式下降  在AppMemoryUsage在逐渐上涨的情况下,是否会最终出现OO......
  • numpy实现PSI指标计算
    计算方法populationstabilityindex,群体稳定性指标,比较特征的分布在两个样本空间内的差异度,计算公式:\(PSI=\sum\limits_{i=1}^{n}(A_i-E_i)*ln(\frac{A_i}{E_i})\)参数说明\(A_i\)分箱内真实(Actual)样本个数占比\(E_i\)分箱内期望(Except)样本个数......