I have a PineScript strategy that executes trades when certain conditions are met. Currently, the strategy places up to 5 trades and automatically updates the stop loss to breakeven once the first take profit (TP1) is reached.
Since my strategy runs every 5 minutes, if I take 5 trades and the entry conditions are not met on the next candle, will my code for updating the stop loss to breakeven still work if it’s within the entry condition block?
My question is whether the script’s logic for managing existing trades operates independently from the conditions for opening new trades. Specifically, if a trade has already been placed and the entry conditions for new trades are not met on subsequent candles, will the active trade continue to be managed according to the existing stop loss (SL) and take profit (TP) rules?
// Adding entry price and flag for this trade group
array.push(longEntryPrices, buyOrSellPrice)
array.push(longTP1Hit, false)
// Stoploss is lowest of last 5 candles
slLong = ta.lowest(5)
if waitForLong
currentPrice := close
buyOrSellPrice := currentPrice + continuationPrice
// Adding entry price and flag for this trade group
array.push(longEntryPrices, buyOrSellPrice)
array.push(longTP1Hit, false)
for i = 1 to 5
if slLong > currentPrice
slLong := longCandleLow - spreadValue
longProfit := (buyOrSellPrice + ((buyOrSellPrice - slLong) * i))
else
longProfit := (buyOrSellPrice + ((buyOrSellPrice - actualSlLong) * i))
strategy.entry("Long " + str.tostring(i), strategy.long, qty=RiskValue, limit=buyOrSellPrice, stop=slLong)
// Update SL to breakeven for remaining trades if TP1 is hit
if not array.get(longTP1Hit, array.size(longEntryPrices) - 1) and (high >=(buyOrSellPrice + ((buyOrSellPrice - actualSlLong))))
breakevenPrice = buyOrSellPrice
for j = i + 1 to 5
strategy.exit("Long Exit " + str.tostring(j), "Long " + str.tostring(j), sto=breakevenPrice)
array.set(longTP1Hit, array.size(longEntryPrices) - 1, true)
So Is the “waitForLong” is irrespective of Update SL to breakeven for remaining trades if TP1 is hit