问题描述
/
解决方案
根据 ptrade API 文档规定,get_market_list() 接口在回测和交易模块中,仅限在 before_trading_start(盘前处理)和 after_trading_end(盘后处理)这两个函数中使用。
如果在 initialize 或 handle_data 中调用,就会触发“仅限特定函数中使用”的报错。
正确的代码示例
您可以将获取市场列表的逻辑移至 before_trading_start 中:
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def before_trading_start(context, data):
# 在盘前函数中正确调用 get_market_list
market_list = get_market_list()
log.info("当前市场列表:\n%s" % market_list)
def handle_data(context, data):
pass
补充说明:
同理,获取市场详细信息的接口 get_market_detail(finance_mic) 也受此限制,仅能在 before_trading_start 和 after_trading_end 中调用。