MQL5 - Problem with CopyBuffer count parameter

 
Hi,
I'm learning mql5 and I'm having a problem with CopyBuffer.
When I change the count parameter of CopyBuffer it shows me different values and that is affecting my entry points


Please, see below:

int OnInit()
{


  sma3_handle=iMA (Symbol(),PERIOD_M5,3 ,0,MODE_SMA,PRICE_CLOSE);
}


void OnTick()
{

        CopyBuffer(sma3_handle,0,0,2,sma3);
        Alert("Case 1*****************"+sma3[0]);

        CopyBuffer(sma3_handle,0,0,50,sma3);
        Alert("Case 2*****************"+sma3[0]);
}

I think that sma3[0] would have to have the same value at case 1 and case 2 because [0] means the current "position" of sma, but this is not happening.

What Am I doing wrong? What did I understand wrong? 
Sorry for my English
Thanks
Rogerio

 

Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5 Tries very hard to explain the ordering of the data.

Counting of elements of copied data (indicator buffer with the index buffer_num) from the starting position is performed from the present to the past, i.e., starting position of 0 means the current bar (indicator value for the current bar).

:

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.

In case one, you are retrieving two values starting at zero (newest.) Therefor sma3[1] is the newest and sma3[0] is one back.

In case two, you are retrieving 50 values starting at zero (newest.) Therefor sma3[49] is the newest and sma3[48] is one back and sma3[0] is 50 bars ago.

So of course they don't match.

Set the array as series (Array Functions / ArraySetAsSeries - Reference on algorithmic/automated trading language for MetaTrader 5) after filling and you will get sma3[0] is newest, sma3[1] is one back, etc.

 
whroeder1:

Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5 Tries very hard to explain the ordering of the data.

In case one, you are retrieving two values starting at zero (newest.) Therefor sma3[1] is the newest and sma3[0] is one back.

In case two, you are retrieving 50 values starting at zero (newest.) Therefor sma3[49] is the newest and sma3[48] is one back and sma3[0] is 50 bars ago.

So of course they don't match.

Set the array as series (Array Functions / ArraySetAsSeries - Reference on algorithmic/automated trading language for MetaTrader 5) after filling and you will get sma3[0] is newest, sma3[1] is one back, etc.


It worked! Thanks a lot whroeder1

Reason: