new to MQL5, need help please

To add comments, please log in or register
gANDm
12
gANDm  

Hi,
I'm trying to fill a struct that has a double array.
I'm trying to copy this array from a function returning it.
It is just a start, so code is very clean (no error handling, etc. yet).
I keep getting an error of 'copyLowArray Variable Expected':

Here is the whole code. wrote the errors I got inline:


input ENUM_TIMEFRAMES InpLoadedPeriod=PERIOD_H1; // Period to be loaded
datetime startDate, endDate;
string symbols[] = {"GBPUSD", "EURUSD", "USDJPY", "USDCHF", "NZDUSD", "USDCAD", "AUDUSD"};
struct symbol_data
{
   string symbol;
   int barsNo;
   long VolumeValues[];
   double LowValues[];
   double CloseValues[];
   double HighValues[];
   double  OpenValues[];
   datetime DateValues[];
};

symbol_data symbolsArr[]; //array of symbols with data for bars

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   endDate = TimeCurrent();
   startDate=endDate-(1000*InpLoadedPeriod*60); // Start date- 1000 bars before current date
//---Init symbolsArr
   for(int i=0; i<ArraySize(symbols); i++)
   {
      //ArraySetAsSeries(symbolsArr[i].LowValues,true);
      int bars=Bars(symbols[i],InpLoadedPeriod, startDate, endDate);
      symbolsArr[i].symbol = symbols[i];
      symbolsArr[i].barsNo = bars;
      
      ArrayCopy(symbolsArr[i].LowValues, copyLowArray(symbols[i],InpLoadedPeriod,bars) ,0,0,WHOLE_ARRAY);  //try 2 - got error: copyLowArray variable expected 
      symbolsArr[i].LowValues= copyLowArray(symbols[i],InpLoadedPeriod,bars); //this was try 1 - got error:  LowValues- invalid array access
      
      
   }
   
//---
   return(INIT_SUCCEEDED);
}
  
double copyLowArray(string symbol,ENUM_TIMEFRAMES timeframe,int index)
{
   double LowValues[];
   ArraySetAsSeries(LowValues,true);
   printf("---Run CopyLow for " + symbol);
   int copied=CopyLow(symbol,timeframe,startDate,endDate,lowV);
   if(copied>0 && index<=copied) printf("---Copied Low=%d", copied);
   //else return 0;
   return(LowValues);  
}

The way I overcame it was to define a global array, fill it in the function and then copy it to relevant place... But it doesn't sound logic to do so for every param in the structure (there will be more)... does it?


Thanks!
Sergey Golubev
Moderator
106109
Sergey Golubev  
whroeder1
17943
whroeder1  
  1. When you post code please use the SRC button! Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. double copyLowArray(string symbol,ENUM_TIMEFRAMES timeframe,int index){
       double LowValues[];
       :
       int copied=CopyLow(symbol,timeframe,startDate,endDate,lowV);
       :
       return(LowValues);  

    Only post code that compiles. How can you return an array? What is startDate, endDate, lowV? Etc.

  3. If you're going to write OOP, then write it, don't mix your struct and procedural code.
    struct symbol_data
    {
       string symbol;
       int barsNo;
       double LowValues[];
    
       void fill(ENUM_TIMEFRAMES timeframe){
          barsNo = CopyLow(symbol,timeframe,0, Bars(symbol,timeframe), LowValues);
       }
    }
    
    symbol_data symbolsArr[];
    
    ...
    
    for(int i=0; i<ArraySize(symbolsArr); i++) symbolsArr[i].fill(InpLoadedPeriod);
    
  4. On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Timeseries and Indicators Access /  Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
gANDm
12
gANDm  
whroeder1:
  1. When you post code please use the SRC button! Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. Only post code that compiles. How can you return an array? What is startDate, endDate, lowV? Etc.

  3. If you're going to write OOP, then write it, don't mix your struct and procedural code.
  4. On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Timeseries and Indicators Access /  Data Access - Reference on algorithmic/automated trading language for MetaTrader 5

Sorry... new to the forum as well.

I did not mix all, just didn't write all the procedures.  Here is the whole code. wrote the errors I got inline:


input ENUM_TIMEFRAMES InpLoadedPeriod=PERIOD_H1; // Period to be loaded
datetime startDate, endDate;
string symbols[] = {"GBPUSD", "EURUSD", "USDJPY", "USDCHF", "NZDUSD", "USDCAD", "AUDUSD"};
struct symbol_data
{
   string symbol;
   int barsNo;
   long VolumeValues[];
   double LowValues[];
   double CloseValues[];
   double HighValues[];
   double  OpenValues[];
   datetime DateValues[];
};

symbol_data symbolsArr[]; //array of symbols with data for bars

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   endDate = TimeCurrent();
   startDate=endDate-(1000*InpLoadedPeriod*60); // Start date- 1000 bars before current date
//---Init symbolsArr
   for(int i=0; i<ArraySize(symbols); i++)
   {
      //ArraySetAsSeries(symbolsArr[i].LowValues,true);
      int bars=Bars(symbols[i],InpLoadedPeriod, startDate, endDate);
      symbolsArr[i].symbol = symbols[i];
      symbolsArr[i].barsNo = bars;
      
      ArrayCopy(symbolsArr[i].LowValues, copyLowArray(symbols[i],InpLoadedPeriod,bars) ,0,0,WHOLE_ARRAY);  //try 2 - got error: copyLowArray variable expected 
      symbolsArr[i].LowValues= copyLowArray(symbols[i],InpLoadedPeriod,bars); //this was try 1 - got error:  LowValues- invalid array access
      
      
   }
   
//---
   return(INIT_SUCCEEDED);
}
  
double copyLowArray(string symbol,ENUM_TIMEFRAMES timeframe,int index)
{
   double LowValues[];
   ArraySetAsSeries(LowValues,true);
   printf("---Run CopyLow for " + symbol);
   int copied=CopyLow(symbol,timeframe,startDate,endDate,lowV);
   if(copied>0 && index<=copied) printf("---Copied Low=%d", copied);
   //else return 0;
   return(LowValues);  
}

The way I overcame it was to define a global array, fill it in the function and then copy it to relevant place... But it doesn't sound logic to do so for every param in the structure (there will be more)... does it?


Thanks!

gANDm
12
gANDm  
Sergey Golubev:

Sorry... new to the forum as well.

done

To add comments, please log in or register