Why don't CopyHigh and CopyLow work properly the first time they run?

 
double low_array[];
double high_array[];

int entry = 0;

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
    if(entry == 0)
       {
        CopyMOne(high, time);
        entry = 1;
       }

   else if(entry == 1)
     {    
     entry = 2;
     CopyMOne(high, time);
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


void CopyMOne(const double &_high[], const datetime &_date_time[])
{  
   int t = ArraySize(_high)- 2;

   datetime  Start_date = _date_time[t];
   datetime  End_date = _date_time[t+1];

   int low = CopyLow(_Symbol, PERIOD_M1, Start_date, End_date, low_array);
   int high = CopyHigh(_Symbol, PERIOD_M1, Start_date, End_date, high_array);

   ArrayResize(low_array, low);
   ArrayResize(high_array, high);
}

As in the code above, when I need it, I copy the M1 High and Low prices in a certain date range (the dates can change) into the arrays.

1) When I open the terminal for the first time and throw the indicator to the current chart or any, the data is not copied.


2) When I close the terminal and open it again while there is an indicator on the graph, the data is not received again.


In short, if and only if I delete the indicator and add it back to the chart or compile it from the editor, it works properly.


As in the code above, I added if lines to OnCalculate so that it would run for nothing before, then I could use it again when I wanted to calculate in my main function, but it didn't work.

 
guga19: As in the code above, when I need it, I copy the M1 High and Low prices in a certain date range (the dates can change) into the arrays.

1) When I open the terminal for the first time and throw the indicator to the current chart or any, the data is not copied.
2) When I close the terminal and open it again while there is an indicator on the graph, the data is not received again.
In short, if and only if I delete the indicator and add it back to the chart or compile it from the editor, it works properly. As in the code above, I added if lines to OnCalculate so that it would run for nothing before, then I could use it again when I wanted to calculate in my main function, but it didn't work.

Because you are not taking into consideration the data access and synchronisation requirements. Please read the following documentation section completely:

 
Fernando Carreiro #:

Because you are not taking into consideration the data access and synchronisation requirements. Please read the following documentation section completely:

Thank you. I used the same script as in the document. First I run the script, then the indicator and it works. Now I have to find a way to run this script automatically before the indicator. Because that's not flexible. :)

Also, since I only want M1 data, I cannot get data above a certain limit on H4 or daily charts.
 
guga19 #: Thank you. I used the same script as in the document. First I run the script, then the indicator and it works. Now I have to find a way to run this script automatically before the indicator. Because that's not flexible. :)Also, since I only want M1 data, I cannot get data above a certain limit on H4 or daily charts.

You don't run the script before the indicator. That was not the point of the documentation. The point is for you to incorporate that technique into your indicator itself.

Don't just be a "script kiddy". Study the example and apply the knowledge to your own code.

 

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
          Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
          Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
          Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
          Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
          SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

 
William Roeder #:

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
          Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
          Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
          Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
          Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
          SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

Thank you. There are a few things I don't understand.

Firstly:

For example, I start 1000 candles backwards from the current candle on the H1 chart and I want to calculate the values of any 1-hour candle in "M1" separately when I need it.

My question is:

SeriesInfoInteger(Symbol(), PERIOD_M1, SERIES_TERMINAL_FIRSTDATE);  //This gives me the first date in the terminal. 2014.... bla bla.


SeriesInfoInteger(Symbol(), PERIOD_CURRENT, SERIES_FIRSTDATE);
//This gives the oldest date in a given period. Here, PERIOD_CURRENT = M1 can reach 2022 at the earliest.
//PERIOD_CURRENT = H1 is 2014.... but I need M1 not H1. I need to be able to get the oldest date in the terminal. Here I am a little confused.
 
The returned date & time for "SERIES_FIRSTDATE" is dependant on how many bars your chart can have. If you have set the "Max bars in chart" parameter in the Options→Chart to a smaller value, then that will be the result.
 
Fernando Carreiro #:
The returned date & time for "SERIES_FIRSTDATE" is dependant on how many bars your chart can have. If you have set the "Max bars in chart" parameter in the Options→Chart to a smaller value, then that will be the result.

Well, while I was thinking it came to my mind and I did it. (I used to use MT4 for something else in the past) I was going to post it here. You wrote exactly that.

But thanks again anyway.

 
guga19 #: Well, while I was thinking it came to my mind and I did it. (I used to use MT4 for something else in the past) I was going to post it here. You wrote exactly that. But thanks again anyway.

Please clarify — is this thread about MT5 or about MT4?

 
Fernando Carreiro #:

Please clarify — is this thread about MT5 or about MT4?

MT5

Reason: