Files
xMQL4/Docs/Samples/BarIndex.md
T
2024-01-25 04:03:36 +03:30

1.5 KiB

BarIndex

in pine script bar_index reffers to current candle number, in reversal indexing. in MQL for accessing bar_index to calculate indicator data, we have to do something like this:

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[])
  {
    //
    // here we define our limit ...
    int limit = rates_total - prev_calculated;

    //
    // if our indicator requires some candles to pass as a length,
    // and then start calculating by past passed values, by this way
    // we are going to inform MQL Compiler, which do nothing, until 
    // required length passed ...
    if(rates_total <= InpBullsPeriod) {
        return(0);
    }

    //
    // because bar index a zero start indexing system, for fixing 
    // qctual indexing in for loops, we have to call this ...
    if(prev_calculated > 0) {
        limit++;
    }
    
    //
    // Loop through Bar Indexes ...
    for(int i = 0; i < limit; i++) {
      //
      // Filling Exists Buffers, based on current Bar Index and required Conditions ...
      ExtTempBuffer[i]=iMA(NULL,0,InpBullsPeriod,0,MODE_EMA,PRICE_CLOSE,i);
      ExtBullsBuffer[i]=high[i]-ExtTempBuffer[i];
    }
    
    //
    return(rates_total);
}