怎么修改为直接卖出,不用排序法


(vinjie2018) #1
# 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:
        context.order_target(context.symbol(instrument), 0)
        cash_for_sell -= positions[instrument]
        if cash_for_sell <= 0:
            break

(vinjie2018) #2

用排序法,有时前一天买入一只股票,第二没有卖出,导致持仓满了,不能买其它股票。


(达达) #3

把逻辑换成止盈止损就好了,看你需要什么卖出条件

    #------------------------------------------止损模块START--------------------------------------------
    date = data.current_dt.strftime('%Y-%m-%d')
    costs = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
    # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
    current_stoploss_stock = [] 
    if len(costs) > 0:
        for i in costs.keys():
            stock_cost = costs[i] 
            stock_market_price = data.current(context.symbol(i), 'price') 
            # 亏5%就止损
            if (stock_market_price - stock_cost) / stock_cost <= -0.05:   
                context.order_target_percent(context.symbol(i),0)     
                current_stoploss_stock.append(i)
                print('日期:',date,'股票:',i,'出现止损状况')
    #-------------------------------------------止损模块END--------------