How to loop the indicator buffer efficiently and how to declare dynamic array?

 

Hi all :

I am a newbie to the MT4 programming. I have read through most of the MT4 book but I wasn't able to find any way to loop through the indicator array. 

I have two questions. 

1. the first question is how to loop into the indicator efficiently.  

For example, I have a fractal indicator and I want to examine its historical value. A simple task is to count how many swing highs and how many swing lows.

I have a simple solution as: 

 

int high_counter=0;

int low_counter=0;

for (int i=0; i < bars; i++){

    if (ExtUpFractalsBuffer[i] != 0 ) high_counter++;
    if (ExtDownFractalsBuffer[i] != 0) low_counter++;

}       

The  ExtUpFractalsBuffer and ExtDownFractalsBuffer are the buffers in the Fractal Indicator. 

Obviously,  this is not an efficient solution. Is there any better ways to do that?

2. The second question is how to declare a dynamic array in MQL4? I don't mean the array-timeseries. 

I mean something equivalent like vector<double> in C++ or ArrayList in C#. The array can accept new element, delete existing element and its size is variable. 

 

Thanks

 
asuralm:

Hi all :

2. The second question is how to declare a dynamic array in MQL4? I don't mean the array-timeseries. 

I mean something equivalent like vector<double> in C++ or ArrayList in C#. The array can accept new element, delete existing element and its size is variable. 

You can't . . . use ArrayResize() to change the size of the array and ArraySetAsSeries() to change the way elements are added/removed from the array . . . 
 
asuralm:
1. the first question is how to loop into the indicator efficiently.  

For example, I have a fractal indicator and I want to examine its historical value. A simple task is to count how many swing highs and how many swing lows.

2. The second question is how to declare a dynamic array in MQL4? I don't mean the array-timeseries. 
  1. Create an extra array and keep a running count.
    ExtUpFractalsBuffer[i] = ..
    // if (ExtUpFractalsBuffer[i] != 0) upFractalCount[i] = upFractalCount[i+1] + 1;
    // else                             upFractalCount[i] = upFractalCount[i+1];
    upFractalCount[i] = upFractalCount[i+1] + (ExtUpFractalsBuffer[i] != 0); // Equivalent hack
    

  2. A CI can have up to 8 buffers automatically sized. They don't all need to be displayed.
    #property indicator_buffers 1 // number of displayed buffers
    int init(){
        //---- 2 additional buffers are used for counting.
        IndicatorBuffers(3);
        SetIndexBuffer(0,ind_buffer1);
        SetIndexBuffer(1,ind_buffer2);
        SetIndexBuffer(2,ind_buffer3);
    Once you go beyond needing 8 then you must manage your own. From my code
    bool    ResizeBuffer(double& buffer[], int size){
        if (ArrayRange(buffer,0) != size){          // ArrayRange allows 1D or 2D arrays
            ArraySetAsSeries(buffer, false);    // Shift values B[2]=B[1]; B[1]=B[0]
            if (ArrayResize(buffer, size) <= 0){ DisableTrading(
                "ArrayResize [2] failed: " + GetLastError() );      return(false);  }
            ArraySetAsSeries(buffer, true);
        }
        return(true);
    }
    

 
WHRoeder:
  1. Create an extra array and keep a running count.
  2. A CI can have up to 8 buffers automatically sized. They don't all need to be displayed.Once you go beyond needing 8 then you must manage your own. From my code

Thanks WHRoeder. It's a clever way to do the countering.