Navigation:Home > Content >

=x=Triple-EMA.mq4

Time: 2018-07-25 | Download file:=x=Triple-EMA.mq4

//+------------------------------------------------------------------+
//|                                      Triple EMA (Short Term).mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, maria77."

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Yellow
#property indicator_color2 Red
#property indicator_color3 Gray
#property indicator_color4 Red
#property indicator_color5 Lime

extern int MAPeriod1 = 4;
extern int MAPeriod2 = 9;
extern int MAPeriod3 = 18;
extern int MAType=2;

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];

int    MAMode;
string strMAType;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(5);
//---- indicators

//---- drawing settings
   SetIndexBuffer(1,ExtMapBuffer1);
   SetIndexBuffer(2,ExtMapBuffer2);
   SetIndexBuffer(3,ExtMapBuffer3);
   SetIndexBuffer(4,ExtMapBuffer4);
   SetIndexBuffer(5,ExtMapBuffer5);
   
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,2);
   
switch (MAType)
   {
      case 1: strMAType="EMA"; MAMode=MODE_EMA; break;
      case 2: strMAType="SMMA"; MAMode=MODE_SMMA; break;
      case 3: strMAType="LWMA"; MAMode=MODE_LWMA; break;
      case 4: strMAType="LSMA"; break;
      default: strMAType="SMA"; MAMode=MODE_SMA; break;
   }
   IndicatorShortName( strMAType+ " (" +MAPeriod3 + ") ");
//---- initialization done
   return(0);
  }

double LSMA(int Rperiod, int shift)
{
   int i;
   double sum;
   int length;
   double lengthvar;
   double tmp;
   double wt;

   length = Rperiod;
 
   sum = 0;
   for(i = length; i >= 1  ; i--)
   {
     lengthvar = length + 1;
     lengthvar /= 3;
     tmp = 0;
     tmp = ( i - lengthvar)*Close[length-i+shift];
     sum+=tmp;
    }
    wt = sum*6/(length*(length+1));
    
    return(wt);
}

int start()

  {
  
   double MA_Cur, MA_Prev;
   int limit;
   int counted_bars = IndicatorCounted();
   //---- check for possible errors
   if (counted_bars<0) return(-1);
   //---- last counted bar will be recounted
   if (counted_bars>0) counted_bars--;
   limit = Bars - counted_bars;

   for(int i=limit; i>=0; i--)
   {
      if (MAType == 4)
      {
        MA_Cur = LSMA(MAPeriod3,i);
        MA_Prev = LSMA(MAPeriod3,i+1);
      }
      else
      {
        MA_Cur = iMA(NULL,0,MAPeriod3,0,MAMode,PRICE_CLOSE,i);
        MA_Prev = iMA(NULL,0,MAPeriod3,0,MAMode,PRICE_CLOSE,i+1);
      }
 
         
//========== COLOR CODING ===========================================               
       
       ExtMapBuffer5[i] = MA_Cur; //red 
       ExtMapBuffer4[i] = MA_Cur; //red
       ExtMapBuffer3[i] = MA_Cur; //red  
       
        if (MA_Prev > MA_Cur)
        {
        ExtMapBuffer4[i] = EMPTY_VALUE;
        
        }
       else if (MA_Prev < MA_Cur) 
        {
        ExtMapBuffer3[i] = EMPTY_VALUE; //-1 red/greem tight
        
        }
         else 
         {
         
         ExtMapBuffer3[i]=EMPTY_VALUE;//EMPTY_VALUE;
         ExtMapBuffer4[i]=EMPTY_VALUE;//EMPTY_VALUE;
         }
        
      }
    
      return(0);
  }
//+------------------------------------------------------------------+

Recommend