Strategy.exit()执行部分止盈后无法识别需平仓总仓位的问题
Pine Script 策略问题:部分止盈后
strategy.exit()无法平仓全部加仓仓位 问题现象
执行部分止盈后调用strategy.exit(),仓位仍有部分留存,加仓订单未被计入需平仓的总仓位;若关闭部分止盈功能,strategy.exit()可正常平仓所有加仓仓位。
场景说明
- 无部分止盈时:
strategy.exit()成功平仓全部仓位 - 使用部分止盈后:
strategy.exit()仅平仓部分仓位,加仓部分留存
相关代码
if GreenCase() // 开多单 tp1 := false // 部分止盈标志位 tp2 := false if strategy.position_size < 0 // 若有空单,先平仓反转 strategy.cancel("SELL") strategy.close("SELL", comment="REVERSE") adds := 0 strategy.entry("BUY", strategy.long, pos_qty) strategy.exit("Buy_exit", "BUY", limit=lpp, stop=lsp) else if strategy.position_size == 0 adds := 0 strategy.entry("BUY", strategy.long, pos_qty) strategy.exit("Buy_exit", "BUY", limit=lpp, stop=lsp) if RedCase() // 开空单 tp1 := false tp2 := false if strategy.position_size > 0 strategy.cancel("BUY") strategy.close("BUY", comment="REVERSE") adds := 0 strategy.entry("SELL", strategy.short, pos_qty) strategy.exit("Sell_exit", "SELL", limit=spp, stop=ssp) else if strategy.position_size == 0 adds := 0 strategy.entry("SELL", strategy.short, pos_qty) strategy.exit("Sell_exit", "SELL", limit=spp, stop=ssp) if strategy.position_size < 0 and close > currEntryPrice+(percentNewEntry*(currEntryPrice/100)) adds+=1 strategy.order("SELL", strategy.short, pos_add_qty*adds, comment="Add "+str.tostring(adds)) tp1 := false tp2 := false if strategy.position_size > 0 and close < currEntryPrice-(percentNewEntry*(currEntryPrice/100)) adds+=1 strategy.order("BUY", strategy.long, pos_add_qty*adds, comment="Add "+str.tostring(adds)) tp1 := false tp2 := false if partial // 部分止盈开关 if strategy.position_size > 0 if close >= original_entry_price + partial_1_level*(original_long_TP-original_entry_price)/100 and tp1 == false strategy.close("BUY", qty_percent = partial_1_percent, immediately=true, comment="Partial TP 1", alert_message="") tp1 := true if close >= original_entry_price + partial_2_level*(original_long_TP-original_entry_price)/100 and tp2 == false strategy.close("BUY", qty_percent = partial_2_percent, immediately=true, comment="Partial TP 2", alert_message="") tp2 := true if strategy.position_size < 0 if close <= original_entry_price - partial_1_level*(original_entry_price-original_short_TP)/100 and tp1 == false strategy.close("SELL", qty_percent = partial_1_percent, immediately=true, comment="Partial TP 1", alert_message="") tp1 := true if close <= original_entry_price - partial_2_level*(original_entry_price-original_short_TP)/100 and tp2 == false strategy.close("SELL", qty_percent = partial_2_percent, immediately=true, comment="Partial TP 2", alert_message="") tp2 := true
问题原因
Pine Script中,strategy.exit()默认仅与绑定的strategy.entry()初始仓位关联,后续通过strategy.order()加仓的仓位不会自动纳入该exit订单的跟踪范围。当使用strategy.close()执行部分止盈后,原exit订单的目标平仓量仍为初始entry的剩余仓位,无法覆盖加仓部分,因此触发exit时仅平仓初始剩余仓位,加仓仓位留存。
解决方案
方案1:加仓/部分止盈后重新创建exit订单
每次加仓或部分止盈操作后,取消原exit订单并重新创建,确保新exit订单跟踪当前全部仓位:
修正加仓代码
if strategy.position_size < 0 and close > currEntryPrice+(percentNewEntry*(currEntryPrice/100)) adds+=1 strategy.order("SELL", strategy.short, pos_add_qty*adds, comment="Add "+str.tostring(adds)) tp1 := false tp2 := false // 取消原空单exit并重新创建 strategy.cancel("Sell_exit") strategy.exit("Sell_exit", "SELL", limit=spp, stop=ssp) if strategy.position_size > 0 and close < currEntryPrice-(percentNewEntry*(currEntryPrice/100)) adds+=1 strategy.order("BUY", strategy.long, pos_add_qty*adds, comment="Add "+str.tostring(adds)) tp1 := false tp2 := false // 取消原多单exit并重新创建 strategy.cancel("Buy_exit") strategy.exit("Buy_exit", "BUY", limit=lpp, stop=lsp)
修正部分止盈代码
if partial // 部分止盈开关 if strategy.position_size > 0 if close >= original_entry_price + partial_1_level*(original_long_TP-original_entry_price)/100 and tp1 == false strategy.close("BUY", qty_percent = partial_1_percent, immediately=true, comment="Partial TP 1", alert_message="") tp1 := true // 重新创建多单exit strategy.cancel("Buy_exit") strategy.exit("Buy_exit", "BUY", limit=lpp, stop=lsp) if close >= original_entry_price + partial_2_level*(original_long_TP-original_entry_price)/100 and tp2 == false strategy.close("BUY", qty_percent = partial_2_percent, immediately=true, comment="Partial TP 2", alert_message="") tp2 := true // 重新创建多单exit strategy.cancel("Buy_exit") strategy.exit("Buy_exit", "BUY", limit=lpp, stop=lsp) if strategy.position_size < 0 if close <= original_entry_price - partial_1_level*(original_entry_price-original_short_TP)/100 and tp1 == false strategy.close("SELL", qty_percent = partial_1_percent, immediately=true, comment="Partial TP 1", alert_message="") tp1 := true // 重新创建空单exit strategy.cancel("Sell_exit") strategy.exit("Sell_exit", "SELL", limit=spp, stop=ssp) if close <= original_entry_price - partial_2_level*(original_entry_price-original_short_TP)/100 and tp2 == false strategy.close("SELL", qty_percent = partial_2_percent, immediately=true, comment="Partial TP 2", alert_message="") tp2 := true // 重新创建空单exit strategy.cancel("Sell_exit") strategy.exit("Sell_exit", "SELL", limit=spp, stop=ssp)
方案2:改用strategy.close_all()触发平仓
若无需区分初始与加仓仓位,可在exit条件触发时直接调用strategy.close_all()替代strategy.exit(),确保平仓全部仓位。
内容的提问来源于stack exchange,提问作者Blu
相关产品推荐
相关产品推荐

