//+------------------------------------------------------------------+
//|                                                    111112222.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

extern int CalculatBars = 200;
extern int SizeMaxBar   =1000;
extern int SizeMaxTeloBar   =1000;
extern int StartTime    =8; 
extern int EndTime      =23;  
double     b,h,l,s,t;
int i;
string comm="";
datetime calcStart;
int OnInit()
  {if(Bars<CalculatBars){Alert("!Недостачно баров в истории!");return(0);}
   if(Period()>=1440){StartTime=0;EndTime=0;}
   Comment("");
   calcStart =0;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

  
  if(time[0]>calcStart){
   b=0;h=0;l=0;s=0;t=0;comm="";
   if(Bars<CalculatBars){Alert("!Недостачно баров в истории!");return(0);}
   for (i=CalculatBars; i>=1; i--) {
    if(Period()<1440){
      if((StartTime!=0||EndTime!=0)&&TimeHour(time[i])<StartTime)continue;
      if((StartTime!=0||EndTime!=0)&&TimeHour(time[i])>=EndTime) continue;
      if(SizeMaxBar!=0&&(high[i]-low[i])/Point > SizeMaxBar)     continue;
      s+=(high[i]-low[i])/Point;
      if(SizeMaxTeloBar!=0&&MathAbs(open[i]-close[i])/Point >SizeMaxTeloBar)     continue;
      t+=MathAbs(open[i]-close[i])/Point;
      if (Open[i]>Close[i]) {
        h+=(High[i]-Open[i])/Point;
        l+=(Close[i]-Low[i])/Point;
      } else {
        h+=(High[i]-Close[i])/Point;
        l+=(Open[i]-Low[i])/Point;
      }
     }
     if(Period()>=1440){
       s+=(high[i]-low[i])/Point;
      if(SizeMaxTeloBar!=0&&MathAbs(open[i]-close[i])/Point >SizeMaxTeloBar)     continue;
      t+=MathAbs(open[i]-close[i])/Point;
      if (Open[i]>Close[i]) {
        h+=(High[i]-Open[i])/Point;
        l+=(Close[i]-Low[i])/Point;
      } else {
        h+=(High[i]-Close[i])/Point;
        l+=(Open[i]-Low[i])/Point;
      } } 
      b++;
    }
  calcStart = time[0];
  comm = comm + "Средняя волатильность: " + DoubleToString(s/b,0 )+ " п.\n";
  comm = comm + "Средний размер тела: " + DoubleToString(t/b,0) + " п.\n";
  comm = comm + "Средний размер верхней тени: " + DoubleToString(h/b,0) + " п.\n";
  comm = comm + "Средний размер нижней тени: " + DoubleToString(l/b,0) + " п.";

  Comment(comm);
 }  
  
   return(rates_total);
  }
void OnDeinit(const int reason)
  {

  Comment("");    
  }