Download MetaTrader 5

CopyRates problem

To add comments, please log in or register
Have you installed MetaTrader 5? Find out more about it!
chinaski
890
chinaski 2014.08.18 03:13 

When i do a copy rates with start index=0, count=2, then rates[1] seems to be the latest bar as i tested by a small script.


void test2(void)
{
                MqlRates                                        rates[];
                int copied=CopyRates(Symbol(),Period(),0,2,rates);
                if(copied > 0)
                {
                        for(int i=0;i < copied;++i)
                        {
                                Print(StringFormat("%d o=%.8f c=%.8f %s",i,rates[i].open,rates[i].close,TimeToString(rates[i].time)));

                        }
                }

}


This is the contrary to what i read in the MQL5 Help:

"The elements ordering of the copied data is from present to the past, i.e., starting position of 0 means the current bar."

According my test, the current bar is stored in 1 not in 0.

Can anyone confirm this ?

Rodrigo Malacarne
Moderator
9892
Rodrigo Malacarne 2014.08.18 05:56  
chinaski:

When i do a copy rates with start index=0, count=2, then rates[1] seems to be the latest bar as i tested by a small script.

This is the contrary to what i read in the MQL5 Help:

"The elements ordering of the copied data is from present to the past, i.e., starting position of 0 means the current bar."

According my test, the current bar is stored in 1 not in 0.

Can anyone confirm this ?

Hello chinaski,

if you want your array to be copied from present to the past, you have to explicitly use 

ArraySetAsSeries(rates,true);
Alain Verleyen
Moderator
30723
Alain Verleyen 2014.08.18 09:55  
chinaski:

When i do a copy rates with start index=0, count=2, then rates[1] seems to be the latest bar as i tested by a small script.



This is the contrary to what i read in the MQL5 Help:

"The elements ordering of the copied data is from present to the past, i.e., starting position of 0 means the current bar."

According my test, the current bar is stored in 1 not in 0.

Can anyone confirm this ?

It's exactly what is described in MQL5 documentation.

Your problem comes from the indexation of your result array, as Malacarne said you.

chinaski
890
chinaski 2014.08.18 16:21  

Hello Angevoyageur,

don't understand this.

From Doc:

"The elements ordering of the copied data is from present to the past, i.e., starting position of 0 means the current bar."


So according what Documentation says: 0 in the target array (ordering of the copied data) is current bar but it is NOT.

1 is the index of the current bar.

So, i mean, without using

ArraySetAsSeries

it should carry the current bar in index=0 and not in index=1.

Rodrigo Malacarne
Moderator
9892
Rodrigo Malacarne 2014.08.18 16:47  
chinaski:

Hello Angevoyageur,

don't understand this.

From Doc:

"The elements ordering of the copied data is from present to the past, i.e., starting position of 0 means the current bar."


So according what Documentation says: 0 in the target array (ordering of the copied data) is current bar but it is NOT.

1 is the index of the current bar.

So, i mean, without using

it should carry the current bar in index=0 and not in index=1.

Hello chinaski,

you are copying 2 bars of data. In this case, the standard indexation of bars in C++ (and MQL5) is to set 0 as the oldest bar and 1 as the current bar.

If you want to invert the indexation, you have to use

ArraySetAsSeries(rates,true);

After doing that, the MT5 terminal will interprete 0 as the current and, of course, 1 as he oldest bar. 

Alain Verleyen
Moderator
30723
Alain Verleyen 2014.08.18 18:29  
chinaski:

Hello Angevoyageur,

don't understand this.

From Doc:

"The elements ordering of the copied data is from present to the past, i.e., starting position of 0 means the current bar."


So according what Documentation says: 0 in the target array (ordering of the copied data) is current bar but it is NOT.

1 is the index of the current bar.

So, i mean, without using

it should carry the current bar in index=0 and not in index=1.

You misunderstood the documentation. It means the source data are always copied from present to past, BUT you are accessing the target array and there it depends of the indexation. See explanation of Malacarne above.

chinaski
890
chinaski 2014.08.18 21:51  
ok, seems to be so.
To add comments, please log in or register