vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 71
声望: 0

1++++
首先在回测代码中通过引擎属性赋值参数如下:

#%%
engine = BacktestingEngine()
engine.set_parameters(

    vt_symbol="IH99.CFFEX",
    interval="1m",
    start=datetime(2019, 6, 1),
    end=datetime(2019, 6, 30),
    rate=0.3/10000,
    slippage=0.2*5,
    size=300,
    pricetick=0.2,
    capital=1_000_000,
    collection_name = "IH99"
)
engine.add_strategy(AtrRsiStrategy, {})

+
+
2+++++
AtrRsiStrategy策略中onbar如下:

    def on_bar(self, bar: BarData):
        """
        Callback of new bar data update.
        """
        self.cancel_all()

        df01=load_select_bar_data_from_mongodb(self.symbol,self.exchange,self.interval,(bar.datetime-timedelta(days=3)),
                                               bar.datetime,self.collection_name)
        print (df01)

        self.put_event()

注:load_select_bar_data_from_mongodb是自定义的从MongoDB数据库读取bardata并生成dataframe的函数,有关于symbol等参数需要传入。
+
+
+
3++++++++++
回测运行报错如下:

2020-11-30 22:50:43.167187  Traceback (most recent call last):
  File "D:\vnpy-2.1.7\vnpy\app\cta_strategy\backtesting.py", line 315, in run_backtesting
    self.callback(data)
  File "D:\vnpy-2.1.7\vnpy\app\cta_strategy\strategies\atr_rsi_strategy.py", line 129, in on_bar
    df01=load_select_bar_data_from_mongodb(self.symbol,self.exchange,self.interval,(bar.datetime-timedelta(days=3)),
AttributeError: 'AtrRsiStrategy' object has no attribute 'symbol'

+
+
+
4++++++++++
所以想请教大佬们,如何让策略接受到上述回测引擎engine.set_parameters所设置的属性?

Member
avatar
加入于:
帖子: 71
声望: 0

description
+
+
策略无法获得引擎设置的属性(如图)

Member
avatar
加入于:
帖子: 2104
声望: 139

on_bar里传进的BarData里不都有吗

Member
avatar
加入于:
帖子: 71
声望: 0

xiaohe wrote:

on_bar里传进的BarData里不都有吗
+
+
+
对对 大佬 我恍惚了。。
MongoDB的collection_name可以再engine.add_strategy(AtrRsiStrategy, {这里传入})对吧

Member
avatar
加入于:
帖子: 2104
声望: 139

对set_parameters函数进行修改了的话是填这的

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号