//+------------------------------------------------------------------+ 
//|   TRO_HL_HISTOGRAM                                               | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|   All my indicators should be considered donationware. That is   |
//|   you are free to use them for your personal use, and are        |
//|   under no obligation to pay for them. However, if you do find   |
//|   this or any of my other indicators help you with your trading  |
//|   then any Gift or Donation as a show of appreciation is         |
//|   gratefully accepted.                                           |
//|                                                                  |
//|   Gifts or Donations also keep me motivated in producing more    |
//|   great free indicators. :-)                                     |
//|                                                                  |
//|   PayPal - THERUMPLEDONE@GMAIL.COM                               |  
//+------------------------------------------------------------------+ 
 
#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpledone.com/" 

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2

#property  indicator_color1  Lime
#property  indicator_color2  Red

extern int myCorner= 0;
extern int x.offset= 530 ;//250
extern int y.offset= 0 ;//-160

extern bool Show.Histogram = true ; 
extern bool Show.PL = true ;

extern string myFont       = "Arial Bold" ;
extern int    myFontSize   = 20 ;

extern int       myLineWidth = 3;
extern int    AlertTime = 5 ;
extern color  AlertTimeColor = Magenta ;

extern int myPctHi = 66 ;
extern int myPctLo = 34 ;

extern color myPctHiColor = Red ; 
extern color myPctMidColor = DimGray ; 
extern color myPctLoColor = Blue ; 

double     H_Buffer[];
double     L_Buffer[];

double spread, Pct, point, range ;
double s1[];


string symbol, tChartPeriod,  tShortName, sSpread, sOpen, sPrice, sHigh, sLow, sPct ;  
int    digits2, digits, period, win, xInc, yInc, xPos, yPos  ; 
 
string   tObjName01 , tObjName02 , tObjName03, tObjName04, tObjName05, tObjName06, tObjName07, tObjName08, tObjName09 ; 
string   tObjName10 ;
 
color colorPct, colorTime, colorPrice, colorHigh, colorLow ; 
 

 
//+------------------------------------------------------------------+
int init()
  {


   period       = Period() ;  
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;   
   point        =  Point ;

   if(digits == 5 || digits == 3) { digits2 = 0 ; point = point * 10 ; } else { digits2 = 0 ; }

   tShortName = symbol + tChartPeriod  ; 


if(Show.Histogram)
{
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,myLineWidth);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,myLineWidth);
   SetLevelStyle(STYLE_DOT, 1, DimGray) ;
   SetLevelValue(1,50);
   SetLevelValue(2,20);   
   SetLevelValue(3,10);
   SetLevelValue(4,0);
   SetLevelValue(5,-10);
   SetLevelValue(6,-20);
   SetLevelValue(7,-50);  
   
}
else
{
   SetIndexStyle(0,DRAW_NONE);  
   SetIndexStyle(1,DRAW_NONE); 
}

   SetIndexBuffer(0,H_Buffer);
   SetIndexBuffer(1,L_Buffer);
   
   IndicatorShortName("HL_Histogram");
   
   SetIndexLabel(0,"HiOp");
   SetIndexLabel(1,"OpLo");

 

   tObjName01 =   tShortName   + "hl01";
   tObjName02 =   tShortName   + "hl02";
   tObjName03 =   tShortName   + "hl03";
   tObjName04 =   tShortName   + "hl04";
   tObjName05 =   tShortName   + "hl05";
   tObjName06 =   tShortName   + "hl06";
   tObjName07 =   tShortName   + "hl07";
   tObjName08 =   tShortName   + "hl08";
   tObjName09 =   tShortName   + "hl09";
   tObjName10 =   tShortName   + "hl10";
      
   IndicatorShortName(tShortName);  
   

   
   return(0);
  }


//+------------------------------------------------------------------+  
int deinit()
  {
   ObjectsDeleteAll(win);   
   return(0);
  }  
   
  
//+------------------------------------------------------------------+


int start()
  {
   
   win =    WindowFind(tShortName);
   ObjectsDeleteAll(win); 
           
   int limit;
   int counted_bars=IndicatorCounted();
 
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   for(int i=0; i<limit; i++)
   {
  
      H_Buffer[i] = (High[i] - Open[i]) / Point ;
      L_Buffer[i] = (Low[i]  - Open[i]) / Point ;

   }

if(Show.PL) { DoShowPL () ; }
   
   return(0);
  }
//+------------------------------------------------------------------+

void DoShowPL ()
{

   yInc = myFontSize + 5 ;
   yPos = 0 ;
   
 
   if(Close[0] > Open[0]) { colorPrice = Lime ; } else if(Close[0] < Open[0]) { colorPrice = Red ; }
   else { colorPrice = Yellow ; }
   
    
   sPrice = symbol + " " + DoubleToStr(Close[0], digits ) ;
   
   ObjectCreate(tObjName06, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName06, sPrice , myFontSize, myFont, colorPrice );  
   ObjectSet(tObjName06, OBJPROP_CORNER, myCorner);
   ObjectSet(tObjName06, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName06, OBJPROP_YDISTANCE, yPos+myFontSize+y.offset); //     
//+--------   

   RefreshRates();
   
   spread  = MarketInfo(symbol,MODE_SPREAD) ; 
   sSpread = "Spread " + DoubleToStr(spread,0 ) ;
   
   yPos = yPos + yInc ;
   
   ObjectCreate(tObjName02, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName02, sSpread , myFontSize, myFont, Orange );  
   ObjectSet(tObjName02, OBJPROP_CORNER, myCorner);
   ObjectSet(tObjName02, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName02, OBJPROP_YDISTANCE, yPos+myFontSize+y.offset); //  
//+--------
 
   string sHiOp = "Long Pips " + DoubleToStr(H_Buffer[0],0 ) ;

   yPos = yPos + yInc ;
      
   ObjectCreate(tObjName03, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName03, sHiOp , myFontSize, myFont, Lime );  
   ObjectSet(tObjName03, OBJPROP_CORNER, myCorner);
   ObjectSet(tObjName03, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName03, OBJPROP_YDISTANCE, yPos+myFontSize+y.offset); //  
//+--------


   string sLoOp = "Short Pips " + DoubleToStr(MathAbs(L_Buffer[0]),0 ) ;

   yPos = yPos + yInc ;
   
   ObjectCreate(tObjName04, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName04, sLoOp , myFontSize, myFont, Red );  
   ObjectSet(tObjName04, OBJPROP_CORNER, myCorner);
   ObjectSet(tObjName04, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName04, OBJPROP_YDISTANCE, (yPos+myFontSize)+y.offset); // 
//+--------
   
   double Current  = (Close[0]  - Open[0]) / point  ;
   string sCurrent = "Current " + DoubleToStr( Current ,0 ) ;

   yPos = yPos + yInc ;
   
   ObjectCreate(tObjName01, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName01, sCurrent , myFontSize, myFont, Orange );  
   ObjectSet(tObjName01, OBJPROP_CORNER, myCorner);
   ObjectSet(tObjName01, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName01, OBJPROP_YDISTANCE, (yPos+myFontSize)+y.offset); // 


   

//+--------

   yPos = yPos + yInc ;  
  
   range = High[0] - Low[0] ; 
  
   if( range != 0) { Pct = 100 * ( Close[0]  - Low[0] ) / range ; } else {Pct = 0 ;} 

   sPct = "Pct " + DoubleToStr(Pct, 0 ) + " %";

   if(Pct > myPctHi ) { colorPct = myPctHiColor ; } else  
   if(Pct < myPctLo ) { colorPct = myPctLoColor ; } else { colorPct = myPctMidColor ; }
        
   
   ObjectCreate(tObjName10, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName10, sPct , myFontSize, myFont, colorPct );  
   ObjectSet(tObjName10, OBJPROP_CORNER, myCorner);
   ObjectSet(tObjName10, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName10, OBJPROP_YDISTANCE, yPos+myFontSize+y.offset); //         
      

//+--------

   yPos = yPos + yInc ;   
   
   sOpen = "Open " + DoubleToStr(Open[0], digits ) ;
   
   ObjectCreate(tObjName05, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName05, sOpen , myFontSize, myFont, LightGray );  
   ObjectSet(tObjName05, OBJPROP_CORNER, myCorner);
   ObjectSet(tObjName05, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName05, OBJPROP_YDISTANCE, yPos+myFontSize+y.offset); //     





//+--------

   yPos = yPos + yInc ;  

   if(Close[0] == High[0]) { colorHigh = Lime ; } else  { colorHigh = Green ; }
       
   sHigh = "High " + DoubleToStr(High[0], digits ) ;
   
   ObjectCreate(tObjName07, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName07, sHigh , myFontSize, myFont, colorHigh );  
   ObjectSet(tObjName07, OBJPROP_CORNER, myCorner);
   ObjectSet(tObjName07, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName07, OBJPROP_YDISTANCE, yPos+myFontSize+y.offset); //   

//+--------

   yPos = yPos + yInc ;  

   if(Close[0] == Low[0]) { colorLow = Red ; } else  { colorLow = FireBrick ; }
       
   sLow = "Low " + DoubleToStr(Low[0], digits ) ;
   
   ObjectCreate(tObjName08, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName08, sLow , myFontSize, myFont, colorLow );  
   ObjectSet(tObjName08, OBJPROP_CORNER, myCorner);
   ObjectSet(tObjName08, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName08, OBJPROP_YDISTANCE, yPos+myFontSize+y.offset); //      
   
   
   
//+--------   

   yPos = yPos + yInc ;  
    
	double bi;
   int bm,bs,bk;
   
   bm=Time[0]+Period()*60-CurTime();
   bi=bm/60.0;
   bs=bm%60;
   bm=(bm-bm%60)/60;
   
   if(bm < AlertTime ) { colorTime = AlertTimeColor ; } else { colorTime = LightGray ; }
   
   string sTime = tChartPeriod + " End: " + bm + "m " + bs + "s" ;

   ObjectCreate(tObjName09, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName09, sTime , myFontSize, myFont, colorTime );  
   ObjectSet(tObjName09, OBJPROP_CORNER, myCorner);
   ObjectSet(tObjName09, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName09, OBJPROP_YDISTANCE, yPos+myFontSize+y.offset); //      
//+--------   	 
 
 
 
 
  }        
//+------------------------------------------------------------------+ 

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="MN";
   }
   return(tfs);
}
//+------------------------------------------------------------------+
  