问题描述
大意是获取回测日期前22天数据进行分析,代码如下:df = get_price(index,startdate='2018-01-01', end_date=today, fields=['open','high','low','close'], count=22)
日志提示:avoid_future_data=True时,get_price的end_date(2022-11-30 00:00:00)不能大于当前日期(2018-01-02 09:30:00),回测时间是2018-01-02日。如何避免未来函数,取得上述数据?求大神不吝赐教。
解决方案
问题分析
在聚宽(JoinQuant)的回测环境中,报错提示 get_price的end_date不能大于当前日期 是因为您在代码中传入的 today 变量获取的是现实世界中的今天(例如 2022-11-30),而回测引擎当前运行到的逻辑时间是 2018-01-02。在 2018 年去获取 2022 年的数据,这就构成了典型的“未来函数”。
为了避免未来函数,您需要让 end_date 动态跟随回测引擎的当前时间。
解决方案
方法一:正确使用 get_price(使用 context.current_dt)
在回测的 handle_data 或定时运行函数中,可以通过 context.current_dt 获取回测当前的逻辑时间,或者通过 context.previous_date 获取前一个交易日。同时请注意,get_price 的 start_date 和 count 不能同时使用(二选一)。
修改后的代码:
def handle_data(context, data):
# 使用 context.current_dt 作为结束时间,获取过去22天的数据
df = get_price(index,
end_date=context.current_dt,
fields=['open','high','low','close'],
count=22)
print(df)
方法二:使用回测专用 API history 或 attribute_history(强烈推荐)
在回测环境中,聚宽官方更推荐使用 history 或 attribute_history。这两个函数是专门为回测设计的,它们默认以当前回测时间为基准,自动向后取数据,天生屏蔽了未来函数,且运行速度通常比 get_price 更快。
获取单只标的多个字段(使用 attribute_history):
def handle_data(context, data):
# 获取单只标的过去22天的天数据(不包含当天)
df = attribute_history(index, 22, '1d', ['open','high','low','close'])
print(df)
获取多只标的单个字段(使用 history):
def handle_data(context, data):
# 获取多只标的过去22天的收盘价
df = history(22, '1d', 'close', security_list=[index, '000001.XSHE'])
print(df)
总结建议
- 时间锚点:在回测中永远不要使用
datetime.date.today(),而应该使用context.current_dt。 - API选择:研究环境(Research)中多用
get_price;回测环境(Backtest)中取历史K线优先使用history、attribute_history或get_bars。