Average True Range Trailing Stops Strategy by Sylvain Vervoort

 
Indicator is attached.

Copyrighted material from Stocks & Commodities Jun 2009

After the reveal of the ATR, which includes the calculation of the GAPS to pick the volatility, the happy Sylvain Vervoort discovered that taking an ATR volatility of 9 periods as a Trail, after there is a close out of the Trail, the chance is of a immediate reversal for or against the old trend. In other words, a reaction with everything or a confirmation of the closure, and if so, the two candlesticks of the new trend are coloured. This color calculus attempt shows the Big Picture.



Code: 

//+------------------------------------------------------------------+
//|                                                    ATR Trail.mq4 |
//+------------------------------------------------------------------+
#property copyright   "http://www.FinanceWave.com™"

#property indicator_chart_window
#property indicator_buffers 6

extern int per = 9;
extern double factor = 1;

double Bar1[],
                 Bar2[],
                 Candle1[],
                 Candle2[];

double ATR[],TR[],BULLS[],BEARS[];
double trail;
int i,n;
int begin=0;
int sit;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   SetIndexBuffer(0,Bar1); SetIndexStyle(0,DRAW_HISTOGRAM,0,1,Red);
   SetIndexBuffer(1,Bar2); SetIndexStyle(1,DRAW_HISTOGRAM,0,1,Lime);
   SetIndexBuffer(2,Candle1); SetIndexStyle(2,DRAW_HISTOGRAM,0,3,Red);
   SetIndexBuffer(3,Candle2); SetIndexStyle(3,DRAW_HISTOGRAM,0,3,Lime);
   SetIndexBuffer(4,BULLS); SetIndexStyle(4,DRAW_LINE,0,1,Green); 
   SetIndexBuffer(5,BEARS); SetIndexStyle(5,DRAW_LINE,0,1,DarkRed); 
   ArraySetAsSeries(ATR,TRUE); ArrayResize(ATR,Bars-1);
   ArraySetAsSeries(TR,TRUE); ArrayResize(TR,Bars-1);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| ATR True                                                         |
//+------------------------------------------------------------------+
int start()
{

begin=0; trueATR();
for(i = Bars-1-14; i>=0; i--)
{

if(begin==0)
{
trail=Close[i]-factor*ATR[i];
begin=1;
sit=1;
}

if(Close[i]>trail && Close[i+1]>trail)
{
if(sit==1)
{trail=MathMax(trail,Close[i]-factor*ATR[i]);}

if(sit==-1)
{trail=MathMax(Close[i]-factor*ATR[i],Close[i+1]-factor*ATR[i+1]); sit=1;}
}


if(Close[i]<trail && Close[i+1]<trail)
{
if(sit==-1)
{trail=MathMin(trail,Close[i]+factor*ATR[i]);}

if(sit==1)
{trail=MathMin(Close[i]+factor*ATR[i],Close[i+1]+factor*ATR[i+1]); sit=-1;}
}

if(sit==1)
{
SetCandleColor(2,i+1);
SetCandleColor(2,i);
BULLS[i]=trail;
BULLS[i+1]=trail;
}

if(sit==-1)
{
SetCandleColor(1,i+1); 
SetCandleColor(1,i);
BEARS[i]=trail;
BEARS[i+1]=trail;
}

}

return(0);
}
//+------------------------------------------------------------------+
void SetCandleColor(int col, int k)
{
        double high,low,bodyHigh,bodyLow;

        {
                bodyHigh = MathMax(Open[k],Close[k]);
                bodyLow  = MathMin(Open[k],Close[k]);
                high            = High[k];
                low             = Low[k];
        }

        Bar1[k] = low;  Candle1[k] = bodyLow;
        Bar2[k] = low;  Candle2[k] = bodyLow;
        
        switch(col)
        {
                case 1:         Bar1[k] = high; Candle1[k] = bodyHigh;  break;
                case 2:         Bar2[k] = high; Candle2[k] = bodyHigh;  break;
        }
}
//+------------------------------------------------------------------+
double trueATR()
{

for(i = Bars-1-1; i>=0; i--)
{

// TR 1a Calculus
TR[i]=MathMax(MathMax(High[i]-Low[i],MathAbs(High[i]-Close[i+1])),MathAbs(Low[i]-Close[i+1]));

// ATR 1a Calculus
if(i==Bars-1-per)
{

n=0;
while(n<per)
{
ATR[i]=ATR[i]+TR[i+n];
n++;
}
ATR[i]=ATR[i]/floor(per);

}

// ATR Calculus
else if(i<=Bars-1-per)
{
ATR[i]=(ATR[i+1]*floor(per-1)+TR[i])/floor(per);
}

}

return(0);
}
//+------------------------------------------------------------------+
Files:
ATR_Trail.mq4  7 kb