Trying to add a Moving Average to indicator but need help

 
Can someone see why this does not work?  I have added MA's before but this time the indicator will not even load and makes MT4 unresponsive.  Any help greatly appreciated.

//+------------------------------------------------------------------+
//|                                                Complex_pairs.mq4 |
//|                                              SemSemFX@rambler.ru |
//|              http://onix-trade.net/forum/index.php?showtopic=107 |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 4//3  added extra buffer for Moving Average
#property indicator_color1 White
#property indicator_color2 MediumSpringGreen
#property indicator_color3 Red
#property indicator_color4 Yellow

//---- input parameters - added parameters for Moving Average
extern int     MA_Periods     = 6;
extern string  _MA_info_      = "0 sma; 1 ema; 2 smma; 3 lwma";
extern int     MA_MODE        = 2;//0 sma; 1 ema; 2 smma; 3 lwma
extern int     mShift         = 0;


//---- buffers



double     ExtBuffer0[];
double     ExtBuffer1[];
double     ExtBuffer2[]; 
//
double     ExtBuffer3[]; // additional buffer for Moving Average 

//---- parameters
// for monthly
int mn_per=50;
int mn_fast=1;
// for weekly
int w_per=50;
int w_fast=1;
// for daily 
int d_per=50;
int d_fast=1;
// for H4      
int h4_per=50;    
int h4_fast=1;
// for H1
int h1_per=50;
int h1_fast=1;
// for M30
int m30_per=50;
int m30_fast=1;
// for M15
int m15_per=50;
int m15_fast=1;
// for M5
int m5_per=50;
int m5_fast=1;
// for M1
int m1_per=50;
int m1_fast=1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetLevelValue(1,0);
//
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexStyle(3,DRAW_LINE);   // additional buffer for Moving Average 
   
   IndicatorDigits(Digits+1);
   SetIndexDrawBegin(0,34);
   SetIndexDrawBegin(1,34);
   SetIndexDrawBegin(2,34);
   SetIndexDrawBegin(3,34);     // additional buffer for Moving Average    
   
   SetIndexBuffer(0,ExtBuffer0);
   SetIndexBuffer(1,ExtBuffer1);
   SetIndexBuffer(2,ExtBuffer2);
   SetIndexBuffer(3,ExtBuffer3); // additional buffer for Moving Average    
   

   
   
   IndicatorShortName(Symbol() + "(" + Period() + "): ");
   SetIndexLabel(0,"Data");
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
   SetIndexLabel(3,"MA");         // additional buffer for Moving Average 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
   double OPEN,HIGH,LOW,CLOSE;
//----
   if(counted_bars<0) return(-1);
//----
   if(counted_bars>0) counted_bars-=10;
//----   
   limit=Bars-counted_bars;
//----
   int Price=6;
   int Mode=3;
   int per1,per2;
//
   switch(Period())
     {
      case 1:     per1=m1_per; per2=m1_fast; break;
      case 5:     per1=m5_per; per2=m5_fast; break;
      case 15:    per1=m15_per;per2=m15_fast; break;
      case 30:    per1=m30_per;per2=m30_fast; break;
      case 60:    per1=h1_per; per2=h1_fast; break;
      case 240:   per1=h4_per; per2=h4_fast; break;
      case 1440:  per1=d_per;  per2=d_fast; break;
      case 10080: per1=w_per;  per2=w_fast; break;
      case 43200: per1=mn_per; per2=mn_fast; break;
     }
   for(int i=0; i<limit; i++)
     {
        if (Symbol()=="EURUSD")
        {
         OPEN=EUR(Mode,PRICE_OPEN,i,per1,per2)-USD(Mode,PRICE_OPEN,i,per1,per2);
         HIGH=EUR(Mode,PRICE_HIGH,i,per1,per2)-USD(Mode,PRICE_HIGH,i,per1,per2);
         LOW=EUR(Mode,PRICE_LOW,i,per1,per2)-USD(Mode,PRICE_LOW,i,per1,per2);
         CLOSE=EUR(Mode,PRICE_CLOSE,i,per1,per2)-USD(Mode,PRICE_CLOSE,i,per1,per2);
        }
        if (Symbol()=="EURGBP")
        {
         OPEN=EUR(Mode,PRICE_OPEN,i,per1,per2)-GBP(Mode,PRICE_OPEN,i,per1,per2);
         HIGH=EUR(Mode,PRICE_HIGH,i,per1,per2)-GBP(Mode,PRICE_HIGH,i,per1,per2);
         LOW=EUR(Mode,PRICE_LOW,i,per1,per2)-GBP(Mode,PRICE_LOW,i,per1,per2);
         CLOSE=EUR(Mode,PRICE_CLOSE,i,per1,per2)-GBP(Mode,PRICE_CLOSE,i,per1,per2);
        }
        if (Symbol()=="EURCHF")
        {
         OPEN=EUR(Mode,PRICE_OPEN,i,per1,per2)-CHF(Mode,PRICE_OPEN,i,per1,per2);
         HIGH=EUR(Mode,PRICE_HIGH,i,per1,per2)-CHF(Mode,PRICE_HIGH,i,per1,per2);
         LOW=EUR(Mode,PRICE_LOW,i,per1,per2)-CHF(Mode,PRICE_LOW,i,per1,per2);
         CLOSE=EUR(Mode,PRICE_CLOSE,i,per1,per2)-CHF(Mode,PRICE_CLOSE,i,per1,per2);
        }
        if (Symbol()=="EURJPY")
        {
         OPEN=EUR(Mode,PRICE_OPEN,i,per1,per2)-JPY(Mode,PRICE_OPEN,i,per1,per2);
         HIGH=EUR(Mode,PRICE_HIGH,i,per1,per2)-JPY(Mode,PRICE_HIGH,i,per1,per2);
         LOW=EUR(Mode,PRICE_LOW,i,per1,per2)-JPY(Mode,PRICE_LOW,i,per1,per2);
         CLOSE=EUR(Mode,PRICE_CLOSE,i,per1,per2)-JPY(Mode,PRICE_CLOSE,i,per1,per2);
        }
        if (Symbol()=="GBPUSD")
        {
         OPEN=GBP(Mode,PRICE_OPEN,i,per1,per2)-USD(Mode,PRICE_OPEN,i,per1,per2);
         HIGH=GBP(Mode,PRICE_HIGH,i,per1,per2)-USD(Mode,PRICE_HIGH,i,per1,per2);
         LOW=GBP(Mode,PRICE_LOW,i,per1,per2)-USD(Mode,PRICE_LOW,i,per1,per2);
         CLOSE=GBP(Mode,PRICE_CLOSE,i,per1,per2)-USD(Mode,PRICE_CLOSE,i,per1,per2);
        }
        if (Symbol()=="GBPCHF")
        {
         OPEN=GBP(Mode,PRICE_OPEN,i,per1,per2)-CHF(Mode,PRICE_OPEN,i,per1,per2);
         HIGH=GBP(Mode,PRICE_HIGH,i,per1,per2)-CHF(Mode,PRICE_HIGH,i,per1,per2);
         LOW=GBP(Mode,PRICE_LOW,i,per1,per2)-CHF(Mode,PRICE_LOW,i,per1,per2);
         CLOSE=GBP(Mode,PRICE_CLOSE,i,per1,per2)-CHF(Mode,PRICE_CLOSE,i,per1,per2);
        }
        if (Symbol()=="GBPJPY")
        {
         OPEN=GBP(Mode,PRICE_OPEN,i,per1,per2)-JPY(Mode,PRICE_OPEN,i,per1,per2);
         HIGH=GBP(Mode,PRICE_HIGH,i,per1,per2)-JPY(Mode,PRICE_HIGH,i,per1,per2);
         LOW=GBP(Mode,PRICE_LOW,i,per1,per2)-JPY(Mode,PRICE_LOW,i,per1,per2);
         CLOSE=GBP(Mode,PRICE_CLOSE,i,per1,per2)-JPY(Mode,PRICE_CLOSE,i,per1,per2);
        }
        if (Symbol()=="USDCHF")
        {
         OPEN=USD(Mode,PRICE_OPEN,i,per1,per2)-CHF(Mode,PRICE_OPEN,i,per1,per2);
         HIGH=USD(Mode,PRICE_HIGH,i,per1,per2)-CHF(Mode,PRICE_HIGH,i,per1,per2);
         LOW=USD(Mode,PRICE_LOW,i,per1,per2)-CHF(Mode,PRICE_LOW,i,per1,per2);
         CLOSE=USD(Mode,PRICE_CLOSE,i,per1,per2)-CHF(Mode,PRICE_CLOSE,i,per1,per2);
        }
        if (Symbol()=="USDJPY")
        {
         OPEN=USD(Mode,PRICE_OPEN,i,per1,per2)-JPY(Mode,PRICE_OPEN,i,per1,per2);
         HIGH=USD(Mode,PRICE_HIGH,i,per1,per2)-JPY(Mode,PRICE_HIGH,i,per1,per2);
         LOW=USD(Mode,PRICE_LOW,i,per1,per2)-JPY(Mode,PRICE_LOW,i,per1,per2);
         CLOSE=USD(Mode,PRICE_CLOSE,i,per1,per2)-JPY(Mode,PRICE_CLOSE,i,per1,per2);
        }
        if (Symbol()=="CHFJPY")
        {
         OPEN=CHF(Mode,PRICE_OPEN,i,per1,per2)-JPY(Mode,PRICE_OPEN,i,per1,per2);
         HIGH=CHF(Mode,PRICE_HIGH,i,per1,per2)-JPY(Mode,PRICE_HIGH,i,per1,per2);
         LOW=CHF(Mode,PRICE_LOW,i,per1,per2)-JPY(Mode,PRICE_LOW,i,per1,per2);
         CLOSE=CHF(Mode,PRICE_CLOSE,i,per1,per2)-JPY(Mode,PRICE_CLOSE,i,per1,per2);
        }
       ExtBuffer0[i]=(OPEN+HIGH+LOW+CLOSE)/4;

   //---- Addition Moving Average - latest attempt (does not work - MT4 becomes unresponsive)
   while(i>=0)
   {
     //----
      ExtBuffer3[i]= iMAOnArray(ExtBuffer0,0,MA_Periods,mShift,MA_MODE, i);
      //----
      i--;
   }
   //----

        if (ExtBuffer0[i]>0)
             {
             ExtBuffer1[i]=ExtBuffer0[i];
             ExtBuffer2[i]=0.0;
             } 
             else if (ExtBuffer0[i]==0)
                    {
                     ExtBuffer1[i]=0.0;
                     ExtBuffer2[i]=0.0;
                    } 
                    else 
                     {
                      ExtBuffer1[i]=0.0;
                      ExtBuffer2[i]=ExtBuffer0[i];
                     } 
//       
//       
        }
       
//----
   return(0);
  }
//+------------------------------------------------------------------+
  double USD(int Mode, int Price, int i, int per1, int per2)
  {
   return(
            (iMA("EURUSD",0,per1,0,Mode,Price,i)-
            iMA("EURUSD",0,per2,0,Mode,Price,i))*10000
            +
            (iMA("GBPUSD",0,per1,0,Mode,Price,i)-
            iMA("GBPUSD",0,per2,0,Mode,Price,i))*10000
            +
            (iMA("USDCHF",0,per2,0,Mode,Price,i)-
            iMA("USDCHF",0,per1,0,Mode,Price,i))*10000
            +
            (iMA("USDJPY",0,per2,0,Mode,Price,i)-
            iMA("USDJPY",0,per1,0,Mode,Price,i))*100
          );
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
  double EUR(int Mode, int Price, int i, int per1, int per2)
  {
   return(
            (iMA("EURUSD",0,per2,0,Mode,Price,i)-
            iMA("EURUSD",0,per1,0,Mode,Price,i))*10000
            +
            (iMA("EURGBP",0,per2,0,Mode,Price,i)-
            iMA("EURGBP",0,per1,0,Mode,Price,i))*10000
            +
            (iMA("EURCHF",0,per2,0,Mode,Price,i)-
            iMA("EURCHF",0,per1,0,Mode,Price,i))*10000
            +
            (iMA("EURJPY",0,per2,0,Mode,Price,i)-
            iMA("EURJPY",0,per1,0,Mode,Price,i))*100
          );
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
  double GBP(int Mode, int Price, int i, int per1, int per2)
  {
   return(
            (iMA("GBPUSD",0,per2,0,Mode,Price,i)-
            iMA("GBPUSD",0,per1,0,Mode,Price,i))*10000
            +
            (iMA("EURGBP",0,per1,0,Mode,Price,i)-
            iMA("EURGBP",0,per2,0,Mode,Price,i))*10000
            +
            (iMA("GBPCHF",0,per2,0,Mode,Price,i)-
            iMA("GBPCHF",0,per1,0,Mode,Price,i))*10000
            +
            (iMA("GBPJPY",0,per2,0,Mode,Price,i)-
            iMA("GBPJPY",0,per1,0,Mode,Price,i))*100
          );
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
  double CHF(int Mode, int Price, int i, int per1, int per2)
  {
   return(
            (iMA("USDCHF",0,per1,0,Mode,Price,i)-
            iMA("USDCHF",0,per2,0,Mode,Price,i))*10000
            +
            (iMA("EURCHF",0,per1,0,Mode,Price,i)-
            iMA("EURCHF",0,per2,0,Mode,Price,i))*10000
            +
            (iMA("GBPCHF",0,per1,0,Mode,Price,i)-
            iMA("GBPCHF",0,per2,0,Mode,Price,i))*10000
            +
            (iMA("CHFJPY",0,per2,0,Mode,Price,i)-
            iMA("CHFJPY",0,per1,0,Mode,Price,i))*100
          );
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
  double JPY(int Mode, int Price, int i, int per1, int per2)
  {
   return(
            (iMA("USDJPY",0,per1,0,Mode,Price,i)-
            iMA("USDJPY",0,per2,0,Mode,Price,i))*100
            +
            (iMA("EURJPY",0,per1,0,Mode,Price,i)-
            iMA("EURJPY",0,per2,0,Mode,Price,i))*100
            +
            (iMA("GBPJPY",0,per1,0,Mode,Price,i)-
            iMA("GBPJPY",0,per2,0,Mode,Price,i))*100
            +
            (iMA("CHFJPY",0,per1,0,Mode,Price,i)-
            iMA("CHFJPY",0,per2,0,Mode,Price,i))*100
          );
  }
//+------------------------------------------------------------------+
 
for(int i=0; i<limit; i++)
   {
   while(i>=0)
     {
     //----
      ExtBuffer3[i]= iMAOnArray(ExtBuffer0,0,MA_Periods,mShift,MA_MODE, i);
      //----
      i--;
     }
   }

 

 Your for loop is increasing i, then the while is decreasing it, a never-ending loop

The while needs to be after the for loop 

 
Thanks for looking at this Gumrai.  I pasted the above, replacing the previous code and on compiling got error message 'i' - variable already defined

I replaced
for(int i=0; i<limit; i++)  with for(i=0; i<limit; i++)

and it compiled without errors but still will not attach to chart and then MT4 goes unresponsive again.

Did it work for you? Or did you spot the error in coding?

 
NewtonLeo:
Thanks for looking at this Gumrai.  I pasted the above, replacing the previous code and on compiling got error message 'i' - variable already defined

I replaced

and it compiled without errors but still will not attach to chart and then MT4 goes unresponsive again.

Did it work for you? Or did you spot the error in coding?


It's still an infinite loop. As GumRai suggested, place the while after the for loop.

     ExtBuffer0[i]=(OPEN+HIGH+LOW+CLOSE)/4;

   //----

        if (ExtBuffer0[i]>0)
             {
             ExtBuffer1[i]=ExtBuffer0[i];
             ExtBuffer2[i]=0.0;
             } 
             else if (ExtBuffer0[i]==0)
                    {
                     ExtBuffer1[i]=0.0;
                     ExtBuffer2[i]=0.0;
                    } 
                    else 
                     {
                      ExtBuffer1[i]=0.0;
                      ExtBuffer2[i]=ExtBuffer0[i];
                     } 
//       
//       
        }
   
   i=limit;
   //---- Addition Moving Average - latest attempt (does not work - MT4 becomes unresponsive)
   while(i>=0)
   {
     //----
      ExtBuffer3[i]= iMAOnArray(ExtBuffer0,0,MA_Periods,mShift,MA_MODE, i);
      //----
      i--;
   }
       
//----
   return(0);
  }
 
Thankyou pipPod, that fixed the issue.  I could have sworn I'd located it there in one of my earlier attempts but it works now (Whew!).  Thankyou again too GumRai for ID'ing the infinite loop as the cause. I appreciate the time you both took in helping me through this.
 
NewtonLeo:
Thanks for looking at this Gumrai.  I pasted the above, replacing the previous code and on compiling got error message 'i' - variable already defined

I replaced

and it compiled without errors but still will not attach to chart and then MT4 goes unresponsive again.

Did it work for you? Or did you spot the error in coding?


Glad you got it working now.

I spotted the error in the coding, didn't try the code  

Reason: