CopyRates() function for a specified time period

 

Dear Members,

I would really appreciate if anyone can guide me with this.

I want to retrieve the rates using the copyRates() function between 2 specified timings:


Start Time: currentTime() 

End Time: currentTime()-30 minutes.


I found the sample code in the below link, but not sure how to pass the 2 date/time parameters.

https://www.mql5.com/en/docs/series/copyrates

On the page, it mentions

Call by the start and end dates of a required time interval

int  CopyRates(
   string           symbol_name,       // symbol name
   ENUM_TIMEFRAMES  timeframe,         // period
   datetime         start_time,        // start date and time
   datetime         stop_time,         // end date and time
   MqlRates         rates_array[]      // target array to copy
   );




void OnStart()
  {
//---
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied=CopyRates(Symbol(),0,0,100,rates);
   if(copied>0)
     {
      Print("Bars copied: "+copied);
      string format="open = %G, high = %G, low = %G, close = %G, volume = %d";
      string out;
      int size=fmin(copied,10);
      for(int i=0;i<size;i++)
        {
         out=i+":"+TimeToString(rates[i].time);
         out=out+" "+StringFormat(format,
                                  rates[i].open,
                                  rates[i].high,
                                  rates[i].low,
                                  rates[i].close,
                                  rates[i].tick_volume);
         Print(out);
        }
     }
   else Print("Failed to get history data for the symbol ",Symbol());
  }

Thank You.

Documentation on MQL5: Timeseries and Indicators Access / CopyRates
Documentation on MQL5: Timeseries and Indicators Access / CopyRates
  • www.mql5.com
Gets history data of MqlRates structure of a specified symbol-period in specified quantity into the rates_array array. The elements ordering of the copied data is from present to the past, i.e., starting position of 0 means the current bar. If you know the amount of data you need to copy, it should better be done to a statically allocated...
 
francisalmeida:

Dear Members,

I would really appreciate if anyone can guide me with this.

I want to retrieve the rates using the copyRates() function between 2 specified timings:


Start Time: currentTime() 

End Time: currentTime()-30 minutes.


I found the sample code in the below link, but not sure how to pass the 2 date/time parameters.

https://www.mql5.com/en/docs/series/copyrates

On the page, it mentions

Call by the start and end dates of a required time interval


Thank You.

i your version you count the bars.

you have to enter a variable in datetime formart

as exemple

TimeCurrent() = actual time or start time


TimeCurrent()-xxxxxx you can use as endtime or you use

D`01.01.1970`

 
francisalmeida: not sure how to pass the 2 date/time parameters.

Do it exactly as you want. Just pass them.

Documentation
Your code
int  CopyRates(
string          symbol_name,  // symbol name
ENUM_TIMEFRAMES timeframe,    // period
datetime        start_time,   // start date and time
datetime        stop_time,    // end date and time
MqlRates        rates_array[] // target array to copy
);
   int copied=CopyRates(
Symbol(), // symbol name
0,        // Don't hard code constants PERIOD_CURRENT
0,        // Start Time:   TimeCurrent()
100,      // End Time:     TimeCurrent() - 30*60 // 30 Min
rates     // array
);
 

That works perfectly. Thank you very much for your time and inputs William Roeder and Amando, really appreciate it.

Now what I'm looking to do is as below:

Normally a candlestick is formed starting from a fixed time for example: A 30 min candle is formed from 0800 to 0830 and the next one is 0830 to 0900. The closing of the candle is onTick()

I would like to create a candle using CopyRates()  in the reverse order where the statring point is the CurrentTickTime and the end of the array is CurrentTickTime - 30 minutes. Like a floating candle of the last 30 minutes.

Below is the code I have so far with help from the previous comments.. I will be grateful for any further guidance to achieve what i'm looking to do.

void OnTick()
  {

int Interval = 30;

datetime d1=TimeCurrent();
datetime d2=TimeCurrent()-Interval*60;

   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   
   
int copied=CopyRates(
                        Symbol(),             // symbol name
                        PERIOD_CURRENT,       // Don't hard code constants PERIOD_CURRENT
                        d1,                   // Start Time:   TimeCurrent()
                        d2,                   // End Time:     TimeCurrent() - 30*60 // 30 Min
                        rates                 // array
                     );
   
    if(copied>0)
     {
      Print("Bars copied: "+copied);
      string format="open = %G, high = %G, low = %G, close = %G, volume = %d";
      string out;
      int size=fmin(copied,10);
      for(int i=0;i<size;i++)
        {
         out=i+":"+TimeToString(rates[i].time);
         out=out+" "+StringFormat(format,
                                  rates[i].open,
                                  rates[i].high,
                                  rates[i].low,
                                  rates[i].close,
                                  rates[i].tick_volume);
                                  
         Print(out);
        }
     }
   else Print("Failed to get history data for the symbol ",Symbol());
Reason: