Custom Indicator Issue

 

Hello all,

I've been working on a custom indicator. I've followed codes from other indicators but i can't seem to make it work. The second StrengthMo works but does not calculate the current value. The first, Strength-Momentum does not work at all. Can anyone help me to identify what the problem is?

I've attached both codes.

#property indicator_separate_window
#property indicator_buffers    2
#property indicator_color1  clrRed
#property indicator_color2  clrLime
//--- input parameters
input int MoPeriod = 2; // StrengthMo Period
//--- buffers
double         BearsBuffer[];
double         BullsBuffer[];
double         BearsMoBuffer[];
double         BullsMoBuffer[];
double         DiffBullsBuffer[];
double         DiffBearsBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
//--- 4 additional buffers are used for counting.
   IndicatorBuffers(6);
      SetIndexBuffer(2,BearsBuffer);
   SetIndexBuffer(3,BullsBuffer);
   SetIndexBuffer(0,BearsMoBuffer);
   SetIndexLabel(0,"BearsMo");
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexDrawBegin(0,MoPeriod);
   SetIndexBuffer(1,BullsMoBuffer);
   SetIndexLabel(1,"BullsMo");
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexDrawBegin(1,MoPeriod);
   SetIndexBuffer(4,DiffBullsBuffer);
   SetIndexBuffer(5,DiffBearsBuffer);
//--- initialization done
   return(INIT_SUCCEEDED);
  }

int start()
  {
   int    i,limit,k,iniStart,counted_bars;
   double BearsMo, BullsMo;
   
   counted_bars = IndicatorCounted();
//----
   if(counted_bars < 0) 
       return(-1);
//----
   if(counted_bars > 0) 
       counted_bars--;
   limit = Bars - counted_bars;
//----
   for(i = limit - 1; i >= 0; i--)
     {
     if (i<MoPeriod) break;
   iniStart = i - MoPeriod;

   if (Close[i] > Open[i])
   {
   BullsBuffer[i] = High[i] - Low[i];
   BearsBuffer[i] = (High[i] - Close[i]) + (Open[i] - Low[i]);
   }
   else if (Close[i] < Open[i])
   {
   BearsBuffer[i] = High[i] - Low[i];
   BullsBuffer[i] = (High[i] - Open[i]) + (Close[i] - Low[i]);
   }
   if (i>1)
   {
   DiffBearsBuffer[i] = MathAbs(BearsBuffer[i] - BearsBuffer[i-1]);
   DiffBullsBuffer[i] = MathAbs(BullsBuffer[i] - BullsBuffer[i-1]);
          
   for (k=iniStart; k<=i; k++)
   {
   if (k == iniStart)
   {
   BearsMo = DiffBearsBuffer[k];
   BullsMo = DiffBullsBuffer[k];
   }
     
   if (DiffBearsBuffer[k] > DiffBearsBuffer[k-1])
   {
   BearsMo = BearsMo + DiffBearsBuffer[k];
   }
   else BearsMo = BearsMo - DiffBearsBuffer[k];
     
   if (DiffBullsBuffer[k] > DiffBullsBuffer[k-1])
   {
   BullsMo = BullsMo + DiffBullsBuffer[k];
   }
   else BullsMo = BullsMo + DiffBullsBuffer[k];
   }
   BearsMoBuffer[i] = BearsMo;
   BullsMoBuffer[i] = BullsMo;
   }
      }
//---
   return(0);
  }
//+------------------------------------------------------------------+
Files:
 
  1. int start()
    Start using the new style Event Handling Functions - Functions - Language Basics - MQL4 Reference and use strict.
  2. if(counted_bars > 0) 
           counted_bars--;
    limit = Bars - counted_bars;
    No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 forum
  3.      if (i<MoPeriod) break;
       iniStart = i - MoPeriod;
    
       if (i>1)
       {
       if (k == iniStart)
       {
       BearsMo = DiffBearsBuffer[k];
       BullsMo = DiffBullsBuffer[k];
       }
    Bogus attempt. Your look back for bearsBuffer is zero, your look back for the diff buffers should be bears look back plus MoPeriod. Do your lookbacks correctly. Use two separate loops. Get your initial values once you once you calculate your starting position.
  4.  if (DiffBearsBuffer[k] > DiffBearsBuffer[k-1])
    Never look at future values. The difference should be [k] - [k+MoPeriod] you're not using your parameter at all.
 
WHRoeder:
  1. Start using the new style Event Handling Functions - Functions - Language Basics - MQL4 Reference and use strict.
  2. No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 forum
  3. Bogus attempt. Your look back for bearsBuffer is zero, your look back for the diff buffers should be bears look back plus MoPeriod. Do your lookbacks correctly. Use two separate loops. Get your initial values once you once you calculate your starting position.
  4. Never look at future values. The difference should be [k] - [k+MoPeriod] you're not using your parameter at all.

Thank you so so much for your help. You were a great help.

It took a while but i think i understood what you were saying.

I'll attach the new code for review and list out the changes i carried out.

 - From what i understood of your explanation, i switched to OnCalculate and used ArraySetAsSeries.

- I majorly left things as is and made tiny changes. I also removed any increments or decrements.

- Finally, i decreased the prev_calculated instead of the rates_total.

I made other minute changes later on but the indicator works now. I deeply appreciate your help and knowledge.

Files: