I'm trying to place a limit order at a moving average when a set of conditions are meant, which is working fine. But I would like the order to be updated at the close of every bar to follow the average up/down. Pretty new to Pine Script and don't know if this is possible.
Here is my code:
//SETUP
setupSqueeze = BB_lower >= KC_lower_mid or BB_upper <= KC_upper_mid ? 1 : 0
setupSqueezeSum = math.sum(setupSqueeze, 8)
//-------------------------------------------------------------------------------------------------------------------------------------
//BREAKOUT
breakOut = BB_lower < KC_lower_low or BB_upper > KC_upper_low ? 1 : 0
breakOutSum = math.sum(breakOut, 2)
//-------------------------------------------------------------------------------------------------------------------------------------
//TRADING CONDITIONS
condition1 = setupSqueezeSum >= 2 and breakOutSum >= 2
//-------------------------------------------------------------------------------------------------------------------------------------
if (condition1 == true and close > ema4 and strategy.position_size == 0)
strategy.entry("Long", strategy.long, limit = ema3)
strategy.exit("Long Stop", from_entry = "Long", loss = 2000, profit = 4000)
if (condition1 == true and close < ema4 and strategy.position_size == 0)
strategy.entry("Short", strategy.short, limit = ema3)
strategy.exit("Short Stop", from_entry = "Short", loss = 2000, profit = 4000)
You can use strategy.opentrade to test if a trade is on.
You can use id_entry to know if the actual trade is a long or a short.
Then adjust the exit parameters to your wish :
if strategy.opentrade > 0
if strategy.opentrades.entry_id(strategy.opentrades-1)=="Long"
strategy.exit("Long Stop", from_entry = "Long", loss = NewValueLoss, profit = NewValueProfit)
else if strategy.opentrades.entry_id(strategy.opentrades-1)=="Short"
strategy.exit("Short Stop", from_entry = "Short", loss = 2000, profit = 4000)