qlib icon indicating copy to clipboard operation
qlib copied to clipboard

分钟频数据回测问题

Open hanson-young opened this issue 1 year ago • 7 comments

❓ Questions and Help

在实例examples/highfreq的yaml文件中没有回测部分的配置

我进行了简单的配置,用自己的因子数据进行训练,采用TopNDropout策略

回测出来发现无费收益跑赢了benchmark(沪深300 ),但如果算上手续费差异非常大,不应该如此离谱,然后对持仓进行了分析,发现它每分钟都会进行交易,前一分钟买入的股票,下一分钟可能就卖出了,这显然是不合理的,因为不符合A股的交易规则。如下图 image

我最终定位到qlib的持仓设计上 qlib\backtest\position.py::Position。对于A股来说,持仓需要考虑到三个概念,昨日持仓,总持仓,可卖持仓,但是qlib中仅仅只有一个amount市值。那么在回测时,就必然会出现把今天买入的持仓今日卖出,这是严格意义上的T0交易。 在Strategy和Executor、Exchange的实现上,也并没有发现阻止这种情况发生的代码。

hanson-young avatar Jan 25 '24 03:01 hanson-young

还是没人回复吗?我自己实现了一个HighFreqTopkDropoutStragegy解决了这个问题......

hanson-young avatar Jan 29 '24 07:01 hanson-young

其实你可以得到预测结果文件后,拿到其他回测平台去回测,比如放到backtrader去回测,这样可以完全自己控制t0问题。qlib系列二教程里有怎样在backtrader中拿到qlib预测结果文件,执行定制回测策略的例子。

quant2008 avatar Jan 30 '24 00:01 quant2008

还是没人回复吗?我自己实现了一个HighFreqTopkDropoutStragegy解决了这个问题......

请问你可以分享你实现的HighFreqTopkDropoutStragegy吗?下面卖课的真的是。

shijie2016 avatar Feb 02 '24 08:02 shijie2016

How to get the transaction detail in your picture in non_highfreq mode?

Tsuruko04 avatar Feb 14 '24 18:02 Tsuruko04

To get the transaction details, you can also refer to #1655, where we discussed more detailed. In sort, change the yaml to this:

---
qlib_init:
  ...
task:
  model:
    ...
  dataset:
    ...
  record:
    - class: SignalRecord
      module_path: qlib.workflow.record_temp
      kwargs:
        ...
    - class: PortAnaRecord
      module_path: qlib.workflow.record_temp
      kwargs:
        config:
          executor:
            class: SimulatorExecutor
            module_path: qlib.backtest.executor
            kwargs:
              time_per_step: day
              generate_portfolio_metrics: True
              verbose: True
          strategy:
            ...
          backtest:
            ...

I add

executor: 
class: SimulatorExecutor
module_path: qlib.backtest.executor
kwargs: 
time_per_step: day 
generate_portfolio_metrics: True 
verbose: True

to port_analysis_config: &port_analysis_config and it works.

(It it not to the initial question)

Imbernoulli avatar Feb 14 '24 18:02 Imbernoulli

还是没人回复吗?我自己实现了一个HighFreqTopkDropoutStragegy解决了这个问题......

您好,请问您方便一下您写的这个策略学习一下吗?感觉逻辑像是添加一个$buyDate然后判断$buyDate需要和¥today不等于才能进行交易?

l0ngc avatar Mar 27 '24 20:03 l0ngc

@l0ngc 其实逻辑都差不多,就是对每一份买卖标注下日期

hanson-young avatar Mar 28 '24 02:03 hanson-young