Indicator Array Question

 

If I set detrender[i] = 1.3100 or some other constant, then the indicator plots without any issues. However, when I make any references to the smooth array, it won't show any values. How do I get this value to calculate properly?

int start()
  {
   int Counted_bars=IndicatorCounted();

   for ( int i=Bars-Counted_bars-1; i >= 0; i--)
   {
      price[i] = (High[i] + Low[i]) * 0.5 ;
   
      period[i] = 0;
      if ( Counted_bars - i < 5 )
      {
         smooth[i] = (4* price[i] + 3 * price[i+1] + 2 * price[i+2] + price[i+3] ) / 10 ;
         
         // detrender doesn't work if it references smooth, but works if I put in a constant
         detrender[i] = 0.962*smooth[i] + 0.5769*smooth[i+2] - 0.5769*smooth[i+4] - 0.0962*smooth[i+6] *(0.075*period[i+1] + 0.54);
      }
      
   } // end of for loop
   return(0);
  }
 
texasnomad:

If I set detrender[i] = 1.3100 or some other constant, then the indicator plots without any issues. However, when I make any references to the smooth array, it won't show any values. How do I get this value to calculate properly?

Just for testing try declaring the smooth array as smooth[2000] instead of as you've probably done; smooth[]

 
DayTrader wrote >>

Just for testing try declaring the smooth array as smooth[2000] instead of as you've probably done; smooth[]

That worked... any idea why?

 

You can't use a variable to set array length:


NO: int someArray[c];

YES : int someArray[40];


to resize the array:

int someArray[];

ArrayResize(someArray, 44);

or

ArrayResize(someArray, someCounterVariable);

 
jmca wrote >>

You can't use a variable to set array length:

NO: int someArray[c];

YES : int someArray[40];

to resize the array:

int someArray[];

ArrayResize(someArray, 44);

or

ArrayResize(someArray, someCounterVariable);

the ArraySize was never declared. I simply declared:

double detrender[];

then set that value according to what you see in the code at the top.

 

Your original array had a length (size) of 0, therefore it had no elements to hold your values.

The MQL4 editor is a fairly simple and limited one. In more 'serious' enviroments you would

get an errormessage like " Out Of Bounds Exception" when trying to access non-existent array elements...

MQL4 just keeps you guessing, there are serveral issues like this in the editor/compiler.

If you know the size of the array upon declaration then set its size explicitly, like smooth[8].

Otherwise resize it when its size is known, like jmca says above..

Reason: