ATR TRAILING STOP
Forums › ProRealTime English forum › ProBuilder support › ATR TRAILING STOP
- This topic has 28 replies, 4 voices, and was last updated 5 years ago by nfulcher.
-
-
09/22/2019 at 8:42 AM #108134
Hi everyone, I have imported the ATR STOP indicator from the library as shown below. It works great although I would like the indicator to move closer to the original stop level if ATR reduces ( but not move further away if ATR increases). In my attached file I have set the trailing stop at 5 x ATR. From the vertical line I have drawn on the chart you can see as ATR reduces, the trailing stop remains horizontal. Can someone produce the code that would ratchet the stoploss closer?
Kind Regards Simon
1234567891011121314151617181920212223242526272829// Périodep = 14// Average True Range XATRx = AverageTrueRange[p](close) * 3.5// ATRts = ATR Trailing Stop// Inversion de tendanceIF close crosses over ATRts THENATRts = close - ATRxELSIF close crosses under ATRts THENATRts = close + ATRxENDIF// Cacul de l'ATRts lors de la même tendanceIF close > ATRts THENATRnew = close - ATRxIF ATRnew > ATRts THENATRts = ATRnewENDIFELSIF close < ATRts THENATRnew = close + ATRxIF ATRnew < ATRts THENATRts = ATRnewENDIFENDIFreturn ATRts as "ATR Trailing Stop"09/22/2019 at 9:31 AM #10813709/22/2019 at 9:50 AM #10813809/22/2019 at 11:21 AM #108146You could do something like this. If ATR is lower than the previous bars ATR then the distance calculation is adjusted by a factor from 0.1 to 1. This means that if ATR reduces then the stoploss level moves faster towards price.
123456789101112131415161718192021222324252627282930313233343536373839// Périodep = 14factor = 0.7// Average True Range XATRx = AverageTrueRange[p](close) * 3.5// ATRts = ATR Trailing Stop// Inversion de tendanceIF close crosses over ATRts THENATRts = close - ATRxELSIF close crosses under ATRts THENATRts = close + ATRxENDIF// Cacul de l'ATRts lors de la même tendanceif atrx < atrx[1] thenatrtemp = atrx*factorelseatrtemp = atrxendifIF close > ATRts THENATRnew = close - ATRtempIF ATRnew > ATRts THENATRts = ATRnewENDIFendifIF close < ATRts THENATRnew = close + ATRtempIF ATRnew < ATRts THENATRts = ATRnewENDIFendifreturn ATRts as "ATR Trailing Stop"09/22/2019 at 11:42 AM #108149Thanks Vonasi, that looks like exactly what I need. Just noticed on the far right hand side of your attached chart that price breaks down through the trailing stop at around 11,250. Now that the blue and black lines are above the current price, why aren’t they at exactly the same level? Shouldn’t both lines commence at the same point at activation and then the blue line should move closer if ATR reduces?
Cheers Simon
09/22/2019 at 3:47 PM #108167That is because the ATR < ATR[1] adjustment is carried out in the code after the initial calculation of the ATRts value. A simple re-organising of the code fixes this hopefully (not tested).
12345678910111213141516171819202122232425262728293031323334353637// Périodep = 14factor = 0.7// Average True Range XATRx = AverageTrueRange[p](close) * 3.5// Cacul de l'ATRts lors de la même tendanceif atrx < atrx[1] thenatrtemp = atrx*factorelseatrtemp = atrxendif// ATRts = ATR Trailing StopIF close > ATRts THENATRnew = close - ATRtempIF ATRnew > ATRts THENATRts = ATRnewENDIFendifIF close < ATRts THENATRnew = close + ATRtempIF ATRnew < ATRts THENATRts = ATRnewENDIFendif// Inversion de tendanceIF close crosses over ATRts THENATRts = close - ATRxELSIF close crosses under ATRts THENATRts = close + ATRxENDIFreturn ATRts coloured(0,0,255)as "ATR Trailing Stop"09/22/2019 at 9:24 PM #108184Another way of doing it is to set a flag when the line flips from one side to the other to ensure that no factor is applied at that point.
123456789101112131415161718192021222324252627282930313233343536p = 14factor = 0.7ATRx = AverageTrueRange[p](close) * 3.5IF close crosses over ATRts THENATRts = close - ATRxflag = 1ELSIF close crosses under ATRts THENATRts = close + ATRxflag = 1ENDIFif atrx < atrx[1] and not flag thenatrtemp = atrx*factorflag = 0elseatrtemp = atrxflag = 0endifIF close > ATRts THENATRnew = close - ATRtempIF ATRnew > ATRts THENATRts = ATRnewENDIFendifIF close < ATRts THENATRnew = close + ATRtempIF ATRnew < ATRts THENATRts = ATRnewENDIFendifreturn ATRts coloured(0,0,255)as "ATR Trailing Stop"Note: Code edited 20190923 16:42 as I spotted a cut and paste error!
09/24/2019 at 5:43 AM #108300Thanks for your help Vonasi. Is it possible to code something like this? Basically an ATR based trailing stop for long or short positions that is user adjustable by ATR period and also a multiplier ( just like supertrend). That hangs from the highest/lowest CLOSE. Once price CLOSES below/above the trailing stop value , the trailing stop line flips to be a trailing stop for opposite position. The trailing stop would be calculated and move per time period bar/candle BUT would never move further away from price ONLY move closer to it. Like the green line in the attached image ( how it moves with nearly every bar ). But without the little moves away from the current price. I hope this makes sense.
Regards Simon
09/24/2019 at 7:54 AM #108313This indicator is the Chandelier exit, here is the explanation and formula:
Formula and Calculations
Chandelier Exits show two lines – the chandelier exit long and the chandelier exit short. The exit long is used to close long positions whereas the exit short is used to close short positions. Therefore, the rule of the indicator is to close long positions when the price goes below the exit long and to close short positions when the price goes above exit short.
The Chandelier long and short are used to compute the value of ATR. Then, using the recommended setting of 22 periods, the Chandelier exit will calculate the highest high or the lowest low for the same period used to calculate ATR. Lastly, subtract the multiple ATR from the figure obtained for the highest high, and add multiple ATR to the value obtained for the lowest low.
The formulas for the two lines are as follows:
Chandelier Exit Long: n-day Highest High – ATR (n) x Multiplier
Chandelier Exit Short: n-day Lowest Low + ATR (n) x Multiplier
Where:
- N is the default unit period of 22 or the number that the trader chooses.
- The multiplier is the default 3.0 Average True Range.
09/24/2019 at 9:53 AM #108325Hi Nicolas, thanks for jumping in to help as well. It’s not the exact Chandelier exit but would act a bit like it. More like the Supertrend indicator. I’m wanting the indicator to hang from the CLOSE rather than the Chandeliers highest high/lowest low. Also to flip after a CLOSE BELOW whereas the Supertrend flips once the trailing value is TOUCHED. The Chandelier exit calculates the highest high or the lowest low for the same period used to calculate ATR. I’m wanting the indicator to do something different. Lets say I’ve got a long trade on and my exit is a close below 2x ATR(15) from the highest close. I’m wanting the indicator to hang 2 x current bars ATR (15) value down from the highest close. I want the trailing stop to move closer to price every single bar that ATR reduces. If ATR increase from one bar to the next, I want the trailing stop line to remain horizontal rather than moving away from price. When price closes 2x ATR(15) below the highest close in the sequence It flips over to be a trailing stop for a short position. The problem I have with current ATR and Chandelier trailing stops is this. The trailing stop line often remains horizontal for certain periods whereas I would like it to tighten up bar by bar ( closer to price) as volatility decreases.
Kind Regards Simon
09/24/2019 at 10:07 AM #108326I’ll add an attachment. Excuse the very crude example. From the red arrow ( highest close in sequence ) the supertrend remains horizontal whilst ATR ( in bottom window ) decreases. My drawn in black line is the trailing stop movement I’m hoping for.
Cheers
09/24/2019 at 10:41 AM #108330Something like this? Note that the trailing stop is not reset to the highest or lowest close once the trend change, but that could be an option?
123456789101112131415161718192021222324252627282930313233343536373839404142p=14mult=2atr = AverageTrueRange[p](close) * multonce trend=1//once ts=closeif close crosses over ts thentrend=1elsif close crosses under ts thentrend=-1endif//ts=ts[1]if trend=1 thenhh=max(hh,close)ll=hhif atr<atr[1] thenhhlevel=hh-atrif hhlevel>ts thents=hhlevelendifendifr=0g=168elsell=min(ll,close)hh=0if atr<atr[1] thenlllevel=ll+atrif lllevel<ts thents=lllevelendifendifr=255g=0endifreturn ts coloured(r,g,0) style(line,3)1 user thanked author for this post.
09/24/2019 at 12:07 PM #108341Hi Nicolas, WOW THANKYOU!! I’ve only just studied the blue square area by calculating the ATR (15 ) per bar and it looks great. It creeps closer to price perfectly and exits at the right spot. I set the indicator to 5xATR(15) GREEN and you can see it compared to Supertrend 5/15 BLUE DASHED LINE.
I would love that option of it to reset to the highest/lowest close once the trend changes. I hope its not too much trouble for you.
Many thanks again….the customer support offered by you guys is amazing.
Regards Simon
09/24/2019 at 1:16 PM #108350Just added the final version with the 2 different modes in the library, follow the link: Another ATR Trailing Stop
09/24/2019 at 1:44 PM #108352Hi Nicolas, just noticed the indicator is doing some strange things. On some charts its performing perfectly and others a bit weird. See attached chart for an example. Although the same chart on a 60 min timeframe seems fine. Note…. I used parameters 5 and 15.
Regards Simon
-
AuthorPosts
Find exclusive trading pro-tools on