//+------------------------------------------------------------------+
//|                                           CandleWicksDisplay.mq4 |
//|                                  Copyright © 2011, Andriy Moraru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Andriy Moraru"
#property link      "http://www.earnforex.com"

/*
   Alerts you when the candle's wick (shadow) reaches a certain length.
   Your e-mail settings are set in Tools -> Options -> Email.
   Also displays the candle wicks' length above and below the candles.
*/

// The indicator uses only objects for display, but the line below is required for it to work.
#property indicator_chart_window

extern int DisplayWickLimit = 20; // In standard pips
color DisplayBullColor = Gray;
color DisplayBearColor = LimeGreen;
extern int CSMinLength = 8; // Distance from High to Pip Count
extern int BearCandleAlert = 10; // In broker pips
extern int BullCandleAlert = 10; // In broker pips
extern int Font_size = 5;
extern bool WaitForClose = true; // Wait for a candle to close before checking wicks' length
extern bool EmailAlert = true;
extern bool SoundAlert = false;
extern bool VisualAlert = false;

// Time of the bar of the last alert
datetime AlertDone;

double Poin;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
	Poin = Point;
	//Checking for unconvetional Point digits number
   if ((Point == 0.00001) || (Point == 0.001)) Poin *= 10;
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   for (int i = 0; i < Bars; i++)
   {
      ObjectDelete("Red-" + TimeToStr(Time[i], TIME_DATE|TIME_MINUTES));
      ObjectDelete("Green-" + TimeToStr(Time[i], TIME_DATE|TIME_MINUTES));
   }
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   string name, length;
   bool DoAlert = false;
   int index = 0;
   
   if (WaitForClose) index = 1;
   
   int counted_bars = IndicatorCounted();
   if (counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   if (limit > 15000) limit = 15000;
     
   for (int i = 0; i <= limit; i++)
   {
      if (Open[i] <= Low[i])
      {
         
            name = "Red-" + TimeToStr(Time[i], TIME_DATE|TIME_MINUTES);
            length = DoubleToStr(MathAbs((Close[i] - Open[i]) / Poin), 0);
            if (ObjectFind(name) != -1) ObjectDelete(name);
            ObjectCreate(name, OBJ_TEXT, 0, Time[i], High[i] + CSMinLength * Poin);
            ObjectSetText(name, length, Font_size, "Verdana", DisplayBullColor);
        
    
      }
      else 
      {
         
            name = "Red-" + TimeToStr(Time[i], TIME_DATE|TIME_MINUTES);
            length = DoubleToStr(MathAbs((Close[i] - Open[i]) / Poin), 0);
            if (ObjectFind(name) != -1) ObjectDelete(name);
            ObjectCreate(name, OBJ_TEXT, 0, Time[i], High[i] + CSMinLength * Poin);
            ObjectSetText(name, length, Font_size, "Verdana", DisplayBullColor);
        
               
      }
   }
   
   if (AlertDone == Time[index]) return(0); // Already sent an alert for this candle
   
   if (Close[index] >= Open[index]) // Bullish candle
   {
      if ((High[index] - Close[index] >= BearCandleAlert * Point) || (Open[index] - Low[index] >= BullCandleAlert * Point)) DoAlert = true;
   }
   else // Bearish candle
   {
      if ((High[index] - Open[index] >= BearCandleAlert * Point) || (Close[index] - Low[index] >= BullCandleAlert * Point)) DoAlert = true;
   }
   
   if (DoAlert)
   {
      datetime tc = TimeCurrent();
      string time = TimeYear(tc) + "-" + TimeMonth(tc) + "-" + TimeDay(tc) + " " + TimeHour(tc) + ":" + TimeMinute(tc);
      if (VisualAlert) Alert(time + " - wick limit reached!");
      if (SoundAlert) PlaySound("alert.wav");
      if (EmailAlert) SendMail("CandleWick Alert", time + " - wick limit reached!");
      AlertDone = Time[index];
   }
      
   return(0);
}
//+------------------------------------------------------------------+