//+------------------------------------------------------------------+
//|                                                                  |
//|                     Bid Line Price & Timer.mq4                   |
//|                                                                  | 
//+------------------------------------------------------------------+

#property copyright "Copyright © 2009 traderathome"
#property link      "email:   traderathome@msn.com"

/*--------------------------------------------------------------------
     This indicator places 3 labels in the vacinity of the bid line:
a horizontal LineLabel representing the bid level, a PriceLabel based 
on the bid price and a TimerLabel showing the remaining time to go for 
the current candle.  

     All labels can be shifted to the right of the current candle
until context begins to disappear.  They can be shifted above or below
the actual bid line.  The LineLabel can be made thicker by increasing
the font size, and it can be made shorter or longer.  The text of the 
PriceLabel and TimerLabel can be of the font style and size of your 
choosing.  And color selection is available for each label.  The
PriceLabel offers 3 colors for:  when price has not changed, has just
increased, or has just decreased. 
 
     If the chart scale is not fixed 1 to 1, and you open the MT4
Terminal window below the chart, the labels will shift down slightly.
Their position becomes lower than intended by the settings.  In most
cases this shift down will be minor and compensated for in advance by 
the settings you select. 
    
     This indicator can be used for both forex and non-forex items,
such as stocks and gold.  Credit goes to Domas4 for coding this into
the price label.  
----------------------------------------------------------------------
Suggested Settings:        White Chart       Black Chart
                    
LineLabel_Color            Black             WhiteSmoke               
LineLabel_FontSize         16                10
LineLabel_Length           08                13
LineLabel_ShiftRight       21                31
LineLabel_ShiftUpDown      14                10
PriceLabel Static          Blue              DodgerBlue
PriceLabel Up              ForestGreen       LimeGreen
PriceLabel Down            Crimson           Red 
PriceLabel_FontStyle       Arial Bold        Arial 
PriceLabel_FontSize        16                16
PriceLabel_ShiftRight      12                12
PriceLabel_ShiftUpDown     15                15
TimerLabel_Color           Blue              DarkOrange  
TimerLabel_FontStyle       Arial Bold        Arial
TimerLabel_FontSize        11                11
TimerLabel_ShiftRight      35                35
TimerLabel_ShiftUpDown     -6                -6
--------------------------------------------------------------------*/
#property indicator_chart_window
#property  indicator_buffers 1
#property  indicator_color1  CLR_NONE

extern bool   Indicator_On                   = true;
extern string _                              = "";
extern string Part1                          = "General Inputs:";
extern bool   Use_For_Forex                  = true;
extern bool   Show_LineLabel                 = true;
extern bool   Show_PriceLabel                = true;
extern bool   Show_TimerLabel                = true;
extern string __                             = "";
extern string Part2                          = "LineLabel Settings:";
extern color  LineLabel_Color                = Black;
extern int    LineLabel_FontSize             = 16; 
extern int    LineLabel_Length               = 08; // 06, 08 and 11
extern int    LineLabel_ShiftRight           = 21; // 15, 20 and 25
extern int    LineLabel_ShiftUpDown          = 14; 
extern string ___                            = "";
extern string Part3                          = "PriceLabel Settings:";
extern color  PriceLabel_StaticColor         = Blue;
extern color  PriceLabel_UPColor             = ForestGreen;
extern color  PriceLabel_DOWNColor           = Crimson;
extern string PriceLabel_FontStyle           = "Arial Bold";
extern int    PriceLabel_FontSize            = 16; 
extern int    PriceLabel_ShiftRight          = 12; // 03, 12 and 21
extern int    PriceLabel_ShiftUpDown         = 15; 
extern string ____                           = "";
extern string Part4                          = "TimerLabel Settings:";
extern color  TimerLabel_Color               = Blue;
extern string TimerLabel_FontStyle           = "Arial Bold";
extern int    TimerLabel_FontSize            = 11;
extern int    TimerLabel_ShiftRight          = 35; // 15, 35 and 48 
extern int    TimerLabel_ShiftUpDown         = -6;

int      fxDigits;
double   Old_Price, scale;
string   Price;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
   {
   if (Use_For_Forex)  {string sub=StringSubstr(Symbol(), 3, 3);
   if (sub == "JPY") {fxDigits = 2;} else {fxDigits = 4;}}
   return(0);
   }
//+------------------------------------------------------------------+
//| Custom indicator de-initialization function                      |
//+------------------------------------------------------------------+
int deinit()
   {  
   int obj_total= ObjectsTotal();  
   for (int k= obj_total; k>=0; k--) {
      string name= ObjectName(k);
      if (StringSubstr(name,0,8)=="[BidLine") {ObjectDelete(name);}}    
   }  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
   {	
   //Clear chart, turn off or proceed 
   deinit();if (Indicator_On == false) {return(0);}  
   scale = WindowPriceMax() - WindowPriceMin();	
   
   if(Show_LineLabel){
      //Get x,y co-ordinates for BidLine
      double xb = Time[0];
      double yb = Bid + ((scale/400) * LineLabel_ShiftUpDown);
      //Prepare text for BidLine
      string pointer = "[BidLine] Pointer";
      string Tab0; for(int i=1; i<=LineLabel_Length; i++) {Tab0 = Tab0 + "_";}  
      string Tab1; for( i=1; i<=LineLabel_ShiftRight; i++) {Tab1 = Tab1 + " ";} 
      string Text1 = Tab1 + Tab0;  //0,8 and 7,10  
      //Construct/move BidLine       
      if(ObjectFind(pointer) != 0){
         ObjectCreate(pointer, OBJ_TEXT, 0, xb, yb);
         //ObjectSetText(pointer, Text1, PriceLabel_FontSize, PriceLabel_FontStyle, LineLabel_Color);}
         ObjectSetText(pointer, Text1, LineLabel_FontSize, "Arial", LineLabel_Color);}
      else {ObjectMove(pointer, 0, xb, yb);}}
      
   if(Show_PriceLabel){
      //Get x,y co-ordinates for PriceLabel
      double xp = Time[0];
      double yp = Bid + ((scale/400) * PriceLabel_ShiftUpDown);    	
      //Assign the Price & Colors
      int Color = PriceLabel_StaticColor; 
      if (Bid > Old_Price) {Color = PriceLabel_UPColor;}
      if (Bid < Old_Price) {Color = PriceLabel_DOWNColor;}
      Old_Price=Bid;    
      //Define the standard digits used for Price
      if (Use_For_Forex) {Price=DoubleToStr(Bid, fxDigits);}
      else {Price=DoubleToStr(Bid, Digits);}  
      //Prepare text for PriceLabel 
      string price = "[BidLine] Price";
      string Tab2; for(i=1; i<=PriceLabel_ShiftRight; i++) {Tab2 = Tab2 + " ";} 
      string Text2 = Tab2 + "             " + Price; //15       
      //Construct/move PriceLabel                     
      if(ObjectFind(price) != 0){
         ObjectCreate(price, OBJ_TEXT, 0, xp, yp);
         ObjectSetText(price, Text2, PriceLabel_FontSize, PriceLabel_FontStyle, Color);}
      else {ObjectMove(price, 0, xp, yp);}}   
   
   if (Show_TimerLabel){
      //Get time (H/M/S) remaining on current candle		
   	int h,m,s,t;    
      t=Time[0]+(Period()*60)-CurTime();
      s=t%60; string seconds = (s); if (s<10) {seconds = ("0"+seconds);} 
      m=(t-t%60)/60;   
      for(i=0; i<24; i++){
         if(m>=60){m=m-60;h=h+1;}
         string minutes = (m); if (m<10) {minutes = ("0"+minutes);}   
         string hours = (h); if (h<10) {hours = ("0"+hours);} 
         string timeleft = (minutes+":"+seconds);
         if (h>=1) {timeleft = (hours+":"+minutes+":"+seconds);}
         if (Period()>1440){timeleft = "OFF";}}                 
      //Get x,y co-ordinates for TimerLabel
      double xt = Time[0];
	   double yt = Bid + ((scale/1000) * TimerLabel_ShiftUpDown);
	   //Prepare text for TimerLabel
	   string bartimer = "[BidLine] Timer";
   	string tab3 = "  ";
	   if(Symbol()== "USDJPY"){tab3= "";}
	   if(Symbol()== "CHFJPY"){tab3= "";}
	   if(Symbol()== "AUDJPY"){tab3= "";}
	   if(Symbol()== "CADJPY"){tab3= "";}
	   if(Symbol()== "XAGJPY"){tab3= "";}	   
	   tab3= tab3 + "       ";
	   if (h>0) {tab3 = StringSubstr(tab3,5);}	
      for(i=0; i<=TimerLabel_ShiftRight; i++){tab3 = tab3 + " ";}  
      string Text3 = tab3 + timeleft;  
      string extra = "                    ";        
      //Construct/move TimerLabel  
      if(ObjectFind(bartimer) != 0){
         ObjectCreate(bartimer, OBJ_TEXT, 0, xt, yt);
         ObjectSetText(bartimer, Text3 , TimerLabel_FontSize, TimerLabel_FontStyle, TimerLabel_Color);}
      else{ObjectMove(bartimer, 0, xt, yt);}}            	   	          
          
   return(0);//Finished
   }
//+------------------------------------------------------------------+
//| End Program                                                      |
//+------------------------------------------------------------------+   



