Skip to main content

ATR-based trailing stop for Amibroker (AFL)

amifan almost 16 years ago Amibroker (AFL)

  • Rating:
    3 / 5 (Votes 6)
  • Tags:
    amibroker, stop loss

trailing stop with ATR-based trailing stop.
It allows to use both fixed percentage AND ATR-based “Chandelier” stop.

Screenshots

Indicator / Formula

Copy & Paste Friendly
Version(5.20); // requires v5.20 
SetBarsRequired(sbrAll); 

// get start date 
Start = Cross( DateNum(), ParamDate("Start date", "2010-01-01" ) ); 
Started = Flip( Start, 0 ); 

StopMode = ParamToggle("Stop Mode", "Fixed|Chandelier" ); 
StopLevel = Param("Fixed perc %", 14, 0.1, 50, 0.1)/100; 
StopATRFactor = Param("Chandelier ATR multiple", 4, 0.5, 10, 0.1 ); 
StopATRPeriod = Param("Chandelier ATR period", 14, 3, 50 ); 

// calculate support and resistance levels 
if( StopMode == 0 ) // fixed percent trailing stop 
{ 
 sup = C * ( 1 - stoplevel ); 
 res = C * ( 1 + stoplevel ); 
} 
else // Chandelier ATR-based stop 
{ 
 sup = C - StopATRFactor * ATR( StopATRPeriod ); 
 res = C + StopATRFactor * ATR( StopATRPeriod ); 
} 

// calculate trailing stop line 
trailARRAY = Null; 
trailstop = 0; 
for( i = 1; i < BarCount; i++ ) 
{ 
if( Started[ i ] == 0 ) continue; 

if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop ) 
   trailstop = Max( trailstop, sup[ i ] ); 
else 
if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop ) 
   trailstop = Min( trailstop, res[ i ] ); 
else 
   trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );       

trailARRAY[ i ] = trailstop; 
} 

// generate buy/sell signals based on crossover with trail stop line 
Buy = Start OR Cross( C, trailArray ); 
Sell = Cross( trailArray, C ); 

PlotShapes(Buy*shapeUpArrow,colorGreen,0,trailarray); 
PlotShapes(Sell*shapeDownArrow,colorRed,0,trailarray); 

Plot( Close,"Price",colorBlack,styleBar); 
Plot( trailARRAY,"trailing stop level", colorRed );

7 comments

almost 16 years ago

The indicator is just displaying plain bar chart and nothing else. I am using amibroker 5.3.

2. amifan
almost 16 years ago

Hello godisbogus ,
This image is also of AB 5.30
check parameter window…you should have data from 01-01-2010 …if not then change date from parameter window
Thank you

3. doedau
over 15 years ago

Hi,

Just found this interesting formula and trying to experiment.

My coding not strong and when trying to change the start date parameter, it seemed to change ok but back test results did not change. Also changed startdate manually in code line for ‘start’ but no effect.

Maybe stopATRPeriod is involved??

Thanks for your help in advance.
Regards, Doedau.

over 15 years ago

Hi,

Works fine with lower versions also.
Try this,
Block the following lines in the code.

//Version(5.20); // requires v5.20
//SetBarsRequired(sbrAll);

5. ole
about 14 years ago

This is essentially the same code that was published in Stocks and Commodities and on the AmiBroker website. The only thing that this code does is eliminate two other stops that in the original: fixed percent and modified-Chanelier.

almost 5 years ago

Is there any update on this AFL?

Only Barchart are shown, ATR Trailing lines with triangle shapes are not shown.

over 3 years ago

Update please, I love this afl but can’t use in amibroker 6. Blank in the chart called null for atr.

Leave Comment

Please login here to leave a comment.