Alert Error "array out of range" - page 3

 
ok, tanks
 
GumRai:


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

ffoorr:
will always be executed

How do you come to that conclusion?

 

You post what you claim to be the complete code with no buffers

Then you post code that includes buffers

You try to resize buffers .

Go and learn some simple code. Don't try to run when you haven't learned how to crawl.

 
  1. First you tell us you are using arrays, so we tell you to resize them.
    double MovingAvarage40Buffer[];
    double MovingAvarage50Buffer[];
    int start()
    {
    :   
       ArrayResize(MovingAvarage20,5);ArrayResize(ma20,5); 
       ArrayResize(MovingAvarage40,5);ArrayResize(ma40,5); 
       ArrayResize(MovingAvarage50,5);ArrayResize(ma50,5);  
    Now you post all the code and you re not using arrays, you are using buffers. Do NOT resize buffers.
    double MovingAvarage40Buffer[];
    double MovingAvarage50Buffer[];
    int init(){
       SetIndexBuffer(0,MovingAvarage20Buffer); ...
    }
    int start(){
    /* ArrayResize(MovingAvarage20,5);*/ArrayResize(ma20,5); 
    /* ArrayResize(MovingAvarage40,5);*/ArrayResize(ma40,5); 
    :
    Count starts at zero.

  2. No need for the decrement Contradictory information on IndicatorCounted() - MQL4 forum
     int counted_bars = IndicatorCounted();
     if( counted_bars > 0) counted_bars --;
      int limit=Bars-counted_bars;
      
        for(int j=0; j<limit; j++)
    To calculate a N moving average you must have N bars but you ask with j=Bars - 1. Handle your maximum look back.
    int counted_bars = IndicatorCounted();
    #define LOOKBACK PeriodMovingAvarage50 // Largest
    if(counted_bars < LOOKBACK) counted_bars = LOOKBACK;
    for(int j=Bars - 1 - counted_bars; j >= 0; j--)
    Get in the habit of counting down. Is 50 the Largest or is 40?

  3. Typo?
             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);
    
    Why are you putting a 50 MA into a buffer labeled 5?

  4. Same condition
    if (trendDown > trendUp)
          DrawAllert("Il trend è rialzista",clrRed);
    else if (trendDown > trendUp)
          DrawAllert("Il trend è ribassista",clrRed);
    If it's not up is MUST be down
    if (trendDown > trendUp)
          DrawAllert("Il trend è rialzista",clrRed);
    else //if (trendDown > trendUp)
          DrawAllert("Il trend è ribassista",clrRed);
    Or use the new Ternary Operator ?: - MQL4 Documentation
    DrawAllert(trendDown > trendUp ? "Il trend è rialzista" : "Il trend è ribassista",
               clrRed);
 

Gumrai, yes i'am wrong, ... "else if" will never be executed, but the "if" will be ?

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