'Trying to use ATR for stops and profit orders but it keeps referencing a different ATR
So I am trying to set take profit and stop loss orders using ATR. The way I would want this to work is when the buy or sell condition is met it would use the current ATR in the stop/profit calculations, so my stop/profit would not change for that trade. My problem is when I plot my profit and stop loss orders I can see them changing when a position is open. Here is the relevant part of the code:
profit = input(title="Profit percent", type=input.float, defval=5.0)
stoploss = input(title="Stop Percent", type=input.float, defval=2.0)
atr = valuewhen((BuySignal or SellSignal),atr(14), 0)
limitlong = strategy.position_avg_price+(atr*profit)
stoplong = strategy.position_avg_price-(atr*stoploss)
limitshort = strategy.position_avg_price-(atr*profit)
stopshort = strategy.position_avg_price+(atr*stoploss)
strategy.entry("Long", true, when=BuySignal, comment="Buy")
strategy.exit("Close Long", "Long", limit=limitlong, stop=stoplong, comment="Close Long")
strategy.entry("Short", false, when=SellSignal, comment="Short")
strategy.exit("Close Short", "Short", limit=limitshort, stop=stopshort, comment="Close Short")
Any help is appreciated. I'm also using v4 if that matters.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
