Plot line shifting on ATR chandelier style stop loss (looping method)

Hi there,

I'm trying to code a custom ATR chandelier style stop loss using the looping method and plot it, but I'm finding that the plotline of the stop loss shifts when I scrub the time slider.
I find that the higher the multiplier is, the more the stop loss line shifts. In my code below I've entered a high multiplier in to demonstrate the problem more clearly.

Below is a screen capture video of the problem. Please note the bottom left corner of the video as I scrub the video.

https://youtu.be/eYSb66DE8sE

Also, on other charts, the plot line sometimes flickers off altogether when I am scrubbing.

Would anyone know what is causing the shifting? Am I coding something incorrectly?

Any help would be appreciated.

Thanks

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
_SECTION_END();

Buy = C>MA(C,150);
Sell = 0;
trailARRAY = Null;
trailstop = 0;

my_atr = ATR(14);
mult_ATR = 15*my_atr;


for( i = 1; i < BarCount; i++ )
{
	
   if( trailstop == 0 AND Buy[ i ] ) 
   { 
		stoplevel = mult_ATR[ i ] ;
		trailstop = Close[ i ] - stoplevel;
	
   }
   else Buy[ i ] = 0; // remove excess buy signals

   if( trailstop > 0 AND Low[ i ] < trailstop )
   {
      Sell[ i ] = 1;
      SellPrice[ i ] = trailstop;
      trailstop = 0;
   }

   if( trailstop > 0 )
   {
	  stoplevel = mult_ATR[ i ] ;
      trailstop = Max( High[ i ] - stoplevel, trailstop );
      trailARRAY[ i ] = trailstop;
   }

}

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

intrade = Flip(Buy,Sell);

Filter = intrade OR Sell;

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

You should put this link from original code into the posted code

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
_SECTION_END();

/// modified trailing stop code from 
/// @link https://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
Buy = C>MA(C,150);
Sell = 0;
trailARRAY = Null;
trailstop = 0;

my_atr = ATR(14);
mult_ATR = 15*my_atr;

for( i = 1; i < BarCount; i++ )
{
	
   if( trailstop == 0 AND Buy[ i ] ) 
   { 
		stoplevel = mult_ATR[ i ] ;
		trailstop = Close[ i ] - stoplevel;
	
   }
   else Buy[ i ] = 0; // remove excess buy signals

   if( trailstop > 0 AND Low[ i ] < trailstop )
   {
      Sell[ i ] = 1;
      SellPrice[ i ] = trailstop;
      trailstop = 0;
   }

   if( trailstop > 0 )
   {
	  stoplevel = mult_ATR[ i ] ;
      trailstop = Max( High[ i ] - stoplevel, trailstop );
      trailARRAY[ i ] = trailstop;
   }

}

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

intrade = Flip(Buy,Sell);

Filter = intrade OR Sell;

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

Recommended reading:

Hi fxshrat,

Thanks for that (and the reminder about the link). It was really helpful.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.