如何在PineScript中管理多离场条件?实操问题求助
多条件平仓策略实现问题
需求
希望在满足以下特定条件时平仓:
- TP1 - 价格达到计算出的百分比(示例设为5%,实际为动态值)时平仓对应比例仓位
- TP2 - 逻辑同TP1,阈值为10%时平仓剩余对应比例仓位
- SL - 价格下跌至特定百分比以下时全部平仓
- 止损移至盈亏平衡点(BE) - 当盈利达到特定阈值(如3%)时,将止损位调整至入场价
问题现状
尝试使用strategy.exit()、strategy.close()及其组合实现,但始终存在条件互相抵消或失效的问题,逻辑处理混乱。
CASE1:仅使用strategy.close()
表现:TP1和SL正常触发,但TP2从未生效。
代码:
// TP1 and SL BE if (tp1_cond and not tp1_taken) //and not tp1_taken) strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId)) //strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs //strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess array.set(tp1_taken_arr, tp1_index, true) // TP2 if (tp2_cond and not tp2_taken) strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId)) //strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp2perc) array.set(tp2_taken_arr, tp2_index, true) // Stop-loss condition if (stopLossCond) strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true) //strategy.exit("SL", from_entry = tradeId, stop = entryPrice)
CASE2:仅使用strategy.exit()
表现:仅条件4(移止损到BE)和条件3(SL)生效,TP1、TP2失效。
代码:
// TP1 and SL BE if (tp1_cond and not tp1_taken) //and not tp1_taken) //strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId)) strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs //strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess array.set(tp1_taken_arr, tp1_index, true) // TP2 if (tp2_cond and not tp2_taken) //strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId)) strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp2perc) array.set(tp2_taken_arr, tp2_index, true) // Stop-loss condition if (stopLossCond) //strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true) strategy.exit("SL", from_entry = tradeId, stop = entryPrice) // Check if price has popped by the break-even threshold and move stop loss to break-even - increased peformance of the strategy and decreased the drawdown - if set specifically at 3%, needs some experimenting if (currentProfit >= slBeThreshold) strategy.exit("POP - Move to BE: "+str.tostring(tradeId), from_entry=tradeId, stop=entryPrice)
CASE3:注释条件4后使用strategy.exit()
表现:TP1、TP2和SL均可正常生效,但缺少止损移至BE的功能。
代码:
// TP1 and SL BE if (tp1_cond and not tp1_taken) //and not tp1_taken) //strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId)) strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs //strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess array.set(tp1_taken_arr, tp1_index, true) // TP2 if (tp2_cond and not tp2_taken) //strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId)) strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp2perc) array.set(tp2_taken_arr, tp2_index, true) // Stop-loss condition if (stopLossCond) //strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true) strategy.exit("SL", from_entry = tradeId, stop = entryPrice)
EDIT1:当前最优组合(仍有局限)
表现:能正常执行TP1、TP2和SL,但无法处理条件4(止损移至BE),也无法扩展更多平仓条件。
代码:
// TP1 and SL BE if (tp1_cond and not tp1_taken) //and not tp1_taken) strategy.close(tradeId, qty = tradeQty * tp1perc/100, comment="TP1: "+str.tostring(tradeId)) //strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty = tradeQty * tp1perc) // one exit order somehow cancels another, despite different IDs //strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess array.set(tp1_taken_arr, tp1_index, true) // TP2 if (tp2_cond and not tp2_taken) //strategy.close(tradeId, qty = tradeQty * tp1perc/100, comment="TP2: "+str.tostring(tradeId)) strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty = tradeQty * tp2perc/100) array.set(tp2_taken_arr, tp2_index, true) // Stop-loss condition if (stopLossCond) //strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true) strategy.exit("SL: "+str.tostring(tradeId), from_entry = tradeId, stop = entryPrice)
内容的提问来源于stack exchange,提问作者Ondrej Tokar
相关产品推荐
相关产品推荐

