问答交流

怎么设置安条件卖出

由youquan创建,最终由small_q 被浏览 28 用户

我打算设置涨10%卖,跌10%止损,请问应该怎么设置,是在主函数里代码设置吗?哪位大神给个例子学习下,谢谢

标签

金融交易
评论
  • 拿去参考,稍微修改修改就行 ``` equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0} positions_stop={e.symbol:p.cost_basis for e,p in context.portfolio.positions.items()} # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断 stoploss_stock = [] stopwin_stock=[] if len(equities) > 0: for i in equities.keys(): stock_cost=positions_stop[i] stock_market_price = data.current(context.symbol(i), 'price') # 最新市场价格 last_sale_date = equities[i].last_sale_date # 上次交易日期 delta_days = data.current_dt - last_sale_date hold_days = delta_days.days # 持仓天数 # 建仓以来的最高价 highest_price_since_buy = data.history(context.symbol(i), 'high', hold_days, '1d').max() # 确定止损位置 stoploss_line = highest_price_since_buy - highest_price_since_buy * 0.07 #record('止损位置', stoploss_line) # 如果价格下穿止损位置 if stock_market_price < stoploss_line: context.order_target_percent(context.symbol(i), 0) stoploss_stock.append(i) if stock_market_price/stock_cost > 1.07 and data.can_trade(context.symbol(i)) and not context.has_unfinished_sell_order(i): context.order_target_percent(context.symbol(i),0) stopwin_stock.append(i) if len(stoploss_stock)>0: print('日期:', today, '股票:', stoploss_stock, '出现跟踪止损状况') if len(stopwin_stock)>0: print(today,'止盈股票列表',stopwin_stock) ``` \
  • 非常感谢
{link}