Pine Script: strategy.exit with profit parameter executing at next bar open instead of intrabar

76 views Asked by At

I am working on a strategy in TradingView using Pine Script. I have a line of code where I am using the strategy.exit function with the profit parameter to set a profit target for exiting a short position:

strategy.exit("Win", "Short", profit=TP_Perc)

I expect this to execute intrabar when the profit target TP_Perc is reached. However, for some reason, it is executing at the open of the next candle.

Despite having "use_bar_magnifier" set to true, which should allow for more precise order fills during backtesting by inspecting smaller timeframes, the strategy.exit function seems to be waiting for the next available price tick before filling the order.

I have ensured that all necessary conditions are met on my TradingView account and am certain that this is an issue with my code, not with TradingView itself.

Is there something I am missing or misunderstanding regarding the behavior of the strategy.exit function with the profit parameter in Pine Script? How can I achieve intrabar execution for this exit order based on the specified profit target? Any insights or suggestions would be greatly appreciated.

I have tried using a few different argument combinations limit, stop and one with a combination of both.

if (na(entryPrice) == false) 
    if (high > stopLossLevel)
        strategy.close("Short", "Loss")

    else if (low <= takeProfitLevel)
        //strategy.exit("Win", "Short", limit=takeProfitLevel)
        //strategy.exit("Win", "Short", limit=takeProfitLevel, stop=takeProfitLevel)
        strategy.exit("Win", "Short", profit=TP_Perc)
0

There are 0 answers