//--------------------------------
//Alert if Candle high wick is higher than BB upper and low wick is lower than BB 
//--------------------------------       


#property copyright "BlueRain"
#property link     ""

#property indicator_chart_window 
#property indicator_buffers 1

#property indicator_color1 Red


//--- indicator parameters
input int      BBPeriod=20;      // Bands Period
input double   BBDeviations=1.0; // Bands Deviations
input int      BBShift=0; 
                   
extern int     BBAppliedPrice=0;  

extern int     BarShift = 0;                    

extern bool     AlertOnOpenBar         = true;
extern bool     AlertOnClosedBar       = true;


static datetime previousAlertTime,previousPrintTime;
static string   previousAlertMessage;
static bool CurrentBarAlerted = false;
static datetime currentbarAlertTime;


//Arrow
static color MarkerColor = Red;

double ExtMapBuffer[]; //this buffer is used to store actual value



//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init() 
{ 
    //we need 4 buffer 
    //2 for line ( red/green)
    //2 for market ( red/green)
    IndicatorBuffers(0);  
    
    SetIndexBuffer(0, ExtMapBuffer); 
    ArraySetAsSeries(ExtMapBuffer, true); 
    SetIndexStyle(0,DRAW_ARROW,EMPTY,1,MarkerColor);
    SetIndexEmptyValue(0, -1);
    
   //circle
 //  SetIndexArrow(3,108);
 //  SetIndexArrow(4,108);
   
   //thumbs up
     SetIndexArrow(0,233);
       
    IndicatorShortName(WindowExpertName()); 
    
    //this is needed to make sure alert is happening on time frame only    
    previousAlertTime = Time[0];
    
    previousAlertMessage = "Init Alert Msg";
 
    
   
    return(0); 
} 

//+------------------------------------------------------------------+ 
//| Custor indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 
int deinit() 
{ 
    // ???? ????? ?????? ?????? 
    return(0); 
} 


//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 
int start() 
{ 

   int CountedBars=IndicatorCounted();  // added
   if (CountedBars<0) return(-1); // added
   if (CountedBars>0) CountedBars--;   // added
   
   static datetime timeCur; 
    datetime timePre = timeCur; 
    timeCur=Time[0];
    bool isNewBar = timeCur != timePre;

 
    
    string PriceInfoString = EMPTY_VALUE;
    
    double UpperB, MiddleB, LowerB; 
    
   
     
  for (int x = Bars - IndicatorCounted() - 1; x >= 0; x--)
    { 
       
       UpperB = iBands(_Symbol,Period(),BBPeriod,BBDeviations,BBShift,BBAppliedPrice,MODE_UPPER, x + BarShift);
       MiddleB = iBands(_Symbol,Period(),BBPeriod,BBDeviations,BBShift,BBAppliedPrice,MODE_MAIN, x + BarShift);
       LowerB = iBands(_Symbol,Period(),BBPeriod,BBDeviations,BBShift,BBAppliedPrice,MODE_LOWER, x + BarShift);
       double atr=iATR(NULL,0,100,x); 
       
       
       if ( (High[x] >= UpperB ) && (Low[x] <= LowerB ))
           {
             ExtMapBuffer[x] = Low[x] - atr/2;
           }
      
    
    } 

//alert



  
if ( AlertOnClosedBar && ( Time[0]>previousAlertTime ))
 {
       if (ExtMapBuffer[1] > 0)
          SimpleAlert(StringConcatenate(WindowExpertName(), _Symbol, " ", TF2Str(_Period), "Closed Candle length longer than BB: "));
 }
 
 if ( AlertOnOpenBar )
 {
 
   if(isNewBar){ 
        
         CurrentBarAlerted = false;
     }
          
    if (CurrentBarAlerted == false)
     {
       
       if (ExtMapBuffer[0] > 0) 
       {
          CurrentBarAlerted = true;
          SimpleAlert(StringConcatenate(WindowExpertName(), _Symbol, " ", TF2Str(_Period), "Open Candle length longer than BB: "));
          
        }
      }
 
 } 
 
 
   
   return(0);
   
 } 
    
 
  
  
void SimpleAlert(string doWhat)
{
  
   if (previousAlertTime != Time[0] || doWhat != previousAlertMessage)
   {
      previousAlertTime    = Time[0];
      previousAlertMessage =  doWhat;
      Alert(doWhat);
    //  SaveAlertToDisk(doWhat);
      
   }
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string TF2Str(int nperiod)
  {
   switch(nperiod)
     {
      case PERIOD_M1: return("M1");
      case PERIOD_M5: return("M5");
      case PERIOD_M15: return("M15");
      case PERIOD_M30: return("M30");
      case PERIOD_H1: return("H1");
      case PERIOD_H4: return("H4");
      case PERIOD_D1: return("D1");
      case PERIOD_W1: return("W1");
      case PERIOD_MN1: return("MN");
     }
   return(Period());
  }
//+------------------------------------------------------------------+
