3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
关于 JoinQuant 聚宽平台 get_index_weights 接口的数据更新时间以及如何避免未来函数,根据官方 API 文档的说明,具体情况如下:
您完全不需要担心在正常调用时会引入未来函数,原因如下:
get_index_weights(index_id, date=None) 且不传入 date 参数时,系统会默认使用 context.current_dt.date()(即策略当前运行到的逻辑日期)。context.current_dt 之后的数据。get_index_stocks 和 get_index_weights。”为了确保策略的严谨性,建议在策略中这样调用:
def handle_data(context, data):
# 不传入 date 参数,默认使用 context.current_dt.date()
# 这样获取到的永远是策略当前时刻所能看到的最新历史权重,绝无未来函数
weights_df = get_index_weights('000300.XSHG')
# 如果需要明确指定日期,请确保传入的日期 <= context.current_dt.date()
# weights_df = get_index_weights('000300.XSHG', date=context.previous_date)
总结:您不需要刻意去卡“月末交易日开盘前的8点”这个时间点。只要在回测/模拟中依赖 context.current_dt(默认行为),聚宽的底层引擎会自动为您匹配当时真实可见的最新权重数据,从根本上杜绝了未来函数的问题。