You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JoinQuant策略报错:filter_paused函数触发TypeError类型错误

问题修复方案

核心错误原因

  1. filter_st函数返回值错误:你把过滤后的股票列表转成了字符串(return str(non_st_list)),导致传给filter_paused的不是股票代码列表,而是一个字符串。遍历字符串时,stock变成了单个字符,执行curr_data[stock]自然触发"字符串索引必须是整数"的错误。
  2. filter_paused函数逻辑错误:你把传入的股票代码列表直接赋值给curr_data,但curr_data需要的是包含股票行情数据的对象,不是代码列表。

具体修复步骤

1. 修正filter_st函数的返回值

去掉字符串转换,直接返回股票代码列表:

def filter_st(stocks, day):
   dataframe = get_extras('is_st', stocks, start_date = day, end_date = day, df = True)
   non_st_list = []
   for i in list(dataframe.columns):
        if not dataframe[i].bool():
           non_st_list.append(i)
   # 直接返回列表,不要转成字符串
   return non_st_list

2. 修正filter_paused函数的行情数据获取逻辑

使用handle_data传入的data参数(当前时间点的行情数据集)来获取股票状态,同时增加存在性判断避免KeyError:

# 修改filter_paused,接收data参数
def filter_paused(stocks, data):
    stock_list = [stock for stock in stocks if stock in data and not (
        data[stock].paused or
        (data[stock].day_open == data[stock].high_limit) or
        (data[stock].day_open == data[stock].low_limit)
        )]
    return stock_list

3. 修正handle_data中的调用逻辑

调用filter_paused时传入data参数:

def handle_data(context, data):
    # Get the current date in the form 20XX-XX-XX
    current_date = context.current_dt.date()
    # Get all stocks
    all_stocks = list(get_all_securities(types = ['stock'], date = None).index)
    # Filter ST stocks with the base of all_stocks in the previous step
    non_st_stocks = filter_st(all_stocks, current_date)
    # Filter paused stocks on top of non_st_stocks in the previous step
    filtered_stocks = filter_paused(non_st_stocks, data)
    # Print the remaining stock list after filtering ST stocks and paused stocks
    print(filtered_stocks)

额外优化建议

可以将filter_st函数简化为列表推导式,提升代码简洁性:

def filter_st(stocks, day):
    dataframe = get_extras('is_st', stocks, start_date=day, end_date=day, df=True)
    return [code for code in dataframe.columns if not dataframe[code].bool()]

内容的提问来源于stack exchange,提问作者Evariste Galois

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 01:30:26