Navigation:Home > Content >

Elder_auto_envelope.mq4

Time: 2012-05-15 | Download file:Elder_auto_envelope.mq4

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1  PaleVioletRed
#property indicator_color2  DeepSkyBlue
#property indicator_color3  DimGray
#property indicator_color4  DimGray
#property indicator_width1  2
#property indicator_width2  2
#property indicator_style3  STYLE_DOT
#property indicator_style4  STYLE_DOT

//
//
//
//
//

extern int    EmaPeriod        = 22;
extern int    FastEmaPeriod    = 13;
extern int    Price            = PRICE_CLOSE;
extern double DeviationsFactor = 2.7;
extern int    DeviationsPeriod = 100;

//
//
//
//
//

double slowEma[];
double fastEma[];
double envelopeUp[];
double envelopeDn[];

//+------------------------------------------------------------------+
//|                                                                  |
//|------------------------------------------------------------------|
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,slowEma);
   SetIndexBuffer(1,fastEma);
   SetIndexBuffer(2,envelopeUp);
   SetIndexBuffer(3,envelopeDn);

      IndicatorShortName("Elder auto ebvelope");
   return(0);
}
int deinit() { return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double work[][2];
#define _percp 0
#define _devs  1

//
//
//
//
//

int start()
{
   int i,k,r,limit,counted_bars=IndicatorCounted();
   int period = MathMax(PERIOD_W1,Period());
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //

   if (ArrayRange(work,0) != Bars) ArrayResize(work,Bars);
   for(i=limit, r=Bars-i-1; i>=0; i--,r++)
   {
      double ema   = iMA(NULL,0,EmaPeriod,0,MODE_EMA,Price,i); 
      double bullp = MathAbs(High[i]-ema);
      double bearp = MathAbs(Low[i] -ema);
      double maxp  = MathMax(bullp,bearp);
      work[r][_percp] = maxp/ema;
      work[r][_devs]  = work[r-1][_devs];
         
         //
         //
         //
         //
         //

            if (iBarShift(NULL,period,Time[i])!= iBarShift(NULL,period,Time[i+1]))
            {
               double avg = 0;
               double sum = 0;
               for (k=0; k=0; k++) avg += work[r-k-1][_percp];
                                                                avg /= DeviationsPeriod;
               for (k=0; k=0; k++) sum += (work[r-k-1][_percp]-avg)*(work[r-k-1][_percp]-avg);
            
               //
               //
               //
               //
               //
               
               work[r][_devs] = MathSqrt(sum/DeviationsPeriod);
            }
    
         //
         //
         //
         //
         //
      
         slowEma[i]    = ema;
         envelopeUp[i] = ema*(1+(DeviationsFactor*work[r][_devs]));
         envelopeDn[i] = ema*(1-(DeviationsFactor*work[r][_devs]));
            if (FastEmaPeriod>0) fastEma[i] = iMA(NULL,0,FastEmaPeriod,0,MODE_EMA,Price,i); 
   }
   return(0);         
}

Recommend