CopyRates problem

 

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 ?

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

 

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.

 
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. 

 
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.

 
ok, seems to be so.
Reason: