ArrayCopySeries and ArrayCopyRates on MQL5?

 

Hello all,

I fount out that ArrayCopySeries  and ArrayCopyRates  are 2 useful functions in MQL4. It does not really copy anything except when you call it first time. That saves some manual labour when I work in multiple timeframes.

And since it does not actually copy the whole array every time I call, It make program run faster.

Problem is these 2 functions does not exist in MQL5, only in MQL4.

Any one have same idea?

Do you use ArrayCopySeries  and ArrayCopyRates in MQL4 and found disappointed when they are not available in MQL5?

 
MarkJoy:

Hello all,

I fount out that ArrayCopySeries  and ArrayCopyRates  are 2 useful functions in MQL4. It does not really copy anything except when you call it first time. That saves some manual labour when I work in multiple timeframes.

And since it does not actually copy the whole array every time I call, It make program run faster.

Problem is these 2 functions does not exist in MQL5, only in MQL4.

Any one have same idea?

Do you use ArrayCopySeries  and ArrayCopyRates in MQL4 and found disappointed when they are not available in MQL5?

MQL5 is different in quite a few ways. It might take you a while to port your code (depending on the complexity)

ArrayCopySeries can be replaced by CopyTime, CopyHigh etc https://www.mql5.com/en/docs/series/copytime

Also, these methods consume less CPU cycles than you might expect.

Documentation on MQL5: Timeseries and Indicators Access / CopyTime
Documentation on MQL5: Timeseries and Indicators Access / CopyTime
  • www.mql5.com
Timeseries and Indicators Access / CopyTime - Reference on algorithmic/automated trading language for MetaTrader 5
 

I know about the CopyTime, CopyHigh. MQL4 has those functions.

My point is it is much simpler when working with MQL4 by using ArrayCopySeries or ArrayCopyRates .

1> The return value of ArrayCopySeries tell me if there is a new bar on other timeframe.

2> With ArrayCopySeries  I have the direct access to Series array without creating a buffer array . On MQL5 I have to create a buffer array.

3> Since I have to create a buffer array, I have to manually check if it is valid, if there is a new bar and have to increase the array.


I wish that MQL5 provides those functions.

 
MarkJoy:

I know about the CopyTime, CopyHigh. MQL4 has those functions.

My point is it is much simpler when working with MQL4 by using ArrayCopySeries or ArrayCopyRates .

1> The return value of ArrayCopySeries tell me if there is a new bar on other timeframe.

2> With ArrayCopySeries  I have the direct access to Series array without creating a buffer array . On MQL5 I have to create a buffer array.

3> Since I have to create a buffer array, I have to manually check if it is valid, if there is a new bar and have to increase the array.


I wish that MQL5 provides those functions.

1) Using the return of ArrayCopySeies can be unreliable once you reach MaxBars because the number of bars can stay the same, you should probably check for a change in bar 0 time as well.

2) Yes you loose direct access to the arrays, this is one of many things that are different between the platforms.

3) You don't need to increase the array size, if you use dynamic arrays this will be done fro you.


MQL5 has a new architecture, in theory it is more advanced. Sadly we have to port our code at least once

 
James Cater:

1) Using the return of ArrayCopySeies can be unreliable once you reach MaxBars because the number of bars can stay the same, you should probably check for a change in bar 0 time as well.

2) Yes you loose direct access to the arrays, this is one of many things that are different between the platforms.

3) You don't need to increase the array size, if you use dynamic arrays this will be done fro you.


MQL5 has a new architecture, in theory it is more advanced. Sadly we have to port our code at least once


1)  MaxBars is some what far than enough for normal use. But thanks for bringing that point here.

3) If I want to keep track of Series Array, I still have to ArrayResize everytime a new bar come (especially in multiple timeframes case). Because I only copy whole array (with defined size) at init, then every tick / new bar I have to update the array.

 
MarkJoy:


1)  MaxBars is some what far than enough for normal use. But thanks for bringing that point here.

3) If I want to keep track of Series Array, I still have to ArrayResize everytime a new bar come (especially in multiple timeframes case). Because I only copy whole array (with defined size) at init, then every tick / new bar I have to update the array.

Resizing an array is very low on cycles if you are adding to the end of the array, just make sure you set a suitably large reserve size so it doesn't have to reallocate and copy the entire contents too often.

You can test all the overheads quite accurately with the built in performance analyser (Debug/Start Profilng)

 
James Cater:

Resizing an array is very low on cycles if you are adding to the end of the array, just make sure you set a suitably large reserve size so it doesn't have to reallocate and copy the entire contents too often.

You can test all the overheads quite accurately with the built in performance analyser (Debug/Start Profilng)


I only resize when new bar comes. I think that copy the whole array and ArrayResize have similiar cycles, that's why I try to avoid both. Only resize when new bar comes.
 
MarkJoy:

I only resize when new bar comes. I think that copy the whole array and ArrayResize have similiar cycles, that's why I try to avoid both. Only resize when new bar comes.

You can resize the array just after you copy it to add the 1000 reserve buffer. Once you have done this the next resizes will be negligible...

(cycles are taken from the Profiler stats with 100 iterations of the code below)

      datetime timeArr[];
      int      count = Bars(_Symbol, PERIOD_H1);                           // 1 cycle
      int      timeRows = CopyTime(_Symbol, PERIOD_H1, 0, count, timeArr); // 6 cycles
      
      if (timeRows > -1)
      {
         ArrayResize(timeArr, timeRows+1, 1000);    // 4 cycles   (take a 4 cycle hit up front)
         ArrayResize(timeArr, timeRows, 1000);      // 0.1 cycle
      }

      //...
      ArrayResize(timeArr, timeRows+1, 1000);       // 0.1 cycle
      ArrayResize(timeArr, timeRows+2, 1000);       // 0.1 cycle
      ArrayResize(timeArr, timeRows+3, 1000);       // 0.1 cycle
      ArrayResize(timeArr, timeRows, 1000);         // 0.1 cycle
 
James Cater:

You can resize the array just after you copy it to add the 1000 reserve buffer. Once you have done this the next resizes will be negligible...

(cycles are taken from the Profiler stats with 100 iterations of the code below)


Thank you for posting the code and explanation. I have some questions regarding "cycle":

What is the cycle here? Is it CPU cycle? How can it be 0.1 cycle?

 
My first post in this forum. Trying to do my first conversion from mql4 to mql5 but don't know or understand how to convert ArrayCopyRates to work in mql5.
I've been searching the web for a few days trying to find a working example but haven't found one yet. I've found a lot of documentation that probably explains how to do it but no example code showing how to implement it.
Could someone be so kind as to show me a working code snippet of how to do this conversion?
double ArrayM1[][6];
int ArrSizeM1;

ArrayCopyRates(ArrayM1, Symbol(), PERIOD_M1);
ArrSizeM1 = ArrayRange(ArrayM1, 0);
Thank you!
 
Frank Jarrett :
My first post in this forum. Trying to do my first conversion from mql4 to mql5 but don't know or understand how to convert ArrayCopyRates to work in mql5.
I've been searching the web for a few days trying to find a working example but haven't found one yet. I've found a lot of documentation that probably explains how to do it but no example code showing how to implement it.
Could someone be so kind as to show me a working code snippet of how to do this conversion?
Thank you!

An example of working with OHLC (MqlRates structure and function  CopyRates)

//+------------------------------------------------------------------+
//|                                                      Test_en.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.000"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int start_pos=0,count=3;
   if(CopyRates(Symbol(),Period(),start_pos,count,rates)!=count)
      return;
//---
   string text="";
   for(int i=0; i<count; i++)
      text=text+"bar #"+IntegerToString(i)+", time: "+TimeToString(rates[i].time,TIME_DATE|TIME_MINUTES)+"\n";
   Print(text);
  }
//+------------------------------------------------------------------+


Result:

2019.10.30 08:54:40.641 Test_en (EURUSD,H1)     bar #0, time: 2019.10.30 08:00
2019.10.30 08:54:40.688 Test_en (EURUSD,H1)     bar #1, time: 2019.10.30 07:00
2019.10.30 08:54:40.688 Test_en (EURUSD,H1)     bar #2, time: 2019.10.30 06:00
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...
Files:
Test_en.mq5  3 kb
Reason: