//+------------------------------------------------------------------+
//|                                                Candle length.mq4 |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  Red
#property  indicator_width1  3
#property  indicator_minimum  0

#include <hanover --- function header.mqh>

extern int      LookbackCandles        = 100;
extern bool     CandleBodyOnly         = true;
extern double   AlertPips              = 10;
extern int      AlertCandle            = 0;
extern bool     ChartAlert             = true;
extern string   EmailSubject           = "";

double     ind_buffer1[];
datetime   prev_time, alert_time;
string     ccy, sym, IndiName;
int        dig, tf, tmf;
double     spr, pnt, tickval, bidp, askp, minlot, lswap, sswap;

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  ArrayInitialize(ind_buffer1,EMPTY_VALUE);
  SetIndexBuffer(0,ind_buffer1);
  SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);
  SetIndexDrawBegin(0,0);
  IndicatorShortName("Candle length");

  sym     = Symbol();
  ccy     = Symbol();
  tmf     = Period();
  bidp    = MarketInfo(ccy,MODE_BID);
  askp    = MarketInfo(ccy,MODE_ASK);
  pnt     = MarketInfo(ccy,MODE_POINT);
  dig     = MarketInfo(ccy,MODE_DIGITS);
  spr     = MarketInfo(ccy,MODE_SPREAD);
  tickval = MarketInfo(ccy,MODE_TICKVALUE);
  minlot  = MarketInfo(ccy,MODE_MINLOT);
  lswap   = MarketInfo(ccy,MODE_SWAPLONG);
  sswap   = MarketInfo(ccy,MODE_SWAPSHORT);
  if (dig == 3 || dig == 5) {
    pnt     *= 10;
    spr     /= 10;
    tickval *= 10;
  }  
  
  prev_time  = -9999;
  alert_time = -9999;
  plot_ind();
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  plot_ind();
  return(0);
}

//+------------------------------------------------------------------+
int plot_ind()   {
//+------------------------------------------------------------------+
// If AlertCandle < 0, set pips = avg height of last -AlertCandle candles
  double pips = AlertPips;
  if (AlertCandle < 0)  {
    int cnt = -AlertCandle;
    double sum = 0;
    for (int i=1; i<=cnt; i++)  {
      if (CandleBodyOnly)
        sum += MathAbs(Open[i]-Close[i])/pnt;
      else
        sum += (High[i]-Low[i])/pnt;
    }
    pips = AlertPips * DivZero(sum,cnt);    
  }  
//  log(sum,cnt,AlertPips,pips);  
  for (i=0; i<=LookbackCandles; i++)  {
    if (CandleBodyOnly)
      ind_buffer1[i] = MathAbs(Open[i]-Close[i])/pnt;
    else
      ind_buffer1[i] = (High[i]-Low[i])/pnt;

    int AC = MathMax(AlertCandle,0); 
    if (i==AC && AlertPips > 0)  {
//      log(i,AlertCandle,AlertPips,ind_buffer1[AlertCandle],Time[0],alert_time);      
      if (Time[0] > alert_time)  {
        if (ind_buffer1[AC] >= pips)  {
          alert_time = Time[0];
          string alert_text = sym + "," + TFToStr(tmf) + NumberToStr(pips,"': Candle length exceeds 'TR5.1' pips'");
          if (ChartAlert)          Alert(alert_text);
          if (EmailSubject > "")   SendMail(EmailSubject, alert_text);
    } } }
  }  
  return(0);
}

#include <hanover --- extensible functions.mqh>


