Copy Array in 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?

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

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

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

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

  2. 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)

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

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?