zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

现货期权合约交易所开发需求丨现货期权合约交易所系统开发(实现技术及代码)

2023-03-14 22:38:39 时间

  Want to know clearly what is a quantitative trading robot?First of all,we should clarify the basic concept of quantitative trading:

  Quantitative trading refers to an investment method that uses modern statistics and mathematical methods to trade through computer technology.Quantitative trading selects a variety of"high probability"events that can achieve excess returns from massive historical data to formulate strategies,uses quantitative models to verify and solidify these laws and strategies,and then strictly implements the solidified strategies to guide investment,in order to obtain sustained,stable and higher than average returns.

  入口代码为vnpy.app.cta_backtester.ui.widget.BacktesterManager.start_backtesting()方法.获取界面配置的合约相关参数,弹窗vnpy.app.cta_backtester.ui.widget.BacktestingSettingEditor对话框,用户确认后,调用vnpy.app.cta_backtester.engine.BacktesterEngine.start_backtesting()开始回测,该方法开启新线程调用run_backtesting().此方法调用逻辑如下:

  1.获取vnpy.app.cta_strategy.backtesting.BacktestingEngine类对象,传入界面参数.

  2.调用vnpy.app.cta_strategy.backtesting.BacktestingEngine.load_data()方法加载数据.根据是bar模式还是tick模式,调用load_bar_data()和load_tick_data()加载数据到self.history_data的list中。默认bar模式,进入vnpy.app.cta_strategy.backtesting.load_bar_data(),调用vnpy.trader.database.database_sql.SqlManager.load_bar_data(),它通过peewee的orm库读取数据库.

  3.调用vnpy.app.cta_strategy.backtesting.BacktestingEngine.run_backtesting()开始回测.

  (1).根据bar还是tick模式,确认进入BacktestingEngine.new_bar()还是BacktestingEngine.new_tick()方法.

  (2).进入vnpy.app.cta_strategy.strategies.double_ma_strategy.DoubleMaStrategy.on_init()初始化策略.进入vnpy.app.cta_strategy.template.CtaTemplate.load_bar()初始化加载.

  (3).遍历历史K线数据,统计总天数.如果是一个月的分钟线,针对前10根K线(这块没看懂?),每一根K线都进入double_ma_strategy.DoubleMaStrategy.on_bar()方法处理。首先调用vnpy.trader.utility.ArrayManager.update_bar()更新当前K线信息.然后计算短期和长期均线价格,如果短期上穿长期均线,且当前仓位为0,则调用template.CtaTemplate.buy()方法下单买入.下单进入vnpy.app.cta_strategy.template.CtaTemplate.send_order()方法,回测期间该方法返回空.如果短期下穿长期均线,且当前仓位为0,调用vnpy.app.cta_strategy.template.CtaTemplate.short()做空.

  (4).调用double_ma_strategy.DoubleMaStrategy.on_start()触发start事件.

  (5).调用BacktestingEngine.new_bar()从第10根K线开始回放历史数据.该方法处理逻辑如下:

  (5.1)调用BacktestingEngine.cross_limit_order()撮合本地限价单委托,将最新的行情K线或者TICK和策略之前下达的所有委托进行检查,如果能够撮合成交,则返回并记录数据。

  (5.2)撮合本地停止单(条件单)委托.

  K线图表

  入口代码为BacktesterManager.show_candle_chart().获取之前加载的数据库里面的bar数据。

  1.调用CandleChartDialog.update_history(),调用vnpy.chart.manager.BarManager.update_history(),调用vnpy.chart.item.ChartItem.update_history()遍历所有K线,调用vnpy.chart.item.CandleItem._draw_bar_picture()通过QT画K线图.

  2.获取所有交易记录,调用widget.CandleChartDialog.update_trades()在K线图上标记买卖记录.