问答交流

AI策略回测主函数如何加入均线判断?

由pudong365创建,最终由small_q 被浏览 22 用户

你好!请问AI选股策略输入特征无均线特征量,在回测部分特征抽取也是前述特征量,在最后回测部分卖出时想加上均线判断:

2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按StockRanker预测的排序末位淘汰

if not is_staging and cash_for_sell > 0:
    equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}
    instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(
            lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))
    # print('rank order for sell %s' % instruments)
    for instrument in instruments:
        #if mean(close_0,5)<mean(close_0,20)   and  shift(mean(close_0,5),1)>shift(mean(close_0,20),1):
        if context.symbol(instrument).mean(close_0,5)<context.symbol(instrument).mean(close_0,20) :
            context.order_target(context.symbol(instrument), 0)
            cash_for_sell -= positions[instrument]
        
        if cash_for_sell <= 0:
            break

但是无论是mean(close_0,5) 还是 context.symbol(instrument).mean(close_0,5) 取值都会出错,请问这个均线值回测部分如何写呢?

<ipython-input-2-6c6956ee1b3e> in m12_handle_data_bigquant_run(context, data) 43 for instrument in instruments: 44 #if mean(close_0,5)<mean(close_0,20) and shift(mean(close_0,5),1)>shift(mean(close_0,20),1): ---> 45 if context.symbol(instrument).mean(close_0,5)<context.symbol(instrument).mean(close_0,20) : 46 context.order_target(context.symbol(instrument), 0) 47 cash_for_sell -= positions[instrument]

AttributeError: 'zipline.assets._assets.Equity' object has no attribute 'mean'

标签

选股策略输入特征回测函数均线
评论
  • 回测模块里没法用表达式引擎的表示方式,只能用numpy或者是pandas的求滚动均值的方法。
{link}