Help with Engulfing code

 

Hi Everyone,

My idea is that: If there is a Down-bar, I will track the first Up-bar that has Close higher than the Open of the Down-bar.

Also, the first Up-bar must be within 5 bars from the Down-bar.

Could you please help me debug my code below?

Thank you for your help.

HHC

#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//--- buffers
double engulf[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexBuffer(0,engulf);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int    numberbars=Bars-counted_bars-1;
   
   int    i=numberbars;
   int    j;

   
while (i>0)
{
      if (Open[i]>Close[i])                     //if it is a Down-bar
      {
         
         for(j=i-1;j>=i-5;j--)
         {
               if (Open[j]>Close[j])            //If it is a Down-bar, skip
                  { i=j-1;
                    continue;}
               else                             //if it is a Up-bar, start checking
                  if (Close[j]>Open[j]) 
                        engulf[j]=High[j]+0.001;
                        i=j-1;
                        continue;
         }               
      } 
}  
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
hhcfx:

Hi Everyone,

My idea is that: If there is a Down-bar, I will track the first Up-bar that has Close higher than the Open of the Down-bar.

Also, the first Up-bar must be within 5 bars from the Down-bar.

Could you please help me debug my code below?

Thank you for your help.

HHC

It's not a good idea to search in the future. You have to start from an Upbar and then seek in the past if there is a down bar encountering your requirements.
Reason: