/*-------------------------------------------------------------------
   Name: BarTimeLeft v1.0NF.mq4
   Copyright ©2010, Xaphod, http://forexwhiz.appspot.com
   
   Description: Displays the bar time left next to the current bar.
                Note, the bar time is off a few seconds. 
                The window other than the chart window is expected to
                be a stochastic indicator with setting 8,3,3.
                
   Change log: 
       2010-05-18. Xaphod, V1.0NF 
         Initial Release
-----------------------------------------------------------------*/
// Indicator properties
#property copyright "Copyright © 2010, Xaphod, forexwhiz.appspot.com"
#property link      "http://forexwhiz.appspot.com"
#property indicator_chart_window

#define INDICATOR_NAME "BarTimeLeft"
#define INDICATOR_VERSION "1.03"

extern int      BarTimePos=15;  // Position of the bar time relative to the latest bar
extern bool     BarTimeShowArrow=false;  // Draw an arrow infront of the time "<-15:30"
extern int      BarTimeFontSize=25;   // Set font size
extern color    BarTimeTextColor=Black;   // Set time left string text color
extern int      BarTimeWindow=2;     // Set window. If not chart window then stochastic(8,3,2) window 
extern int      BarTimeMaxTimeFrame=60;     // Maximum TF onwhich to show clock: 1,5,15,60,240,1440,10080,43200


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {
  return(0);
}
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit(){
  // Clear text objects
  for(int i=ObjectsTotal()-1; i>-1; i--)
    if (StringFind(ObjectName(i),INDICATOR_NAME)>=0)  ObjectDelete(ObjectName(i));
  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {   
  if (BarTimeMaxTimeFrame>=Period())
    BarTimeLeft(BarTimePos, BarTimeWindow, BarTimeShowArrow, BarTimeFontSize, BarTimeTextColor);   
  return(0);
}


//-----------------------------------------------------------------------------
// function: BarTimeLeft()
// Description: Displays the bar time left next to the current bar.
//-----------------------------------------------------------------------------
int BarTimeLeft(int ibarTimePos=3, int iWindow=0, bool bShowArrow=False, int iFontSize=8, color cTextColor=White) {
  datetime tTimeLeft,tTextPos;
  double dPrice;
  string sBarTimeLeft;
  string sObjTextName=INDICATOR_NAME+"_BarTime_Text";
  string sArrow=CharToStr(60)+CharToStr(150);
  
  // Calc time left and display position
  tTimeLeft=Time[0]+Period()*60-CurTime();
  tTextPos=Time[0]+Period()*60*ibarTimePos;
  if (iWindow==0)
    dPrice=Bid;
  else
    dPrice=(WindowPriceMax(iWindow)-WindowPriceMin(iWindow))/6; //sets static position in window
  
  // Show hours if period > 60 min 
  if (Period()>60) {
    sBarTimeLeft=TimeToStr(tTimeLeft,TIME_SECONDS );
    }
  else {
    sBarTimeLeft=StringSubstr(TimeToStr(tTimeLeft,TIME_SECONDS),3);
  }  
  
  // Exit if time is not available  
  if (sBarTimeLeft=="alid time")
    return(0);
  
  // Add Arrow infront of time
  if (bShowArrow==true) {
   sBarTimeLeft=sArrow+sBarTimeLeft;
  }  
  
  // Display time left    
  if (iWindow>=0 && iWindow<WindowsTotal()) {
    if (ObjectFind(sObjTextName)<0 )
      ObjectCreate(sObjTextName, OBJ_TEXT, iWindow, tTextPos, dPrice);
    else 
      ObjectMove(sObjTextName, 0, tTextPos, dPrice);
    ObjectSet(sObjTextName, OBJPROP_BACK, false);
    ObjectSetText(sObjTextName, sBarTimeLeft , iFontSize, "Arial", cTextColor); //"Courier New" "Trebuchet MS"   
  }
}


