I'm setting a take profit value 1:1 to the risk. It's based on ATR. Once the price closes above/below atr it gives me the entry point and after that, I calculate the distance between the entry price and the ATR. So this distance will be as same as my TP. But what happens is that the bot keeps getting the ATR level from the last bar, and not from the entry bar.
Here's the sample of the code and a screenshot of a trade:
//@version=4
buy = crossover(close,atr)
sell = crossunder(close,atr)
strategy.entry("long", true, when = buy)
strategy.entry("short", false, when = sell)
//calculate difference between entry and atr
differenceLong = strategy.position_avg_price - atr
differenceShort = atr - strategy.position_avg_price
//Calculate the TP price
longExitPrice = strategy.position_avg_price + differenceLong
shortExitPrice = strategy.position_avg_price - differenceShort
if (strategy.position_size > 0)
strategy.exit(id="TP", limit = longExitPrice)
if (strategy.position_size < 0)
strategy.exit(id="TP", limit = shortExitPrice)
