Which function replaced the iMaOnArray in MT4? - page 2

 
PzTrading:


People is going to use MT4 for a long, long time.

Yep  
 
PzTrading:

I just tested it, and it is x10 times slower than his MT4 cousin. I will just code it myself.

MT5 has really cool new features, but imo, it has some setbacks, like this.

Did you used ImaOnArray? Now code it yourself. LOL

People is going to use MT4 for a long, long time.

People perhaps will continue to use MT4 for a long time but certainly not for such reason. The link I posted is only for reference, the approach using SimpleMAOnBuffer() is from Metaquotes and certainly a lot more fast, did you test it ?

 
angevoyageur:

People perhaps will continue to use MT4 for a long time but certainly not for such reason. The link I posted is only for reference, the approach using SimpleMAOnBuffer() is from Metaquotes and certainly a lot more fast, did you test it ?

I did! I am struggling with it, actually. I must be doing something wrong because the same code triggers different results, like a coin flip.

Without changing the code, sometimes I get: 2013.08.23 21:37:35    TestOscillator (EURUSD,D1)    array out of range in 'TestOscillator.mq5' (288,35)

Edited: people is still using MT4 because all the apps/ea's/indicators they built over time do not work for MT5, so the migration will be slow.

 
PzTrading:

I did! I am struggling with it, actually. I must be doing something wrong because the same code triggers different results, like a coin flip.

Without changing the code, sometimes I get: 2013.08.23 21:37:35    TestOscillator (EURUSD,D1)    array out of range in 'TestOscillator.mq5' (288,35)

If you post the code I can try to help you.
 
angevoyageur:
If you post the code I can try to help you.

Thanks! This is the my OnCalculate Function.

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    //--
    //-- Turn data into series
    //--
    ArraySetAsSeries(close,true);
    ArraySetAsSeries(high,true);
    ArraySetAsSeries(low,true);
    ArraySetAsSeries(time,true);

    //--
    //-- Limit, Start and Rates 
    //--

    // Iteration limit
    int start = 1;    
    int limit;
    
    // If not enough data
    if(rates_total < OsPeriod) return(0);
   
    // Calculate limit and reset buffers
    if(prev_calculated > rates_total || prev_calculated <= 0)
    {
        limit=rates_total-OsPeriod;
    } else {
        limit=rates_total-prev_calculated;
    }
    
    //--
    //-- Iteration
    //--

    // First loop
    for(int i = limit; i >= start; i--)
    {
        // Open and close
        FextMapBuffer1[i] = open[i];
        FextMapBuffer2[i] = close[i];
    }
    
    // Calculate MA's of these two simple values into buffers 3 and 4
    SmoothedMAOnBuffer(rates_total, prev_calculated, 0, OsPeriod, FextMapBuffer1, FextMapBuffer3);
    SmoothedMAOnBuffer(rates_total, prev_calculated, 0, OsPeriod, FextMapBuffer2, FextMapBuffer4);
}

I am doing a little exercise: saving in two simple buffers the open and close prices, and then drawing a moving average of them on the chart.

The SmoothedMAOnBuffer sometimes yields an out of range error. Am I calculating badly the rates_total and prev_calculated?

Other times, however, it works and draws in a very strange way, probably because I am calculating the MA with unexistant data.

My confusion takes place because the outcome is not deterministic. It changes every time I reload. LOL.

Those numbers are also confusing, I am initializing the buffers as zero.

ArrayInitialize(FextMapBuffer1, 0);
ArrayInitialize(FextMapBuffer2, 0);
ArrayInitialize(FextMapBuffer3, 0);
ArrayInitialize(FextMapBuffer4, 0);
 
PzTrading:

Thanks! This is the my OnCalculate Function.

I am doing a little exercise: saving in two simple buffers the open and close prices, and then drawing a moving average of them on the chart.

The SmoothedMAOnBuffer sometimes yields an out of range error. Am I calculating badly the rates_total and prev_calculated?

I suppose you have also declared your FextMapBufferx with ArraySetAsSeries to true ?

You have to add the following at the end of OnCalculate.

//--- return value of prev_calculated for next call
   return(rates_total);

If it still doesn't work, please provides full code to allow me to compile it.

 
angevoyageur:

I suppose you have also declared your FextMapBufferx with ArraySetAsSeries to true ?

You have to add the following at the end of OnCalculate.

If it still doesn't work, please provides full code to allow me to compile it.

I have finally solved it (partially). Since I was ignoring the first rates until having enough to apply the first average, those values where filled with double empty values, which turn out to be enormous and the moving average went nuts. All I had to do was setting the start parameter of SmoothedMAOnBuffer accordingly.

SmoothedMAOnBuffer(rates_total, prev_calculated, OsPeriod, OsPeriod, FextMapBuffer1, FextMapBuffer3);
SmoothedMAOnBuffer(rates_total, prev_calculated, OsPeriod, OsPeriod, FextMapBuffer2, FextMapBuffer4);
However, now I have another problem. My indicator iterates bars up to shift 1, and ignores the unclosed bar. But the SmoothedMAOnBuffer function does take it into account and the last value of the result buffer is calculated, resulting in a very odd "spike" bar for the last oscillator value. How can I force the moving average functions to ignore bar zero?
 
PzTrading:

I have finally solved it (partially). Since I was ignoring the first rates until having enough to apply the first average, those values where filled with double empty values, which turn out to be enormous and the moving average went nuts. All I had to do was setting the start parameter of SmoothedMAOnBuffer accordingly.

However, now I have another problem. My indicator iterates bars up to shift 1, and ignores the unclosed bar. But the SmoothedMAOnBuffer function does take it into account and the last value of the result buffer is calculated, resulting in a very odd "spike" bar for the last oscillator value. How can I force the moving average functions to ignore bar zero?
I just modified the MovingAverages.mqh myself to never get out of range using ArraySize and ignoring bar zero. Go figure. Issue solved. Thanks!
Documentation on MQL5: Array Functions / ArraySize
  • www.mql5.com
Array Functions / ArraySize - Documentation on MQL5
 
PzTrading:
I just modified the MovingAverages.mqh myself to never get out of range using ArraySize and ignoring bar zero. Go figure. Issue solved. Thanks!

It's not a good idea. MovingAverages.mqh is part of MT5 and will be updated with the next build, so your modification will be lost.

You have to set a value to all indices of the buffers. So to ignore bar zero, you only have to set the values of your buffers to EMPTY_VALUE.

// Calculate MA's of these two simple values into buffers 3 and 4
   SmoothedMAOnBuffer(rates_total,prev_calculated,OsPeriod,OsPeriod,FextMapBuffer1,FextMapBuffer3);
   SmoothedMAOnBuffer(rates_total,prev_calculated,OsPeriod,OsPeriod,FextMapBuffer2,FextMapBuffer4);

   FextMapBuffer1[0]=EMPTY_VALUE;
   FextMapBuffer2[0]=EMPTY_VALUE;
   FextMapBuffer3[0]=EMPTY_VALUE;
   FextMapBuffer4[0]=EMPTY_VALUE;
 
angevoyageur:

It's not a good idea. MovingAverages.mqh is part of MT5 and will be updated with the next build, so your modification will be lost.

You have to set a value to all indices of the buffers. So to ignore bar zero, you only have to set the values of your buffers to EMPTY_VALUE.

Thanks Angevoageur! (I saved with my own library name)
Reason: