I want to exit a certain positions when certain conditions are met. My conditions are following:
- TP1 – When price passes a calculated %. This is not a static value, but for simplicity, let’s make this 5%.
- TP2 – Same as TP1 but when it passes 10%.
- SL – When price dips below certain %.
- SL to BE – When price passes a certain threshold e.g. 3%.
Now I tried everything I guess. strategy.exit()
and strategy.close()
and their combinations and I always face a problem, where one thing cancels or ignores another and it’s just a hell of a mess to handle.
CASE1: Using strategy.close()
, it never triggers TP2
; TP1
and SL
work fine.
Code for case1:
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)
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)
strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true)
//strategy.exit("SL", from_entry = tradeId, stop = entryPrice)
<code>// 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)
</code>
// 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: If I switch to using strategy.exit()
, only POP
– condition #4 and SL
– condition #3 work.
Code for case2:
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)
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)
//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)
<code>// 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)
enter code here
</code>
// 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)
enter code here
CASE3: If I comment out the condition #4 and use strategy.exit()
all 3 things work TP1
, TP2
and SL
work fine.
Code for case3 (same as case 2, with added condition for #4):
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)
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)
//strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true)
strategy.exit("SL", from_entry = tradeId, stop = entryPrice)
<code>// 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)
</code>
// 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)