a few bars candle pattern

 
Hi to all. Have a little problem. I m not a programmer but i understand a little in metaeditor. I can modify or change values in some indicators and that usually be ok. No errors.
Problem in this indicator is that it shows candle pattern through all history of the chart. And i don't heed this, it can be a messy with all these lines channels etc... i would like to pattern show only in a few last candles or to be manually selected how many last candles to show.

I found some other pattern indicators with that function but i failed to implement that part of the code in this indicator. Wont work. So if anybody is in the mood to help i would appreciate this.

//+------------------------------------------------------------------+
//|             Pattern recognition indicator                        |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Lime

extern bool Show_Alert = true;


extern bool Display_Dark_Cloud_Cover = true;
extern bool Show_DarkCC_Alert = true;
extern color Color_DarkCC = White;
int Text_DarkCC = 8;

extern bool Display_Piercing_Line = true;
extern bool Show_Piercing_Line_Alert = true;
extern color Color_Piercing_Line = White;
int Text_Piercing_Line = 8;

extern bool Display_Bearish_Engulfing = false;
extern bool Show_Bearish_Engulfing_Alert = false;
extern color Color_Bearish_Engulfing = Red;
int Text_Bearish_Engulfing = 8;

extern bool Display_Bullish_Engulfing = false;
extern bool Show_Bullish_Engulfing_Alert = false;
extern color Color_Bullish_Engulfing = Aqua;
int Text_Bullish_Engulfing = 8;


//---- buffers
double upArrow[];
double downArrow[];



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {

//---- indicators
   
   SetIndexStyle(0,DRAW_ARROW, EMPTY);
   SetIndexArrow(0,234);
   SetIndexBuffer(0, downArrow);
      
   SetIndexStyle(1,DRAW_ARROW, EMPTY);
   SetIndexArrow(1,233);
   SetIndexBuffer(1, upArrow);
      
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() {
   {for(int iObj=ObjectsTotal()-1; iObj >= 0; iObj--){
      string   on = ObjectName(iObj);
      
      if(StringFind(on, "BullRev") == 0)  ObjectDelete(on);
      if(StringFind(on, "BearRev") == 0)  ObjectDelete(on);
      if(StringFind(on, "Engulf") == 0)  ObjectDelete(on);
      if(StringFind(on, "Engul") == 0)  ObjectDelete(on);
}  }
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start(){



   double Range, AvgRange;
   int counter, setalert;
   static datetime prevtime = 0;
   int shift;
   int shift1;
   int shift2;
   int shift3;
   int shift4;
   string pattern, period;
   int setPattern = 0;
   int alert = 0;
   int arrowShift;
   int textShift;
   double O, O1, O2, C, C1, C2, C3, L, L1, L2, L3, H, H1, H2, H3;
   double CL, CL1, CL2, BL, BLa, BL90, UW, LW,BodyHigh, BodyLow;
   BodyHigh = 0;
   BodyLow = 0;
   double Doji_Star_Ratio = 0;
   double Doji_MinLength = 0;
   double Star_MinLength = 0;
   int  Pointer_Offset = 0;         // The offset value for the arrow to be located off the candle high or low point.
   int  High_Offset = 0;            // The offset value added to the high arrow pointer for correct plotting of the pattern label.
   int  Offset_ShootStar = 0;       // The offset value of the shooting star above or below the pointer arrow.
   int  Offset_Hammer = 0;          // The offset value of the hammer above or below the pointer arrow.
   int  Offset_Doji = 0;            // The offset value of the doji above or below the pointer arrow.
   int  Offset_Star = 0;            // The offset value of the star above or below the pointer arrow.
   int  Offset_Piercing_Line = 0;   // The offset value of the piercing line above or below the pointer arrow.
   int  Offset_DarkCC = 0;          // The offset value of the dark cloud cover above or below the pointer arrow.
   int  Offset_Bullish_Engulfing = 0;
   int  Offset_Bearish_Engulfing = 0;
   int  CumOffset = 0;              // The counter value to be added to as more candle types are met.
   int  IncOffset = 0;              // The offset value that is added to a cummaltive offset value for each pass through the routine so any 
                                    // additional candle patterns that are also met, the label will print above the previous label. 
   double Piercing_Line_Ratio = 0;      
   int Piercing_Candle_Length = 0;  
   int Engulfing_Length = 0;
   double Candle_WickBody_Percent = 0;
   int CandleLength = 0;  
   
   if(prevtime == Time[0]) {
      return(0);
   }
   prevtime = Time[0];
   
   switch (Period()) {
      case 1:
         period = "M1";
         Doji_Star_Ratio = 0;
         Piercing_Line_Ratio = 0.5;
         Piercing_Candle_Length = 10;
         Engulfing_Length = 10;
         Candle_WickBody_Percent = 0.9;
         CandleLength = 12;  
         Pointer_Offset = 5;
         High_Offset = 15;
         Offset_Hammer = 5;
         Offset_ShootStar = 5;
         Offset_Doji = 5;
         Offset_Star = 5;
         Offset_Piercing_Line = 5;
         Offset_DarkCC = 1;
         Offset_Bearish_Engulfing = 5;
         Offset_Bullish_Engulfing = 5;
         Text_DarkCC = 8;
         Text_Piercing_Line = 8;
         Text_Bearish_Engulfing = 8;
         Text_Bullish_Engulfing = 8;
         IncOffset = 16;
         break;
      case 5:
         period = "M5";
         Doji_Star_Ratio = 0;
         Piercing_Line_Ratio = 0.5;
         Piercing_Candle_Length = 10;
         Engulfing_Length = 10;
         Candle_WickBody_Percent = 0.9;
         CandleLength = 12;
         Pointer_Offset = 20;
         High_Offset = 15;
         Offset_Hammer = 5;
         Offset_ShootStar = 5;
         Offset_Doji = 5;
         Offset_Star = 5;
         Offset_Piercing_Line = 20;
         Offset_DarkCC = 20;
         Offset_Bearish_Engulfing = 20;
         Offset_Bullish_Engulfing = 5;
         Text_DarkCC = 8;
         Text_Piercing_Line = 8;
         Text_Bearish_Engulfing = 8;
         Text_Bullish_Engulfing = 8;
         IncOffset = 16;
         break;
      case 15:
         period = "M15";
         Doji_Star_Ratio = 0;
         Piercing_Line_Ratio = 0.5;
         Piercing_Candle_Length = 10;
         Engulfing_Length = 0;
         Candle_WickBody_Percent = 0.9;
         CandleLength = 12;
         Pointer_Offset = 25;
         High_Offset = 15;
         Offset_Hammer = 5;
         Offset_ShootStar = 5;
         Offset_Doji = 5;
         Offset_Star = 5;
         Offset_Piercing_Line = 25;
         Offset_DarkCC = 40;
         Offset_Bearish_Engulfing = 20;
         Offset_Bullish_Engulfing = 5;
         Text_DarkCC = 8;
         Text_Piercing_Line = 8;
         Text_Bearish_Engulfing = 8;
         Text_Bullish_Engulfing = 8;
         IncOffset = 16;
         break;
      case 30:
         period = "M30";
         Doji_Star_Ratio = 0;
         Piercing_Line_Ratio = 0.5;
         Piercing_Candle_Length = 10;
         Engulfing_Length = 15;
         Candle_WickBody_Percent = 0.9;
         CandleLength = 12;
         Pointer_Offset = 30;
         High_Offset = 15;
         Offset_Hammer = 5;
         Offset_ShootStar = 5;
         Offset_Doji = 5;
         Offset_Star = 5;
         Offset_Piercing_Line = 25;
         Offset_DarkCC = 50;
         Offset_Bearish_Engulfing = 50;
         Offset_Bullish_Engulfing = 25;
         Text_DarkCC = 8;
         Text_Piercing_Line = 8;
         Text_Bearish_Engulfing = 8;
         Text_Bullish_Engulfing = 8;
         IncOffset = 16;
         break;      
      case 60:
         period = "H1";
         Doji_Star_Ratio = 0;
         Piercing_Line_Ratio = 0.5;
         Piercing_Candle_Length = 10;
         Engulfing_Length = 25;
         Candle_WickBody_Percent = 0.9;
         CandleLength = 12;
         Pointer_Offset = 50;
         High_Offset = 20;
         Offset_Hammer = 8;
         Offset_ShootStar = 8;
         Offset_Doji = 8;
         Offset_Star = 8;
         Offset_Piercing_Line = 45;
         Offset_DarkCC = 60;
         Offset_Bearish_Engulfing = 60;
         Offset_Bullish_Engulfing = 45;
         Text_DarkCC = 8;
         Text_Piercing_Line = 8;
         Text_Bearish_Engulfing = 8;
         Text_Bullish_Engulfing = 8;
         IncOffset = 18;
         break;
      case 240:
         period = "H4";
         Doji_Star_Ratio = 0;
         Piercing_Line_Ratio = 0.5;
         Piercing_Candle_Length = 10;
         Engulfing_Length = 20;
         Candle_WickBody_Percent = 0.9;
         CandleLength = 12;
         Pointer_Offset = 100;
         High_Offset = 40;
         Offset_Hammer = 10;
         Offset_ShootStar = 10;
         Offset_Doji = 10;
         Offset_Star = 10;
         Offset_Piercing_Line = 70;
         Offset_DarkCC = 150;
         Offset_Bearish_Engulfing = 140;
         Offset_Bullish_Engulfing = 70;
         Text_DarkCC = 8;
         Text_Piercing_Line = 8;
         Text_Bearish_Engulfing = 8;
         Text_Bullish_Engulfing = 8;
         IncOffset = 25;
         break;
      case 1440:
         period = "D1";
         Doji_Star_Ratio = 0;
         Piercing_Line_Ratio = 0.5;
         Piercing_Candle_Length = 10;
         Engulfing_Length = 30;
         Candle_WickBody_Percent = 0.9;
         CandleLength = 12;
         Pointer_Offset =320;
         High_Offset = 80;
         Offset_Hammer = 15;
         Offset_ShootStar = 15;
         Offset_Doji = 15;
         Offset_Star = 15;
         Offset_Piercing_Line = 150;
         Offset_DarkCC = 250;
         Offset_Bearish_Engulfing = 300;
         Offset_Bullish_Engulfing = 200;
         Text_DarkCC = 8;
         Text_Piercing_Line = 8;
         Text_Bearish_Engulfing = 8;
         Text_Bullish_Engulfing = 8;
         IncOffset = 60;
         break;
      case 10080:
         period = "W1";
         Doji_Star_Ratio = 0;
         Piercing_Line_Ratio = 0.5;
         Piercing_Candle_Length = 10;
         Engulfing_Length = 40;
         Candle_WickBody_Percent = 0.9;
         CandleLength = 12;
         Pointer_Offset = 350;
         High_Offset = 35;
         Offset_Hammer = 20;
         Offset_ShootStar = 20;
         Offset_Doji = 20;
         Offset_Star = 20;
         Offset_Piercing_Line = 400;
         Offset_DarkCC = 900;
         Offset_Bearish_Engulfing = 1000;
         Offset_Bullish_Engulfing = 400;
         Text_DarkCC = 8;
         Text_Piercing_Line = 8;
         Text_Bearish_Engulfing = 8;
         Text_Bullish_Engulfing = 8;
         IncOffset = 35;
         break;
      case 43200:
         period = "MN";
         Doji_Star_Ratio = 0;
         Piercing_Line_Ratio = 0.5;
         Piercing_Candle_Length = 10;
         Engulfing_Length = 50;
         Candle_WickBody_Percent = 0.9;
         CandleLength = 12;
         Pointer_Offset = 1000;
         High_Offset = 45;
         Offset_Hammer = 30;
         Offset_ShootStar = 30;
         Offset_Doji = 30;
         Offset_Star = 30;
         Offset_Piercing_Line = 3300;
         Offset_DarkCC = 4300;
         Offset_Bearish_Engulfing = 1300;
         Offset_Bullish_Engulfing = 300;
         Text_DarkCC = 8;
         Text_Piercing_Line = 8;
         Text_Bearish_Engulfing = 8;
         Text_Bullish_Engulfing = 8;
         IncOffset = 45;
         break;
   }
   
   for (shift = 0; shift < Bars; shift++) {
      
      CumOffset = 0;
      setalert = 0;
      counter=shift;
      Range=0;
      AvgRange=0;
      for (counter=shift ;counter<=shift+9;counter++) {
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10;
      shift1 = shift + 1;
      shift2 = shift + 2;
      shift3 = shift + 3;
      shift4 = shift + 4;
      
      
      O = Open[shift1];
      O1 = Open[shift2];
      O2 = Open[shift3];
      H = High[shift1];
      H1 = High[shift2];
      H2 = High[shift3];
      H3 = High[shift4];
      L = Low[shift1];
      L1 = Low[shift2];
      L2 = Low[shift3];
      L3 = Low[shift4];
      C = Close[shift1];
      C1 = Close[shift2];
      C2 = Close[shift3];
      C3 = Close[shift4];
      if (O>C) {
         BodyHigh = O;
         BodyLow = C;  }
      else {
         BodyHigh = C;
         BodyLow = O; }
      CL = High[shift1]-Low[shift1];
      CL1 = High[shift2]-Low[shift2];
      CL2 = High[shift3]-Low[shift3];
      BL = Open[shift1]-Close[shift1];
      UW = High[shift1]-BodyHigh;
      LW = BodyLow-Low[shift1];
      BLa = MathAbs(BL);
      BL90 = BLa*Candle_WickBody_Percent;
            
         
 // Bearish Patterns  
  
     
      // Check for a Dark Cloud Cover pattern
      if ((C1>O1)&&(O>C)&&((H+L)/2>C)&&(L1>L2)&&(H1>H2)&&(L>L1)&&(H>H1)&&((CL>=Piercing_Candle_Length*Point))) {
         if (Display_Dark_Cloud_Cover == true) {   
            ObjectCreate(GetName("BearRev",shift), OBJ_TEXT, 0, Time[shift1], High[shift1] + (Pointer_Offset+Offset_DarkCC+High_Offset+CumOffset)*Point);
            ObjectSetText(GetName("BearRev",shift), "BearRev", Text_DarkCC, "Times New Roman", Color_DarkCC);
            CumOffset = CumOffset+IncOffset;
            downArrow[shift1] = High[shift1] + (Pointer_Offset*Point);
         }
         if (Show_DarkCC_Alert) {
         if (setalert == 0 && Show_Alert == true) {
            pattern="Bearish Reversal";
            setalert = 1;
         }
         }
      }

      // Check for Bearish Engulfing pattern
      if ((C1>O1)&&(O>C)&&(O>=C1)&&(O1>=C)&&((O-C)>(C1-O1))&&(CL>=(Engulfing_Length*Point))) {
         if (Display_Bearish_Engulfing == true) {
            ObjectCreate(GetName("Engulf",shift), OBJ_TEXT, 0, Time[shift1], High[shift1] + (Pointer_Offset+Offset_Bearish_Engulfing+High_Offset+CumOffset)*Point);
            ObjectSetText(GetName("Engulf",shift), "Engulf", Text_Bearish_Engulfing, "Times New Roman", Color_Bearish_Engulfing);
            CumOffset = CumOffset+IncOffset;
            downArrow[shift1] = High[shift1] + (Pointer_Offset*Point);
         }
         if (Show_Bearish_Engulfing_Alert) {
         if (setalert == 0 && Show_Alert == true) {
            pattern = "Bearish Engulfing";
            setalert = 1;
         }
         }
      }
 
 // End of Bearish Patterns
   
 // Bullish Patterns
   
  
      // Check for Piercing Line pattern
      
      if ((C1<O1)&&(O<C)&&(L1<L2)&&(H1<H2)&&(L<L1)&&(H<H1)&&(((H+L)/2)<C)&&(CL>=(Piercing_Candle_Length*Point))) {
         if (Display_Piercing_Line == true) {   
            ObjectCreate(GetName("BullRev",shift), OBJ_TEXT, 0, Time[shift1], Low[shift1] - (Pointer_Offset+Offset_Piercing_Line+CumOffset)*Point);
            ObjectSetText(GetName("BullRev",shift), "BullRev", Text_Piercing_Line, "Times New Roman", Color_Piercing_Line);
            CumOffset = CumOffset+IncOffset;
            upArrow[shift1] = Low[shift1] - (Pointer_Offset*Point);
         }
         if (Show_Piercing_Line_Alert) {
         if (shift == 0 && Show_Alert == true) {
            pattern="Bullish Reversal";
            setalert = 1;
         }
         }
      }   

      // Check for Bullish Engulfing pattern
      
      if ((O1>C1)&&(C>O)&&(C>=O1)&&(C1>=O)&&((C-O)>(O1-C1))&&(CL>=(Engulfing_Length*Point))) {
         if (Display_Bullish_Engulfing) {   
            ObjectCreate(GetName("Engul",shift), OBJ_TEXT, 0, Time[shift1], Low[shift1] - (Pointer_Offset+Offset_Bullish_Engulfing+CumOffset)*Point);
            ObjectSetText(GetName("Engul",shift), "Engul", Text_Bullish_Engulfing, "Times New Roman", Color_Bullish_Engulfing);
            CumOffset = CumOffset+IncOffset;
            upArrow[shift1] = Low[shift1] - (Pointer_Offset*Point);
         }
         if (Show_Bullish_Engulfing_Alert) {
         if (shift == 0 && Show_Alert == true) {
            pattern="Bullish Engulfing";
            setalert = 1;
         }
         }
      }
      
 // End of Bullish Patterns          
      if (setalert == 1 && shift == 0) {
         Alert(Symbol(), " ", period, " ", pattern);
         setalert = 0;
      }
      CumOffset = 0;
   } // End of for loop
     
   
      
   return(0);
}
//+------------------------------------------------------------------+

string GetName(string aName,int shift)
{
  return(aName+DoubleToStr(Time[shift],0));
}
 

Don't use Bars

for (shift = 0; shift < Bars; shift++) 

Us a number that reflects how many bars you want to calculate for

 
Yeah man thanx very much. Working!

Is there a code which after entering in metaeditor, could manage numbers of bars in indicator like on the picture? Because i would put that indi on forum, and as you know every person is different. Someone want 100 bars with indicator, others would only a few bars. And many people are afraid of using or don't know how to use metaeditor at all.

Picture is from other indicator.



 

I would use a name like "Calculated_Bars"

But if you use "step"

for (shift = 0; shift < Bars; shift++) 
//change to
for (shift = 0; shift < step; shift++) 
 

Yes, i change to - Calculated_Bars, - it's better that way. Work like a charm.

Again, thanks! You help me a lot. A big friendly hug 4u bro.

Reason: