Direction of High and Low prices - no zeros allowed - page 2

 
Alain Verleyen #:

Your are building your indicator buffers in the wrong direction :

You should go from past to present, not the reverse.

Your "previous" is from the future.

You are right. It referred to the future. I didn't notice. Thank you!
I changed the code using i-- in the loop.
It seems to return correct values, but now the problem is that it does not display past values. It only works on prices received after I load the indicator.
Is there a problem with the loop, or the limit maybe?

#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  White
#property indicator_width1  3
#property indicator_style1  3
#property indicator_color2  Magenta
#property indicator_width2  2
#property indicator_style2  3


  
//-------------------------------------------------------------------
//--- indicator buffers
double  HiDir[], LoDir[];
int DirHigh_0 = 0, DirHigh_1 = 0, DirLow_0, DirLow_1;
double H1, H0, H2, L0, L1, L2;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,HiDir);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,LoDir);
   SetIndexStyle(1,DRAW_HISTOGRAM);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration 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[])
  {
//---
 int counted_bars=IndicatorCounted(),
       limit;
 
   if(counted_bars>0)
      counted_bars--;
   
   limit=Bars-counted_bars;
  
   for(int i=limit-2; i==0 ;i--)
   {
   
     H0 = High[i];
     H1 = High[i+1];
     H2 = High[i+2];
     L0 = Low[i];
     L1 = Low[i+1];
     L2 = High[i+2];
   
   if (H0>H1) DirHigh_0 = 1; 
   else if (H0<H1) DirHigh_0 = -1; 
   else DirHigh_0 = 0;
   if (H1>H2) DirHigh_1 = 1; 
   else if (H1<H2) DirHigh_1 = -1; 
   else DirHigh_1 = 0;
   
   if(H0>H1) HiDir[i] = 1;
   else if (H0<H1) HiDir[i] = -1;
   if (DirHigh_0 == 0 && DirHigh_1 != 0) {HiDir[i] = DirHigh_1; DirHigh_0 = DirHigh_1;}
   
   if (L0>L1) DirLow_0 = 1; 
   else if (L0<L1) DirLow_0 = -1; 
   else DirLow_0 = 0;
   if (L1>L2) DirLow_1 = 1; 
   else if (L1<L2) DirLow_1 = -1; 
   else DirLow_1 = 0;
   
   if(L0>L1) LoDir[i] = 1;
   else if (L0<L1) LoDir[i] = -1;
   if (DirLow_0 == 0 && DirLow_1 != 0) {LoDir[i] = DirLow_1; DirLow_0 = DirLow_1;}
   }
   

//--- return value of prev_calculated for next call
   return(rates_total);
  }
   
   
   
 
BBad #:
You are right. It referred to the future. I didn't notice. Thank you!
I changed the code using i-- in the loop.
It seems to return correct values, but now the problem is that it does not display past values. It only works on prices received after I load the indicator.
Is there a problem with the loop, or the limit maybe?

Of course, don't use IndicatorCounted() it's obsolete, or at least use it correctly. Also your loop condition i==0 isn't correct.

   int limit = prev_calculated==0 ? rates_total-2 : rates_total-prev_calculated;
  
   for(int i=limit; i>=0 ;i--)
 
Alain Verleyen #:
for(int i=limit; i>=0 ;i--)
Your suggested code works fine, with rates_total-3.
With  rates_total-2 it displays nothing.
I don't really understand it. I must return to the basics of MQL4 I think.
Thank you very much.
 
BBad #:
Your suggested code works fine, with rates_total-3.
With  rates_total-2 it displays nothing.
I don't really understand it. I must return to the basics of MQL4 I think.
Thank you very much.
-3 because you are using this
   L2 = High[i+2];

Which I didn't notice previously.

 
Alain Verleyen #:
-3 because you are using this

Which I didn't notice previously.

Thank you for helping out on this. I wasn't sure about the direction of the data and couldn't check.
 
Alain Verleyen #:
-3 because you are using this

Which I didn't notice previously.

Yes I understand. Thank you.
Reason: