问答交流

文章回测报错:华泰研报:在XGboost中实现关于有序回归作为损失函数和评价函数

由bqddxi6j创建,最终由small_q 被浏览 19 用户

https://bigquant.com/college/courses/course-v1:public+2023110601+110601/courseware/7708009442174480802b3dd339f4ede0/45dafc16ea744216af376a7dc2961fa5/

老师您好,

我学习上面的视频文章,想试运行代码,但运行不下去,没办法回测,是我哪里没有配置对吗?谢谢老师!

  • \

    
    
    # 我们取前0.6的数据量作为训练集
    date = data['date'].unique()
    date.sort()
    index = int(len(date)*0.6)
    split = pd.to_datetime(date[index]).strftime('%Y-%m-%d')
    train_data = data[data['date']<split]
    test_data = data[data['date']>=split]
    
    # 加载数据
    xtrain = np.array(train_data.drop(['date', 'instrument'], axis=1))
    ytrain = np.array(train_data['label'])
    xtest = np.array(test_data.drop(['date', 'instrument'], axis=1))
    ytest = np.array(test_data['label'])
    
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    Cell In[1], line 2
          1 # 我们取前0.6的数据量作为训练集
    ----> 2 date = data['date'].unique()
          3 date.sort()
          4 index = int(len(date)*0.6)
    NameError: name 'data' is not defined
    


运行到最后报出以下错误:

  • \
    TypeError                                 Traceback (most recent call last)
    Cell In[20], line 53
         49 def m4_after_trading_bigquant_run(context, data):
         50     pass
    ---> 53 m4 = M.hftrade.v2(
         54     instruments=instruments,
         55     options_data=df,
         56     start_date='',
         57     end_date='',
         58     initialize=m4_initialize_bigquant_run,
         59     before_trading_start=m4_before_trading_start_bigquant_run,
         60     handle_tick=m4_handle_tick_bigquant_run,
         61     handle_data=m4_handle_data_bigquant_run,
         62     handle_trade=m4_handle_trade_bigquant_run,
         63     handle_order=m4_handle_order_bigquant_run,
         64     after_trading=m4_after_trading_bigquant_run,
         65     capital_base=1000000,
         66     frequency='daily',
         67     price_type='真实价格',
         68     product_type='股票',
         69     before_start_days='0',
         70     volume_limit=1,
         71     order_price_field_buy='open',
         72     order_price_field_sell='open',
         73     benchmark='000300.SH',
         74     plot_charts=True,
         75     disable_cache=False,
         76     replay_bdb=False,
         77     show_debug_info=False,
         78     backtest_only=False
         79 )
    File module2/common/modulemanagerv2.py:88, in biglearning.module2.common.modulemanagerv2.BigQuantModuleVersion.__call__()
    File module2/common/moduleinvoker.py:370, in biglearning.module2.common.moduleinvoker.module_invoke()
    File module2/common/moduleinvoker.py:292, in biglearning.module2.common.moduleinvoker._invoke_with_cache()
    File module2/common/moduleinvoker.py:253, in biglearning.module2.common.moduleinvoker._invoke_with_cache()
    File module2/common/moduleinvoker.py:210, in biglearning.module2.common.moduleinvoker._module_run()
    File module2/modules/hftrade/v2/__init__.py:417, in biglearning.module2.modules.hftrade.v2.__init__.bigquant_run()
    File module2/modules/hftrade/v2/__init__.py:257, in biglearning.module2.modules.hftrade.v2.__init__.bigquant_run.do_backtest_run()
    File module2/common/modulemanagerv2.py:88, in biglearning.module2.common.modulemanagerv2.BigQuantModuleVersion.__call__()
    File module2/common/moduleinvoker.py:370, in biglearning.module2.common.moduleinvoker.module_invoke()
    File module2/common/moduleinvoker.py:292, in biglearning.module2.common.moduleinvoker._invoke_with_cache()
    File module2/common/moduleinvoker.py:253, in biglearning.module2.common.moduleinvoker._invoke_with_cache()
    File module2/common/moduleinvoker.py:212, in biglearning.module2.common.moduleinvoker._module_run()
    File module2/modules/hfbacktest/v1/__init__.py:613, in biglearning.module2.modules.hfbacktest.v1.__init__.BigQuantModule.run()
    File module2/modules/hfbacktest/v1/__init__.py:550, in biglearning.module2.modules.hfbacktest.v1.__init__.BigQuantModule.run_algo()
    File /var/app/enabled/bigtrader2/bigtrader/run_trading.py:650, in run_backtest(start_date, end_date, strategy, strategy_setting, capital_base, product_type, frequency, instruments, options_data, **kwargs)
        634     capital_base = kwargs.pop("capital")
        636 rt = RunTrading(RunMode.BACKTEST,
        637                 acct_type=acct_type,
        638                 account_id=account_id,
       (...)
        648                 options_data=options_data,
        649                 **kwargs)
    --> 650 return rt.run(**kwargs)
    File /var/app/enabled/bigtrader2/bigtrader/run_trading.py:401, in RunTrading.run(self, **kwargs)
        398         return
        400 if self.trading_env.run_mode == RunMode.BACKTEST:
    --> 401     return self._run_backtest(**kwargs)
        402 else:
        403     if not self.trading_env.account_id:
    File /var/app/enabled/bigtrader2/bigtrader/run_trading.py:428, in RunTrading._run_backtest(self, **kwargs)
        426     debug_print("run_backtest() running...")
        427 t0 = time.time()
    --> 428 bkt_engine.run()
        429 cost_time = round(time.time() - t0, 3)
        430 if show_debug_info:
    File bigtrader/strategy/backtest_engine.py:318, in bigtrader2.bigtrader.strategy.backtest_engine.BacktestEngine.run()
    File bigtrader/strategy/backtest_engine.py:549, in bigtrader2.bigtrader.strategy.backtest_engine.BacktestEngine.transform()
    File bigtrader/strategy/backtest_engine.py:518, in bigtrader2.bigtrader.strategy.backtest_engine.BacktestEngine.transform.replay_bars_dt()
    File bigtrader/event/event_engine.py:88, in bigtrader2.bigtrader.event.event_engine.EventEngine.process_events()
    File bigtrader/event/event_engine.py:83, in bigtrader2.bigtrader.event.event_engine.EventEngine.process_events()
    File bigtrader/event/event_engine.py:131, in bigtrader2.bigtrader.event.event_engine.EventEngine._process_without_lock()
    File bigtrader/strategy/engine.py:197, in bigtrader2.bigtrader.strategy.engine.StrategyEngine.process_bars_event()
    File bigtrader/strategy/engine.py:617, in bigtrader2.bigtrader.strategy.engine.StrategyEngine._call_strategy_func()
    File bigtrader/strategy/engine.py:608, in bigtrader2.bigtrader.strategy.engine.StrategyEngine._call_strategy_func()
    File bigtrader/strategy/strategy_base.py:2221, in bigtrader2.bigtrader.strategy.strategy_base.StrategyBase.call_handle_data()
    Cell In[20], line 36, in m4_handle_data_bigquant_run(context, data)
         34 for ins in instruments:
         35     if ins not in holding_list and data.can_trade(context.symbol(ins)) and len(holding_list)<10:
    ---> 36         context.order_target_percent(context.symbol(ins), 1/10)
         37         holding_list.append(ins)
    File bigtrader/strategy/strategy_base.py:863, in bigtrader2.bigtrader.strategy.strategy_base.StrategyBase.order_target_percent()
    TypeError: Argument 'symbol' has incorrect type (expected str, got EquityContractData)
    

\

标签

损失函数机器学习回测XGBoost函数
评论
  • data的那个报错, 上面应该还有代码:
  • import pandas as pd
  • data = pd.merge(df, label, on=['date', 'instrument'], how='inner')
  • data
  • 你运行了没
  • data这个报错运行过后解决了,回测的报错不知道怎么处理,谢谢大佬
  • 试试吧context.symbol去掉?
  • 谢谢,无奈自己基础太差改不出来….只有等学习一段时间看看能不能把问题修正…..