Search code examples
arraysmql5metatrader5

MQL5 reversing the array gives error


I am trying to copy the stochastic function buffers in the variables. But what I see is that the candles are copied in the array in descending order.

double KArray[],DArray[];
ArraySetAsSeries(KArray,true);
ArraySetAsSeries(DArray,true);
int stochastic_output = iStochastic(_Symbol,PERIOD_M1,5,3,3,MODE_SMA,STO_LOWHIGH);
CopyBuffer(stochastic_output,0,0,15,KArray);


CopyBuffer(stochastic_output,1,0,15,DArray);
for (int i=0;i < Candles_backtest_Stochastic_quantity;i++)
{
PrintFormat("K %d:  %f",i,KArray[i]);
PrintFormat("D %d:  %f",i,DArray[i]);

}

This works well and while printing the array values I am getting the 0th as the current value and the previous values later.

2018.03.22 18:07:23.622 2018.02.01 00:00:00   K 0:  57.291667
2018.03.22 18:07:23.622 2018.02.01 00:00:00   D 0:  63.194444
2018.03.22 18:07:23.622 2018.02.01 00:00:00   K 1:  61.458333
2018.03.22 18:07:23.622 2018.02.01 00:00:00   D 1:  68.754756
2018.03.22 18:07:23.622 2018.02.01 00:00:00   K 2:  70.833333
2018.03.22 18:07:23.622 2018.02.01 00:00:00   D 2:  69.294286
2018.03.22 18:07:23.622 2018.02.01 00:00:00   K 3:  73.972603
2018.03.22 18:07:23.622 2018.02.01 00:00:00   D 3:  57.177428
2018.03.22 18:07:23.622 2018.02.01 00:00:00   K 4:  63.076923

And so on.

But I want to have a reversed array, i.e. the 14th array element must be the 0th and the 0th must be the 14th element of the array.

I tried to make the CopyBuffer() statement to reverse the buffer, but got error, see the example:

2018.03.22 18:11:11.957   Total_back_lagANDvantage_required=3
2018.03.22 18:11:12.073 2018.02.01 00:00:00   K 0:  78.260870
2018.03.22 18:11:12.073 2018.02.01 00:00:00   D 0:  72.579331
2018.03.22 18:11:12.073 2018.02.01 00:00:00   array out of range in 'adxSTUDY.mq5' (173,33)
2018.03.22 18:11:12.073 OnTick critical error
2018.03.22 18:11:12.087 EURUSD,M1: 1 ticks, 1 bars generated. Environment synchronized in 0:00:00.312. Test passed in 0:00:00.594 (including ticks preprocessing 0:00:00.016).
2018.03.22 18:11:12.087 EURUSD,M1: total time from login to stop testing 0:00:00.906 (including 0:00:00.312 for history data synchronization)

Kindly, help me. I do not want to have another buffer to copy the array and reverse it, Is there a way through which I can reverse the array and use it?


Solution

  • I do not want to have another buffer to copy the array and reverse it.

    Right, each copy is expensive in two dimensions - in [TIME]-domain and in [SPACE]-domain. There is for sure better way to avoid any copy to ever take place, for both of these reasons.


    There are two principal approaches:

    A )
    the internal, native MetaTrader Terminal / MQL5 modus operandi, which uses a sort of flag to indicate a reversed-time-stepping addressing on TimeSeries data, where [0] is being always the "NOW" - so changing the flag changes from native-addressing to "natural"-addressing, where [0] is a statically oldest ever cell, but the "NOW"'s address keeps creeping as time is moving forwards. This is the fastest method, yet requires designer to keep attention in which mode the access is set ( uses an On-Off-FSA ).

    B )
    a proxy-mode, where one can use an access function, which internally translates the access mode without having to pay attention to FSA-state.

    Using class-based proxy became even easier way to go in proxy-based operation, as more class-methods allow for designing iterators and other tools, re-using the concept of autonomous proxy-abstracted details and prevent user from "damaging" the state of proxy-abstracted operations and fall outside of proper addressing in either of the actual array[]'s native-states.

    B)-is not any slower than A), yet very handy to use, as one can stop remembering in what native-state the array[] has been left by the last operation, so a comfortable ( similarly if array[]-s get used for emulations of stack-based ops and other higher-level of abstraction data-structures in MQL4/5 domains ).