

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 White
#property indicator_color2 DodgerBlue
#property indicator_style1 2
#property indicator_width1 1

extern int barsToProcess=200;
extern bool Show_Alert = true;

extern bool Display_Inside_Bar = true;
extern bool Alert_Inside_Bar = true;

double upArrow[];
double downArrow[];
string PatternText[5000];
double TodayOpenBuffer[];
extern int TimeZoneOfData= 7;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {

//---- indicators
   
   SetIndexStyle(0,DRAW_ARROW, EMPTY);
   SetIndexArrow(0,226);
   SetIndexBuffer(0, downArrow);
      
   SetIndexStyle(1,DRAW_ARROW, EMPTY);
   SetIndexArrow(1,225);
   SetIndexBuffer(1, upArrow);
      
   
	SetIndexStyle(0,DRAW_LINE);
	SetIndexBuffer(0,TodayOpenBuffer);
	SetIndexLabel(0,"Open");
	SetIndexEmptyValue(0,0.0);
	return(0);

   
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() {
   ObjectsDeleteAll(0, OBJ_TEXT);
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+


int start()
   {
   int counted_bars=IndicatorCounted(),
   limit;
   double Range, AvgRange;
   int counter, setalert;
   static datetime prevtime = 0;
   int shift;
   int shift1;
   int shift2;
   int shift3;
   string pattern, period;
   int setPattern = 0;
   int alert = 0;
   int arrowShift;
   int textShift;
   double O, O1, O2, C, C1, C2, L, L1, L2, H, H1, H2;
   
    if(counted_bars>0)
       counted_bars--;
       limit=Bars-counted_bars;
      
    if(limit>barsToProcess)
       limit=barsToProcess; 
   
     
   if(prevtime == Time[0]) {
      return(0);
   }
   prevtime = Time[0];
   
   switch (Period()) {
      case 1:
         period = "M1";
         break;
      case 5:
         period = "M5";
         break;
      case 15:
         period = "M15";
         break;
      case 30:
         period = "M30";
         break;      
      case 60:
         period = "H1";
         break;
      case 240:
         period = "H4";
         break;
      case 1440:
         period = "D1";
         break;
      case 10080:
         period = "W1";
         break;
      case 43200:
         period = "MN";
         break;
   }
          
   for (int j = 0; j < limit; j++) { 
         PatternText[j] = "pattern-" + j;
   }
   
   for (shift = 0; shift < limit; shift++) {
      
      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;
      
      O = Open[shift1];
      O1 = Open[shift2];
      O2 = Open[shift3];
      H = High[shift1];
      H1 = High[shift2];
      H2 = High[shift3];
      L = Low[shift1];
      L1 = Low[shift2];
      L2 = Low[shift3];
      C = Close[shift1];
      C1 = Close[shift2];
      C2 = Close[shift3];
     
     
     
      
      if ((H-L) <= 3.1) {
      if ((H1>=H)&&(L1<=L)) {
         if (Display_Inside_Bar == true) {   
            ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], Low[shift1] - Range*1.5);
            ObjectSetText(PatternText[shift], "IB", 9, "Times New Roman", Green);
            upArrow[shift1] = Low[shift1] - Range*0.5;
            
         }
         if (Alert_Inside_Bar == true && Show_Alert == true) {
            pattern="Inside Bar Pattern";
            setalert = 1;
         }
      }
      }
      if (setalert == 1 && shift == 0) {
         Alert(Symbol(), " ", period, " ", pattern);
          setalert = 0;
      }
   } 
   
  
   int lastbar;
   int counted_bars1= IndicatorCounted();
   
   if (counted_bars1>0) counted_bars1--;
   lastbar = Bars-counted_bars1;	
   DailyOpen(0,lastbar);
   
   return (0);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int DailyOpen(int offset, int lastbar)
{
   int shift;
   int tzdiffsec= TimeZoneOfData * 3600;
   double barsper30= 1.0*PERIOD_M30/Period();
   bool ShowDailyOpenLevel= True;
   // lastbar+= barsperday+2;  		 
   lastbar= MathMin(Bars-20*barsper30-1, lastbar);

	for(shift=lastbar;shift>=offset;shift--){
	  TodayOpenBuffer[shift]= 0;
     if (ShowDailyOpenLevel){
       if(TimeDay(Time[shift]-tzdiffsec) != TimeDay(Time[shift+1]-tzdiffsec)){      
         TodayOpenBuffer[shift]= Open[shift];         
         TodayOpenBuffer[shift+1]= 0;                                                          
       }
       else{
         TodayOpenBuffer[shift]= TodayOpenBuffer[shift+1];
       }
	  }
   }
   
      
   return(0);
}
//+------------------------------------------------------------------+