An unexpected error using High[] and Low[]

 

Hi all,

I've found out that one of my EAs cannot put High[] and Low[] values into an assigned variable which is also a double typed array.

I've taken out that coding and rewrote a very simple program to test it again with the source code as follows:

//--- Input parameters

//--- Global variables
double           HVal[],LVal[];                    // Storage for Day High/Low

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   HVal[0] = High[0];                                                                                
   LVal[0] = Low[0];      
   Alert("High[0]=",High[0],",HVal[0]=",HVal[0],",Low[0]=",Low[0],",LVal[0]=",LVal[0]);

   return(0);
  }

Any ideas from your experiences? I've already reinstalled MT4 software. From the alert message, I could only see the following:

High[0]=88.64,HVal[0]=0,Low[0]=88.59,LVal[0]=0

Regards,

 

I think you have declared 2 arrays with zero elements . .

Try this . .

double           HVal[10],LVal[10];                    // Storage for Day High/Low
 
RaptorUK:

I think you have declared 2 arrays with zero elements . .

Try this . .


Many thanks. Probably my mistake that I still haven't been known how many elements are required for HVal[] and LVal[]. I'm going to try it soon.

BTW, may I ask if I have to code the arrays being dynamic (unfixed no of elements), how could I use a variable to define these two arrays HVal[] and LVal[]? I think I have tried to define a variable like k and then put k into the two arrays to become HVal[k], LVal[k] but I remembered that I've got error (forgot what error though). Any ideas to accomplish this without errors?

 

There are a few Functions available to handle arrays: https://docs.mql4.com/array

Define with a fixed size, then if you need to make the Arry bigger you can use ArrayResize . . . be a little careful, you might need to SetAsSeries first . . there is info in the Book about Arrays that will probably help : https://book.mql4.com//

 
On a resize, if you want to shift elements so the new one is a[0], then you need my code
bool    ResizeBuffer(double& buffer[], int size){
    if (ArraySize(buffer) != size){
        ArraySetAsSeries(buffer, false);    // Shift values B[2]=B[1]; B[1]=B[0]
        if (ArrayResize(buffer, size) <= 0){
            trading.disabled    = "ArrayResize [1] failed: "+GetLastError()
                                + " Trading disabled.";
            Alert(trading.disabled);    Print(trading.disabled);
            return(false);  }
        ArraySetAsSeries(buffer, true);
    }
    return(true);
}
 
WHRoeder:
On a resize, if you want to shift elements so the new one is a[0], then you need my code

Thank you all for the responses. You did remind me of the resize and the series concern. The coding is fine now. Continue my testing and all for now. :)