//+------------------------------------------------------------------+
//|                                           Statist Toolbox v0.mq4 |
//|                                                  Rafaell (okfar) |
//|                                                            http: |
//+------------------------------------------------------------------+
#property copyright "Rafaell (okfar)"
#property link      "http:"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 SaddleBrown
#property indicator_color3 PapayaWhip
#property indicator_color4 Blue

#define  daySec 86400
#define  hourSec 3600  
#define  minSec 60  

//---- input parameters
extern int     timeAdj=0; 
extern bool    partOfDay = True;
extern string  startDate="2009.8.1";
extern string  endDate="0";
extern int     startHour=4;
extern int     endHour=12;
extern int     partOfDayPeriod = PERIOD_H1;
    

extern int       ExtParam1=1;
//---- buffers
datetime cCandleOT[];
datetime cCandleCT[];
double   cPrice[][4];
int      cTiFr;
int      numCBars;

datetime startDatedt,
         endDatedt;
         
int shifts,
    shifte;
string ind; 

int i; 

color colCBPoz = C'108,174,81';
color colCBNeg = C'189,66,66';

  
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   
//----
   ind = WindowExpertName();
   IndicatorShortName(ind); 
   
   
   startDatedt = StrToTime(startDate);
   
   if (endDate == " " || endDate == "0") {
      endDatedt = iTime(Symbol(), PERIOD_D1, 0)+daySec;
      if (TimeCurrent()>endDatedt+endHour*hourSec )
            endDatedt +=daySec;
//      endDate = TimeToStr(endDatedt, TIME_DATE|TIME_MINUTES);
   }
   else 
      endDatedt = StrToTime(endDate);
   
   startHour += timeAdj;
   endHour += timeAdj;
   if (endHour < startHour)
      endHour +=24;
   
   if (partOfDay) {
   //   if (endHour > startHour)
         cTiFr = (endHour-startHour)*60;
   /*   else if (endHour < startHour)
         cTiFr = (24+(endHour-startHour))*60;
      else 
         Alert("Wrong input (startHour: ",startHour," - endHour: ",endHour);
   */   
      
      numCBars = SetCCandeArray(startDatedt, endDatedt);
      
   }
      
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   for(i=0; i<numCBars; i++) {
      if (ObjectFind(ind+"oc"+i) >= 0)
         ObjectDelete(ind+"oc"+i);
      if (ObjectFind(ind+"hl"+i) >= 0)
         ObjectDelete(ind+"hl"+i);
   }
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----

   
   if ((endDate == " " || endDate == "0") || cCandleOT[0]==0) {
      if (endDatedt < iTime(Symbol(), PERIOD_D1, 0) && (endDate == " " || endDate == "0"))
         endDatedt = iTime(Symbol(), PERIOD_D1, 0)+daySec;
         
         if (TimeCurrent()>endDatedt+endHour*hourSec && (endDate == " " || endDate == "0"))
            endDatedt +=daySec;
        
         
      if (cCandleOT[0]+cTiFr*minSec < TimeCurrent()) {
         numCBars = SetCCandeArray(startDatedt, endDatedt );
         SetCustomCandles(startDatedt, endDatedt, partOfDayPeriod, numCBars-1);
      }
      else if (cCandleOT[1]+cTiFr*minSec+partOfDayPeriod*minSec < TimeCurrent())
         SetCustomCandles(startDatedt, endDatedt, partOfDayPeriod, 1);
      else if (cCandleOT[0]+cTiFr*minSec > TimeCurrent())
         SetCustomCandles(startDatedt, endDatedt, partOfDayPeriod, 0);
   }  
      
   Comment(CalculatePartStat(cPrice, numCBars));
  
  
  
  
      
  
//----
   return(0);
  }
//+------------------------------------------------------------------+

string CalculatePartStat(double price[][], int n)
{
   int   i,
         start,
         end;
   int   countR = 0,
         countAM = 0;
   double   avgRange=0,
            avgAbsMov=0; 
   
   
   if (endDate == " " || endDate == "0")
      start = 1;
   else
      start = 0;   
   
   for (i=start; i<n; i++) {
      if (price[i][PRICE_HIGH] > 0 && price[i][PRICE_LOW] > 0) {
         avgRange += (price[i][PRICE_HIGH]-price[i][PRICE_LOW]);
         countR++;
         end=i;
      }
      if (price[i][PRICE_OPEN] > 0 && price[i][PRICE_CLOSE] > 0) {
         avgAbsMov += MathAbs(price[i][PRICE_OPEN] - price[i][PRICE_CLOSE]);
         countAM++;
      }
   }
   avgRange /= (countR*Point);
   avgAbsMov /= (countAM*Point);
   
   
   return(StringConcatenate("Date: ", TimeToStr(cCandleOT[end], TIME_DATE), " - ", TimeToStr(cCandleCT[start], TIME_DATE),
         "\nFrom hour: ", startHour, " - ", endHour,
         "\nAverage range (high-low): ", DoubleToStr(avgRange, 0), " pips  (No.: ", countR, ")",
         "\nAverage abs. move (open-close): ", DoubleToStr(avgAbsMov, 0), " pips  (No.: ", countAM, ")"));
}            
      
//+------------------------------------------------------------------+      

void SetCustomCandles(datetime start, datetime end, int usedperiod, int numRecal)
{

   int n=0;
   int holDays=0;
   
   while (n<=numRecal+1) {   
//   for (i=0; i<numCBars; i++) {
      
       
      
      if (end+startHour*hourSec-(n+holDays)*daySec > TimeCurrent()) {
         if (end+endHour*hourSec-(n+holDays+1)*daySec < TimeCurrent()) {
            n++;
        //    continue;
/*            cCandleOT[0] = 0;
         cCandleCT[0] = 0;
         cPrice[0][PRICE_OPEN] =0;
         cPrice[0][PRICE_CLOSE] = 0;
         cPrice[0][PRICE_HIGH] = 0;
         cPrice[0][PRICE_LOW]=0;
        
*/         }
         else if (end+endHour*hourSec-(n+holDays)*daySec > TimeCurrent())
        holDays++;
         continue;
      }
      else if (end+startHour*hourSec-(n+holDays)*daySec > end) {
         holDays++;
         continue; 
      }  
      else if (end+startHour*hourSec-(n+holDays)*daySec < start)
         break;
      else   
         shifts = iBarShift(Symbol(), usedperiod, end+startHour*hourSec-(n+holDays)*daySec, true);
      
      if (end+endHour*hourSec-(n+holDays)*daySec > TimeCurrent() && end+startHour*hourSec-(n+holDays)*daySec < TimeCurrent())
         shifte = 0;

      else
         shifte = iBarShift(Symbol(), usedperiod, end+endHour*hourSec-usedperiod*minSec-(n+holDays)*daySec, true);
      
      if (shifts < 0 ) {
         if (shifte < 0) {
            holDays++;
            continue; 
         }
        else {
            while (end+startHour*hourSec-(n+holDays)*daySec > start && shifts < 0) {               
               holDays++;
               shifts = iBarShift(Symbol(), usedperiod, end+startHour*hourSec-(n+holDays)*daySec, true);
               
            }
            if (shifts<0 || end+startHour*hourSec-(n+holDays)*daySec < start)
               break;   
         }                    
      }

      cCandleOT[n] = iTime(Symbol(), usedperiod, shifts);
      cCandleCT[n] = iTime(Symbol(), usedperiod, shifte)+usedperiod*minSec;
      cPrice[n][PRICE_OPEN] = iOpen(Symbol(), usedperiod, shifts);
      cPrice[n][PRICE_CLOSE] = iClose(Symbol(), usedperiod, shifte);
      cPrice[n][PRICE_HIGH] = iHigh(Symbol(), usedperiod, iHighest(Symbol(), usedperiod, MODE_HIGH, shifts-shifte+1, shifte));
      cPrice[n][PRICE_LOW] = iLow(Symbol(), usedperiod, iLowest(Symbol(), usedperiod, MODE_LOW, shifts-shifte+1, shifte)); 
      
      if (cTiFr > 3*Period())
        int  dcand = cTiFr/3;
      
      DrawRec(ind+"hl"+n, cCandleOT[n]+dcand*minSec, cPrice[n][PRICE_HIGH], cCandleCT[n]-dcand*minSec, cPrice[n][PRICE_LOW], "High - Low", Red, STYLE_DASHDOT, False);
      DrawRec(ind+"oc"+n, cCandleOT[n], cPrice[n][PRICE_OPEN], cCandleCT[n], cPrice[n][PRICE_CLOSE], "Open - Close", CandleColorSet(cPrice[n][PRICE_OPEN],  cPrice[n][PRICE_CLOSE]), STYLE_SOLID, True);
      n++;
   }
}



void DrawRec(string LBL, datetime X1,  double Y1, datetime X2, double Y2, string descr, color col, int style, bool back)
{


int   show= 0x001F ;
      if (cTiFr > PERIOD_H4)
         show = show | 0x0020;
      if (cTiFr > PERIOD_D1)
         show = show | 0x0040;              
       
   
   ObjectCreate(LBL, OBJ_RECTANGLE, 0, X1 , Y1, X2, Y2);
   ObjectSet(LBL, OBJPROP_STYLE, style);
   ObjectSet(LBL, OBJPROP_BACK, back);
   ObjectSet(LBL, OBJPROP_WIDTH, 1);
   ObjectSet(LBL, OBJPROP_COLOR, col);
   ObjectSet(LBL, OBJPROP_TIMEFRAMES, show); 
   ObjectSetText(LBL, descr+" "+DoubleToStr(Y1,Digits)+" - "+DoubleToStr(Y2,Digits), 10);
 
} 


color CandleColorSet(double n1, double n2)
{
   if (n1 <= n2)
      return (colCBPoz);
   if (n1 > n2)
      return(colCBNeg);
}   



//+------------------------------------------------------------------+

int SetCCandeArray(datetime start, datetime end)
{
   int countBars;
   countBars = iBarShift(Symbol(), PERIOD_D1, start, false)+1;   
   if (TimeDayOfWeek(start) < 1 || TimeDayOfWeek(start) > 5)
      countBars--;
   
   if (endDate != " " || endDate != "0")
      countBars -= (iBarShift(Symbol(), PERIOD_D1, end, false));
   else 
      countBars++;   

   ArrayResize(cCandleOT, countBars);
   ArrayResize(cCandleCT, countBars);
   ArrayResize(cPrice, countBars);
   
   return(countBars);   
}

//+------------------------------------------------------------------+