Old indicator conversion

 
Hi,

I'm struggling in converting this old indicator to a newer version that uses OnCalculate() instead of onstart().

In the version with OnCalculate() the indicator stops giving any type of indication.

I have read the documentation but there's something I'm missing about how to use the OnCalculate() function and the differences between using onstart().

This indicator has no relevance for me, I just want to learn from what I'm doing wrong and I would like to understand how to convert old indicators into new versions.

Thanks to all who want to explain me.

OLD INDICATOR

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Gray
#property indicator_color2 RoyalBlue
extern int MaPriod = 34;
extern double value = 0.5;

double buffer1[];
double buffer2[];
double buffer3[];

double minVolume;
int init() {
    
    minVolume = Volume[iLowest(NULL, 0,MODE_VOLUME, WHOLE_ARRAY, 0)];
    
    IndicatorBuffers(3);
    SetIndexStyle(0,DRAW_HISTOGRAM, EMPTY, 3);
    SetIndexStyle(1,DRAW_HISTOGRAM, EMPTY, 3);
    
    
    SetIndexBuffer(0, buffer1);
    SetIndexBuffer(1, buffer2);
    SetIndexBuffer(2, buffer3);
    
      
    IndicatorShortName("Volatility("+MaPriod+ ")");
    SetIndexDrawBegin(0,MaPriod);
    
    
    SetIndexLabel(0, "ma");
    SetIndexLabel(1, "value");
    SetIndexLabel(2, "action");

    IndicatorDigits(Digits + 3);
    
    return INIT_SUCCEEDED;    
}


int start() {

    int i;
    int counted_bars=IndicatorCounted();

     if(counted_bars > 0) {
        counted_bars--;
    }
    
    int limit = Bars-counted_bars-1;
   
    for(i = limit - 1 ; i >= 0; i--) {
        buffer3[i] = (
            MathAbs(High[i+1] - Low[i]) + 
            MathAbs(High[i] - Low[i+1]) + 
            MathAbs(Close[i+1] - Close[i])
        )  
        * (Volume[i] - minVolume);
    }
    
    if(Bars - limit <  MaPriod*15) {
        limit = limit - MaPriod*15;
    }
    
    for(i = limit -1; i >= 0; i--) {
        buffer1[i] = iMAOnArray(buffer3, 0, MaPriod, 0, MODE_EMA, i);
    }
    
    double sum = 0;
    int count = 0;
    
    for(i = Bars-1; i >= 0; i--) {
        if(buffer1[i] != EMPTY_VALUE) { 
            sum += buffer1[i];
            count++;
        }
    }
    
    sum = sum / count;

    for(i = limit - 1 ; i >= 0; i--) {
        if(sum * value < buffer1[i]) {
            buffer2[i] = buffer1[i];
        } else {
            buffer2[i] = EMPTY_VALUE;
        }
    }
    
    return(0);
}

NEW INDICATOR

#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Green
extern int MaPriod = 34;
extern double value = 0.5;

double buffer1[];
double buffer2[];
double buffer3[];

long minVolume;
int OnInit() {

    minVolume = Volume[iLowest(NULL, 0,MODE_VOLUME, WHOLE_ARRAY, 0)];
    
    IndicatorBuffers(3);
    SetIndexStyle(0,DRAW_HISTOGRAM, EMPTY, 3);
    SetIndexStyle(1,DRAW_HISTOGRAM, EMPTY, 3);
    
    
    SetIndexBuffer(0, buffer1);
    SetIndexBuffer(1, buffer2);
    SetIndexBuffer(2, buffer3);
    
      
    IndicatorShortName("Volatility("+string(MaPriod)+ ")");
    SetIndexDrawBegin(0,MaPriod);
    
    
    SetIndexLabel(0, "ma");
    SetIndexLabel(1, "value");
    SetIndexLabel(2, "action");

    IndicatorDigits(Digits + 3);
    
    return INIT_SUCCEEDED;    
}

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[])
  {
   
   int i;
   int limit = rates_total - prev_calculated-1;
   
   for(i = limit-1; i >= 0; i--) {
        buffer3[i] = (
            MathAbs(High[i+1] - Low[i]) + 
            MathAbs(High[i] - Low[i+1]) + 
            MathAbs(Close[i+1] - Close[i])
        )  
        * (Volume[i] - minVolume);
    }
    
    if(Bars - limit <  MaPriod*15) {
      limit = limit - MaPriod*15;
    }
    
    for(i = limit -1; i >= 0; i--) {
         buffer1[i] = iMAOnArray(buffer3, 0, MaPriod, 0, MODE_EMA, i);
    }
    
    double sum = 0;
    int count = 0;
    
    for(i = Bars-1; i >= 0; i--) {

        if(buffer1[i] != EMPTY_VALUE) { 
            sum += buffer1[i];
            count++;
        }
    }
    
    sum = sum / count;
    
    for(i = limit - 1 ; i >= 0; i--) {
        if(sum * value < buffer1[i]) {
            buffer2[i] = buffer1[i];
        } else {
            buffer2[i] = EMPTY_VALUE;
        }
    }

   return(rates_total);
  }
 
Marco De Angelis:
Hi,

I'm struggling in converting this old indicator to a newer version that uses OnCalculate() instead of onstart().

In the version with OnCalculate() the indicator stops giving any type of indication.

I have read the documentation but there's something I'm missing about how to use the OnCalculate() function and the differences between using onstart().

This indicator has no relevance for me, I just want to learn from what I'm doing wrong and I would like to understand how to convert old indicators into new versions.

Thanks to all who want to explain me.

OLD INDICATOR

NEW INDICATOR

Old:

int i;
int counted_bars=IndicatorCounted();

  if(counted_bars > 0) {
     counted_bars--;
 }
 
int limit = Bars-counted_bars-1;
Comment(limit); // 1

New:

int i;
int limit = rates_total - prev_calculated-1;
Comment(limit); // -1