Alert Error "array out of range" - page 2

 
texcs:

To make it easier to edit

Then I suggest you to name them something like :

input int PeriodMovingAverageFast=20;
input int PeriodMovingAverageMid=40;
input int PeriodMovingAverageSlow=50;
otherwise you can have PeriodMovingAverage20=40 which is quite confusing.
 
But why do you by error
 

I don't see anything in the code that will make your platform crash.

I think that your problem is elsewhere in the code.

 
the code is all that we have a look?
int start()
{
   int trendUp=0,trendDown=0;
   double MovingAvarage20[],MovingAvarage40[],MovingAvarage50[];
   double ma20[], ma40[], ma50[];
   int counted_bars = IndicatorCounted();
   int limit=Bars-counted_bars;
     //---- check for possible errors
   if(counted_bars < 0) return(-1);
   //---- last counted bar will be recounted
   if(counted_bars > 0) counted_bars--;
   
   ArrayResize(MovingAvarage20,5);ArrayResize(ma20,5); 
   ArrayResize(MovingAvarage40,5);ArrayResize(ma40,5); 
   ArrayResize(MovingAvarage50,5);ArrayResize(ma50,5);     
   //creo l'array dei valori delle ultime 5 candele delle medie
   for ( int i=0; i< 5; i++)
         {  
         //creo l'array dei valori delle ultime 5 candele della media  a 20 periodi
          MovingAvarage20[i]= iMA(NULL, 0, 20,0, MODE_EMA, PRICE_CLOSE,i);
          ma20[i]= MovingAvarage20[i];
          
          //creo l'array dei valori delle ultime 5 candele della media  a 40 periodi
          MovingAvarage40[i]= iMA(NULL, 0, 40,0, MODE_EMA, PRICE_CLOSE,i);
          ma40[i]= MovingAvarage40[i];
          
          //creo l'array dei valori delle ultime 5 candele della media  a 50 periodo
          MovingAvarage50[i]= iMA(NULL, 0, 50,0, MODE_EMA, PRICE_CLOSE,i);
          ma50[i]= MovingAvarage50[i];
         
         if ((ma20[i] > ma40[i] ) && (ma40[i] > ma50[i]))
            trendUp++;                   
         
         if ((ma20[i] < ma40[i]) && (ma40[i]< ma50[i]))
            trendDown++;   
   }         
       
 /*  for(int j=0; j<limit; i++)
      {
//         MovingAvarage20Buffer[j]= iMA(NULL, 0, PeriodMovingAvarage20,0, MODE_EMA, PRICE_CLOSE,j);
  //       MovingAvarage40Buffer[j]= iMA(NULL, 0, PeriodMovingAvarage40,0, MODE_EMA, PRICE_CLOSE,j);
         //MovingAvarage50Buffer[j]= iMA(NULL, 0, PeriodMovingAvarage50,0, MODE_EMA, PRICE_CLOSE,j);
         //ma20 = MovingAvarage20Buffer[j];
         //ma40a = MovingAvarage40Buffer[j];
         //ma40 = MovingAvarage40Buffer[j];   
      }*/
      
      
   if (trendDown > trendUp)
      DrawAllert("Il trend è rialzista",clrRed);
   else if (trendDown > trendUp)
         DrawAllert("Il trend è ribassista",clrRed);     
      
      
 return 0;
}
 
texcs:
the code is all that we have a look?


That is NOT all the code

You call a function, DrawAllert and what is this?

   
   if (trendDown > trendUp)
      DrawAllert("Il trend è rialzista",clrRed);
   else if (trendDown > trendUp)
         DrawAllert("Il trend è ribassista",clrRed);     

The if and the else are the same condition!!

DrawAllert("Il trend è ribassista",clrRed);

will never be executed

 
All the code is this:
#property copyright "Copyright 2014, MetaQuotes Software Corp."

#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
//--- plot Iine1
#property indicator_label1 "MovingAvarage1"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrMediumBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot line2
#property indicator_label2 "MovingAvarage2"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrFuchsia
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- plot line3
#property indicator_label3 "MovingAvarage3"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrYellow
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//--- input parameters 
input int PeriodMovingAvarage20=20;
input int PeriodMovingAvarage40=40;
input int PeriodMovingAvarage50=50;
//--- indicator buffers
double MovingAvarage20Buffer[];
double MovingAvarage40Buffer[];
double MovingAvarage50Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
   //---- line shifts when drawing
   SetIndexShift(1,PeriodMovingAvarage20);
   SetIndexShift(2,PeriodMovingAvarage40);
   SetIndexShift(3,PeriodMovingAvarage50);
   SetIndexDrawBegin(1,PeriodMovingAvarage20);//da riguardare
   SetIndexDrawBegin(2,PeriodMovingAvarage40);//da riguardare
   SetIndexDrawBegin(3,PeriodMovingAvarage50);//da riguardare
   //---- index labels
   SetIndexLabel(1,"Media Mobile a 20");
   SetIndexLabel(2,"Media Mobile a 40");
   SetIndexLabel(3,"Media Mobile a 50");
   //--- indicator buffers mapping
   SetIndexBuffer(1,MovingAvarage20Buffer);
   SetIndexBuffer(2,MovingAvarage40Buffer);
   SetIndexBuffer(3,MovingAvarage50Buffer);
   //---- line shifts when drawing
   SetIndexShift(1,PeriodMovingAvarage20);
   SetIndexShift(2,PeriodMovingAvarage40);
   SetIndexShift(3,PeriodMovingAvarage50);   
   return (0); 
}
void DrawAllert(string text, color C=LightGray)
{

         ObjectCreate         ("rect",OBJ_LABEL,0,0,0,0,0);
         ObjectSet            ("rect",OBJPROP_XDISTANCE,10);
         ObjectSet            ("rect",OBJPROP_YDISTANCE,20);
         ObjectSetText ("rect",text,10,"Times New Roman",clrWhiteSmoke);
} 

void trend( double ma20, double ma40a, double ma40, double ma50)
   {
       
         if ((ma20 > ma40a ) && (ma40 > ma50))
            DrawAllert("Il trend è rialzista",clrRed);
                   
         if ((ma20 < ma40a) && (ma40< ma50))
            DrawAllert("Il trend è ribassista",clrRed);
            
   }             

    
   
int start()
{
   int trendUp=0,trendDown=0;
   double MovingAvarage20[],MovingAvarage40[],MovingAvarage50[];
   double ma20[], ma40[], ma50[];
   int counted_bars = IndicatorCounted();
   int limit=Bars-counted_bars;
     //---- check for possible errors
   if(counted_bars < 0) return(-1);
   //---- last counted bar will be recounted
   if(counted_bars > 0) counted_bars--;
   
   ArrayResize(MovingAvarage20,5);ArrayResize(ma20,5); 
   ArrayResize(MovingAvarage40,5);ArrayResize(ma40,5); 
   ArrayResize(MovingAvarage50,5);ArrayResize(ma50,5);     
   //creo l'array dei valori delle ultime 5 candele delle medie
   for ( int i=0; i< 5; i++)
         {  
         //creo l'array dei valori delle ultime 5 candele della media  a 20 periodi
          MovingAvarage20[i]= iMA(NULL, 0, 20,0, MODE_EMA, PRICE_CLOSE,i);
          ma20[i]= MovingAvarage20[i];
          
          //creo l'array dei valori delle ultime 5 candele della media  a 40 periodi
          MovingAvarage40[i]= iMA(NULL, 0, 40,0, MODE_EMA, PRICE_CLOSE,i);
          ma40[i]= MovingAvarage40[i];
          
          //creo l'array dei valori delle ultime 5 candele della media  a 50 periodo
          MovingAvarage50[i]= iMA(NULL, 0, 50,0, MODE_EMA, PRICE_CLOSE,i);
          ma50[i]= MovingAvarage50[i];
         
         if ((ma20[i] > ma40[i] ) && (ma40[i] > ma50[i]))
            trendUp++;                   
         
         if ((ma20[i] < ma40[i]) && (ma40[i]< ma50[i]))
            trendDown++;   
   }         
/*/  ArrayResize(MovingAvarage20Buffer,limit); 
   ArrayResize(MovingAvarage40Buffer,limit);
   ArrayResize(MovingAvarage50Buffer,counted_bars);
      
    for(int j=0; j<limit; i++)
      {
         MovingAvarage20Buffer[j]= iMA(NULL, 0, PeriodMovingAvarage20,0, MODE_EMA, PRICE_CLOSE,j);
         MovingAvarage40Buffer[j]= iMA(NULL, 0, PeriodMovingAvarage40,0, MODE_EMA, PRICE_CLOSE,j);
//         MovingAvarage50Buffer[j]= iMA(NULL, 0, PeriodMovingAvarage50,0, MODE_EMA, PRICE_CLOSE,j);
      }
*/      
      
   if (trendDown > trendUp)
      DrawAllert("Il trend è rialzista",clrRed);
   else if (trendDown > trendUp)
         DrawAllert("Il trend è ribassista",clrRed);     
      
      
 return 0;
}
 
GumRai:

will never be executed
will always be executed
 
if (trendDown > trendUp)
      DrawAllert("Il trend è rialzista",clrRed);
   else if (trendDown > trendUp)
         DrawAllert("Il trend è ribassista",clrRed);     
Well, this part have the same if condition.
 

Thanks it works, now on this piece of code on "MovingAvarage5Buffer" gives me this error but if i fill the mistake do not appear but if i "Attach"to the graph the errors is "array out of rage"

 int counted_bars = IndicatorCounted();
   int limit=Bars-counted_bars;
   ArrayResize(MovingAvarage20Buffer,limit); 
   ArrayResize(MovingAvarage40Buffer,limit);
   ArrayResize(MovingAvarage5Buffer,30000);
      
    for(int j=0; j<limit; j++)
      {
         MovingAvarage20Buffer[j]= iMA(NULL, 0, PeriodMovingAvarage20,0, MODE_EMA, PRICE_CLOSE,j);
         MovingAvarage40Buffer[j]= iMA(NULL, 0, PeriodMovingAvarage40,0, MODE_EMA, PRICE_CLOSE,j);
         MovingAvarage5Buffer[j]=  iMA(NULL, 0, PeriodMovingAvarage40,0, MODE_EMA, PRICE_CLOSE,j);
      }
         
 

buffers start at 0

   SetIndexShift(0,PeriodMovingAvarage20);
   SetIndexShift(1,PeriodMovingAvarage40);
   SetIndexShift(2,PeriodMovingAvarage50);

then :

 int counted_bars = IndicatorCounted();
 if( counted_bars > 0) counted_bars --;
  int limit=Bars-counted_bars;
  
    for(int j=0; j<limit; j++)
      {
         MovingAvarage20Buffer[j]= iMA(NULL, 0, PeriodMovingAvarage20,0, MODE_EMA, PRICE_CLOSE,j);
         MovingAvarage40Buffer[j]= iMA(NULL, 0, PeriodMovingAvarage40,0, MODE_EMA, PRICE_CLOSE,j);
         MovingAvarage5Buffer[j]=  iMA(NULL, 0, PeriodMovingAvarage40,0, MODE_EMA, PRICE_CLOSE,j);
      }
Reason: