CopyTime only copy 1 rate?

 
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[])
{
        int bars=Bars(Symbol(),0);
        //Print("Bars = ",bars,", rates_total = ",rates_total,",  prev_calculated = ",prev_calculated);

        int count = rates_total - prev_calculated + 1;
        if(count>rates_total)
                count = rates_total;
        datetime st[];
        ArrayResize(st, count);
        PrintFormat("size=%d", ArraySize(st));

        int copyed = CopyTime(Symbol(),Period(),count-1, count,st); //why it only return 1?
        PrintFormat("count=%d, copyed=%d, size=%d", count, copyed, ArraySize(st));

        return(rates_total);
}

Why the copyed  not equal count ?  In fact the CopyTime return 1,  and the  ArraySize(st) also is 1.

 

Hi there,

        int copyed = CopyTime(Symbol(),Period(),count-1, count,st); //why it only return 1?
        PrintFormat("count=%d, copyed=%d, size=%d", count, copyed, ArraySize(st));

    Cause this is a function and it returns error code.

Regards 

OOP in MQL5 by Example: Processing Warning and Error Codes
  • 2010.05.26
  • KlimMalgin
  • www.mql5.com
The article describes an example of creating a class for working with the trade server return codes and all the errors that occur during the MQL-program run. Read the article, and you will learn how to work with classes and objects in MQL5. At the same time, this is a convenient tool for handling errors; and you can further change this tool according to your specific needs.
 

I think i havn't misunderstand it,  in  help document :

CopyTime  Return Value:

Returns the copied data count or -1 in case of an error.


and the running result:

the array:  datetime st[];

before call CopyTime, the size is count. (a big number)

after called, the size is change to 1.

 

Hi,

My mistake.

 

 (AUDUSD,M1) 15:53:25 Bars = 100001, rates_total = 100001,  prev_calculated = 0
 (AUDUSD,M1) 15:53:25 size=100001
 (AUDUSD,M1) 15:53:25 count=100001, copyed=1, size=1

 (AUDUSD,M1) 15:53:56 Bars = 100001, rates_total = 100001,  prev_calculated = 100000
 (AUDUSD,M1) 15:53:56 size=2
 (AUDUSD,M1) 15:53:56 count=2, copyed=2, size=2

 (AUDUSD,M1) 15:54:45 Bars = 100002, rates_total = 100002,  prev_calculated = 100001
 (AUDUSD,M1) 15:54:45 size=2
 (AUDUSD,M1) 15:54:45 count=2, copyed=2, size=2

You are trying to copy date from bar 100001, count 100001, but 100001 is the last bar and you are trying to read data which do not exists.

Regards

PS: I added this line at the start of the function : if (prev_calculated == rates_total) return(rates_total); 

 
ludsic:

Hi,

My mistake.

 

 (AUDUSD,M1) 15:53:25 Bars = 100001, rates_total = 100001,  prev_calculated = 0
 (AUDUSD,M1) 15:53:25 size=100001
 (AUDUSD,M1) 15:53:25 count=100001, copyed=1, size=1

 (AUDUSD,M1) 15:53:56 Bars = 100001, rates_total = 100001,  prev_calculated = 100000
 (AUDUSD,M1) 15:53:56 size=2
 (AUDUSD,M1) 15:53:56 count=2, copyed=2, size=2

 (AUDUSD,M1) 15:54:45 Bars = 100002, rates_total = 100002,  prev_calculated = 100001
 (AUDUSD,M1) 15:54:45 size=2
 (AUDUSD,M1) 15:54:45 count=2, copyed=2, size=2

You are trying to copy date from bar 100001, count 100001, but 100001 is the last bar and you are trying to read data which do not exists.

Regards

PS: I added this line at the start of the function : if (prev_calculated == rates_total) return(rates_total); 


I can't find any error in the code:
      int count = rates_total - prev_calculated + 1;
        if(count>rates_total)
                count = rates_total;
int copyed = CopyTime(Symbol(),Period(),count-1, count,st);

copy start:  from rates_toatal-1  to  0,     total: rates_toatal

 

Hi,

 

Simple example ...

 

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 Red
#property indicator_label1 "Test"

double ExtMapBuffer1[],iHigh[];

int OnInit()
{
  SetIndexBuffer(0,ExtMapBuffer1,INDICATOR_DATA);
  SetIndexBuffer(1,iHigh,INDICATOR_CALCULATIONS);
  IndicatorSetString(INDICATOR_SHORTNAME,"mql5.com forum");

  return(0);
}

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[])
{
   int limit;

   if(prev_calculated==0)
      limit=0;
   else limit=prev_calculated-1;

   CopyHigh(Symbol(),0,limit,rates_total,iHigh);
  
   for(int i=limit;i<rates_total;i++)
    { 
     ExtMapBuffer1[i] = iHigh[i];
    }
   return(rates_total);}        

 

Regards

 
casewidth:

Why the copyed  not equal count ?  In fact the CopyTime return 1,  and the  ArraySize(st) also is 1.


Have you ever read description of the CopyTime function? You have just rates_total values in array time[] and you try to copy additional data from rates_total position-1 (last index in array time). What do you expect to see? https://www.mql5.com/en/docs/series/copytime

The function gets to time_array history data of bar opening time for the specified symbol-period pair in the specified quantity. It should be noted that elements ordering is from present to past, i.e., starting position of 0 means the current bar.


When copying the yet unknown amount of data, it is recommended to use dynamic array as a target array, because if the requested data count is less (or more) than the length of the target array, function tries to reallocate the memory so that the requested data fit entirely.

If you know the amount of data you need to copy, it should better be done to a statically allocated buffer, in order to prevent the allocation of excessive memory.

No matter what is the property of the target array - as_series=true or as_series=false. Data will be copied so that the oldest element will be located at the start of the physical memory allocated for the array. There are 3 variants of function calls.

Documentation on MQL5: Timeseries and Indicators Access / CopyTime
  • www.mql5.com
Timeseries and Indicators Access / CopyTime - Documentation on MQL5
Reason: