//+------------------------------------------------------------------+ 
//|    _TRO_24Hour_Stats_OHMLC                                       | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |  
//|   therumpledone@gmail.com                                        |  
//+------------------------------------------------------------------+ 
// NOTE: THE CURRENT RANGE IS COUNTED IN THE AVERAGE CALCULATIONS
//+------------------------------------------------------------------+ 
#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpldone.com/" 

#property indicator_separate_window
//---- input parameters

extern int win             = 2;
extern int price.x.offset  = 0 ;//250
extern int price.y.offset  = 0 ;//-190



extern string myFont         = "Arial Bold" ;
extern int    myFontSize     = 9 ;
extern color  myCurrentColor = Yellow ;
extern color  myScoreColor   = White ; 
extern color  myAvgColor     = Orange ;


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

int    myPeriod     = 60 ;

double open_price, high_price, low_price, close_price ;

int   MaxBars         = 25;

int      totalLabels;
string   labelNames;
int      corner;
int      window; 
string   shortName ;


double aHigh[25] ; double bHigh[24] ;  //double OM[25] ;
double aOpen[25] ; double bOpen[24] ;
double aLow[25] ;  double bLow[24] ;
double aClose[25] ;double bClose[24] ;
double aMid[25] ; double bMid[24] ;
double CO[25] ;   double PM[25] ;
int PL ; // index to PL arrays 
int PPL;
//+------------------------------------------------------------------+

string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN1";
   }
   return(tfs);
}

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

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
  {
  
   corner     = 0;
   shortName  = MakeUniqueName("24Hr OHMLC ","");
   labelNames = shortName;
   IndicatorShortName(shortName);
   totalLabels     = 0;
      
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {


   while (totalLabels>0) { ObjectDelete(StringConcatenate(labelNames,totalLabels)); totalLabels--;}
  

   return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
 

  
  
  
   window      = WindowFind(shortName);
   int m,n;
   int i,k;
   double point = Point ;

string tTF = TimeFrameToString(myPeriod)  ;

      aHigh[24]  = 0 ; bHigh[24]  = 0 ;
      aOpen[24]  = 0 ; bOpen[24]  = 0 ;
      aLow[24]   = 0 ;  bLow[24]   = 0 ;
      aClose[24] = 0 ;  bClose[24] = 0 ;
      aMid[24]   = 0 ;   bMid[24]   = 0 ;
     CO[24] = 0 ; 
     PM[24] = 0 ;    //OM[24] = 0 ;  
   for( i=0; i < MaxBars  ; i++) 
   {

   
//     PL = TimeHour(Time[i]) ;
     PPL         = TimeHour(iTime(NULL,myPeriod,i-1) ) ;   

     PL           = TimeHour(iTime(NULL,myPeriod,i) ) ;           
        
     aOpen[PL]   = iOpen(NULL,myPeriod,PL);   bOpen[PPL]   = iOpen(NULL,myPeriod,PPL);  
     aHigh[PL]   = iHigh(NULL,myPeriod,PL);    bHigh[PPL]   = iHigh(NULL,myPeriod,PPL);
     aLow[PL]    = iLow(NULL,myPeriod,PL);    bLow[PPL]    = iLow(NULL,myPeriod,PPL); 
     aClose[PL]  = iClose(NULL,myPeriod,PL);  bClose[PPL]  = iClose(NULL,myPeriod,PPL);  
     CO[PL] = ( aClose[PL]- aOpen[PL] ) / point ;     PM[PL] = ( aMid[PL]- bMid[PPL] ) / point ;  
     aMid[PL]    = ( aHigh[PL]+ aLow[PL] ) * 0.5 ;    //OM[PL] = ( aMid[1]- aMid[24] ) / point ;  
      bMid[PPL]    = ( bHigh[PPL]+ bLow[PPL] ) * 0.5 ;  
      aHigh[24]  = aHigh[24]  + aHigh[PL] ; bHigh[24]  = bHigh[24]  + bHigh[PPL] ;  
      aOpen[24]  = aOpen[24]  + aOpen[PL] ; bOpen[24]  = bOpen[24]  + bOpen[PPL] ;
      aLow[24]   = aLow[24]   + aLow[PL] ;  bLow[24]   = bLow[24]   + bLow[PPL] ;
      aClose[24] = aClose[24] + aClose[PL] ; bClose[24] = bClose[24] + bClose[PPL] ;    
      aMid[24]   = aMid[24]   + aMid[PL] ;  bMid[24]   = bMid[24]   + bMid[PPL] ;
        CO[24] = CO[24] + MathAbs (CO[PL]) ;      PM[24] = PM[24] + MathAbs (PM[PL]) ;     //OM[24] = OM[24] + OM[PL] ;    
 } 
 

      aHigh[24]  = aHigh[24] / MaxBars ; bHigh[24]  = bHigh[24] / MaxBars ;
      aOpen[24]  = aOpen[24] / MaxBars ; bOpen[24]  = bOpen[24] / MaxBars ;
      aLow[24]   = aLow[24] / MaxBars ;  bLow[24]   = bLow[24] / MaxBars ;
      aClose[24] = aClose[24] / MaxBars ; bClose[24] = bClose[24] / MaxBars ;   
      aMid[24]   = aMid[24] / MaxBars ;    bMid[24]   = bMid[24] / MaxBars ; 
         CO[24] = CO[24] / MaxBars ;    PM[24] = PM[24] / MaxBars ;  //OM[24] = OM[24] / MaxBars ; 
//+------------------------------------------------------------------+ 

string tAlert = "BUY ZONE" ;
string tStop  = " " ;

//+----------- SCOREBOARD ---------------------------------+  

while (totalLabels>0) { ObjectDelete(StringConcatenate(labelNames,totalLabels)); totalLabels--;}
color tScoreColor;  
n=20 ;

setObject(next(), "M-PM " ,price.x.offset, price.y.offset+n*8    , myScoreColor ,"Arial Bold", 9);
setObject(next(), "+/- " ,price.x.offset, price.y.offset+n*7    , myScoreColor ,"Arial Bold", 15);
setObject(next(), "CLPOPEN " ,price.x.offset, price.y.offset+n*2    , myScoreColor ,"Arial Bold", 9);
setObject(next(), "OHMLC " ,price.x.offset, price.y.offset+n    , myScoreColor ,"Arial Bold", 9);
//setObject(next(), "Open  " ,price.x.offset, price.y.offset+n*2  , myScoreColor ,myFont, 9);
setObject(next(), "High  " ,price.x.offset, price.y.offset+n*3  , myScoreColor ,"Arial Bold", 9);
//setObject(next(), "Mid   " ,price.x.offset, price.y.offset+n*4  , myScoreColor,myFont, 9);
setObject(next(), "Low   " ,price.x.offset, price.y.offset+n*5  , myScoreColor ,"Arial Bold", 9);
//setObject(next(), "Close " ,price.x.offset, price.y.offset+n*6  , myScoreColor ,myFont, 9);
setObject(next(), "MidP   " ,price.x.offset, price.y.offset+n*4  , myScoreColor,"Arial Bold", 9);
setObject(next(), "Close " ,price.x.offset, price.y.offset+n*6  , myScoreColor ,"Arial Bold", 9);
for (i=1,m=70; i < 24.01; i++, m+=43){
int tTHP = TimeHour(iTime(NULL,myPeriod,i-1)) ;	
int tTH = TimeHour(iTime(NULL,myPeriod,i)) ;
int tTHM = TimeMinute(iTime(NULL,myPeriod,i)) ;
 
if( tTH == 0 ) { tScoreColor = myCurrentColor;} else {tScoreColor = myScoreColor ; }
//color OJ ;
setObject(next(), i+"00",price.x.offset+m, price.y.offset+n  , tScoreColor ,"Arial Bold", 9);
//setObject(next(), DoubleToStr(aOpen[tTH],Digits ) ,price.x.offset+m, price.y.offset+n*2 , tScoreColor ,myFont, myFontSize);           
setObject(next(), DoubleToStr(aHigh[tTH],Digits ) ,price.x.offset+m, price.y.offset+n*3 , tScoreColor ,myFont, myFontSize);           
//setObject(next(), DoubleToStr(aMid[tTH],Digits ) ,price.x.offset+m, price.y.offset+n*4 ,    ColorCO ,myFont, myFontSize);           
setObject(next(), DoubleToStr(aLow[tTH],Digits ) ,price.x.offset+m, price.y.offset+n*5 , tScoreColor ,myFont, myFontSize);           
color ColorCO ; color ColorOJ ;  color ColorOM ; 
color ColorCOS ; color ColorOC ;   //color ColorOK ;
if( aOpen[tTH] <  aClose[tTH] ) { ColorCOS = LimeGreen ; } else { ColorCOS = Crimson ; } 
if( bMid[tTHP] <  aMid[tTH] ) { ColorCO = LimeGreen ; } else { ColorCO = Crimson ; } 
if(aClose[tTH]>  bOpen[tTHP] ) { ColorOJ = LimeGreen ; } else { ColorOJ = Crimson ; }  
if( CO[tTH] > 0) { ColorOC = LimeGreen ; } else { ColorOC = Crimson ; } 
 if( PM[tTH] > 0) { ColorOM = LimeGreen ; } else { ColorOM = Crimson ; }  
   
setObject(next(), DoubleToStr(bMid[tTH],Digits ) ,price.x.offset+m, price.y.offset+n*4 , ColorCO ,myFont, 9);    
//setObject(next(), DoubleToStr(aClose[tTH],Digits ) ,price.x.offset+m, price.y.offset+n*6 , ColorCO,myFont, myFontSize);           
 setObject(next(), DoubleToStr(bClose[tTH],Digits ) ,price.x.offset+m, price.y.offset+n*6 , ColorCOS,myFont, 9);  
setObject(next(), DoubleToStr(bOpen[tTH],Digits ) ,price.x.offset+m, price.y.offset+n*2 , ColorOJ,myFont, 9);  
setObject(next(), "  "+DoubleToStr(CO[tTH],0 ) ,price.x.offset+m, price.y.offset+n*7 , ColorOC  ,myFont, myFontSize); 
 setObject(next(), "  "+DoubleToStr(PM[tTH],0 ) ,price.x.offset+m, price.y.offset+n*8 , ColorOM  ,myFont, myFontSize);         
 //  setObject(next(), "  "+DoubleToStr(OM[tTH],0 ) ,price.x.offset+m, price.y.offset+n*9 , ColorOK  ,myFont, myFontSize);     

   
} // for (i=0


//+------------------------------------------------------------------+ 
// AVERAGE PROCESSING
//+------------------------------------------------------------------+ 

setObject(next(), " Avg"  ,price.x.offset+m, price.y.offset+n  , myAvgColor ,"Arial Bold", 9);
setObject(next(), " "+DoubleToStr(aOpen[24],4 ) ,price.x.offset+m, price.y.offset+n*2 , myAvgColor ,"Arial Bold", 9);           
setObject(next(), " "+DoubleToStr(aHigh[24],4 ) ,price.x.offset+m, price.y.offset+n*3 , myAvgColor ,"Arial Bold", myFontSize);           
setObject(next(), " "+DoubleToStr(aMid[24],4 ) ,price.x.offset+m, price.y.offset+n*4 , myAvgColor ,"Arial Bold", 9);           
setObject(next(), " "+DoubleToStr(aLow[24],4 ) ,price.x.offset+m, price.y.offset+n*5 , myAvgColor ,"Arial Bold", myFontSize);           
setObject(next(), " "+DoubleToStr(aClose[24],4 ) ,price.x.offset+m, price.y.offset+n*6 , myAvgColor ,"Arial Bold", myFontSize);           
setObject(next(), " "+DoubleToStr(CO[24],3 ) ,price.x.offset+m, price.y.offset+n*7 , myAvgColor ,"Arial Bold", myFontSize); 
//setObject(next(), " "+DoubleToStr(PM[24],3 ),price.x.offset+m, price.y.offset+n*8 , myAvgColor ,"Arial Bold", 9);   
// setObject(next(), " "+DoubleToStr (OM[24],3 ),price.x.offset+m, price.y.offset+n*9 , myAvgColor ,"Arial Bold", 9);   

WindowRedraw();
  
//+------------------------------------------------------------------+ 
   return(0);
  }
  
//+------------------------------------------------------------------+
//+                                                                  +
//+------------------------------------------------------------------+

void setObject(string name,string text,int x,int y,color theColor, string font = "Arial",int size=10,int angle=0)
{
   string labelName = StringConcatenate(labelNames,name);

      if (ObjectFind(labelName) == -1)
          {
//             ObjectCreate(labelName,OBJ_LABEL,window,0,0);
             ObjectCreate(labelName,OBJ_LABEL,win,0,0);            
             
             ObjectSet(labelName,OBJPROP_CORNER,corner);
             if (angle != 0)
                  ObjectSet(labelName,OBJPROP_ANGLE,angle);
          }               
       ObjectSet(labelName,OBJPROP_XDISTANCE,x);
       ObjectSet(labelName,OBJPROP_YDISTANCE,y);
       ObjectSetText(labelName,text,size,font,theColor);
       
}

//+------------------------------------------------------------------+
//| Custom functions and procedures                                  |
//+------------------------------------------------------------------+

string next() { totalLabels++; return(totalLabels); }  

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
 
string MakeUniqueName(string first, string rest)
{
   string result = first+(MathRand()%1001)+rest;

   while (WindowFind(result)> 0)
          result = first+(MathRand()%1001)+rest;
   return(result);
}


