wicha
wicha

Reputation: 3

"array out of range" when I tried to build in an indicator (SMA of the RSI line) to my EA

I'd like to ask a bit help: I want to integrate a rule to my EA but I cannot make an array properly.. The rule would be "if the SMA of RSI on higher TF is above/under blabla..."

so here is my code:

      double MA;  
   double RSIBuf[];
   double MaBuf[];

ArrayResize(RSIBuf,0);
int counted_bars=IndicatorCounted();
int limit = Bars-counted_bars-1;
for(int i=limit; i>=0; i--)
{
RSIBuf[i] = (iRSI(NULL,higherTF,RSIPeriod,0,i)); 
MaBuf[i] = iMAOnArray(RSIBuf,higherTF,RSI_SMA,0,0,i);
}
MA = MaBuf[0];

... (irrelevant lines of coding)

direction Trend=NEUTRAL;

if(MA>RSI_Up )        Trend=UP;  

the MT4 says its an error on RSIBuf[] Line

Where I made wrong?

thank you very much for your effort

wicha

Upvotes: 0

Views: 764

Answers (2)

user8616658
user8616658

Reputation:

it is usually best to increment and count up with time series access, rather than decrement. Bar [0] is current bar, Bar[1] is previous bar and so on. This video explains it perfectly. https://www.youtube.com/watch?v=JQgfm4v6dhs

Upvotes: 0

J_P
J_P

Reputation: 781

The line ArrayResize(RSIBuf,0) assigns a size of 0 to the array RSIBuf[], this makes no sense as the array needs to have a size>0, and in your case at least=limit.

So in the loop, when you try to assign a value to RSIBuf[i] is an out of the range, because i is greater than 0 (i starts with i=limit)

According to the MQL4 documentation the second parameter should be the new_size, then 0 is a not valid value there:

int  ArrayResize(
   void&  array[],              // array passed by reference
   int    new_size,             // new array size
   int    reserve_size=0        // reserve size value (excess)
   );

Upvotes: 2

Related Questions