Author: Copyright 2014,fxMeter.

This tool is designed to provide traders with a real-time countdown of the remaining time left in the current trading candle (bar) on their MetaTrader 5 chart. By displaying this information clearly on the screen, it helps users monitor exactly how much time remains before the current price period ends and a new one begins.

Core Functionality

The script performs three primary actions to provide a seamless user experience:

  • Real-Time Calculation: The tool constantly monitors the market time against the start time of the current candle. It automatically calculates the difference in hours, minutes, and seconds, ensuring that the countdown remains accurate to the second as the market moves.
  • Dynamic Display Options: Users can choose how they want this information to appear on their workspace. You can set the timer to either follow the live price action (moving along with the candles) or stay in a fixed position on the corner of your screen for constant visibility.
  • Customizable Visuals: To ensure it fits perfectly into your trading layout, the script allows you to customize the text color and the specific font style used for the countdown.

How it Works

  1. Initialization: When you load the script, it prepares the display area and sets up a background timer that refreshes every second.
  2. Continuous Updating: Every second, the script calculates the remaining duration of the current timeframe (e.g., if you are on a 15-minute chart, it tracks the remaining minutes/seconds of that specific block).
  3. Automatic Cleanup: The script is designed to be "clean." It automatically removes its visual elements from your chart when you remove the indicator, ensuring no leftover text or objects clutter your screen.

Key Benefits for Traders

This tool simplifies time management during active trading sessions. Instead of manually calculating how much time remains until the next candle opens—which is vital for timing entries and exits—this script provides an instant, glanceable countdown directly on your chart.

Price Data Components
Series array that contains close prices for each bar
14 Views
0 Downloads
0 Favorites
K-TimeLeft
//+------------------------------------------------------------------+
//|                                                   K-TimeLeft.mq5 |
//|                                          Copyright 2014,fxMeter. |
//|                            https://www.mql5.com/en/users/fxmeter |
//+------------------------------------------------------------------+
//2020-06-13 updated.  iTime() and iClose() have been implemented as API,just need to directly call it.
//2017-11-13 publish to MQL5.COM code base
//2014-11-15 create
#property copyright "Copyright 2014,fxMeter."
#property link      "https://mql5.com/en/users/fxmeter"
#property version   "1.00"
#property indicator_chart_window
#define  OBJ_NAME "time_left_label"
#define  FONT_NAME "Microsoft YaHei"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum ENUM_POS_TL
{
   FOLLOW_PRICE,
   FIXED_POSITION
};
input color  LabelColor=clrOrangeRed;
input ENUM_POS_TL LabelPosition=FIXED_POSITION;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeleteLabel()
{
   int try=10;
   while(ObjectFind(0,OBJ_NAME)==0)
   {
      ObjectDelete(0,OBJ_NAME);
      if(try--<=0)break;
   }
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
   EventSetTimer(1);
   DeleteLabel();

//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   EventKillTimer();
   DeleteLabel();
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
//---

//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
   UpdateTimeLeft();
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void UpdateTimeLeft()
{

   int seconds=0;// the left seconds of the current bar
   int h = 0; //Hour
   int m = 0; //Minute
   int s = 0; //Second  hh:mm:ss

   datetime time  = iTime(Symbol(),PERIOD_CURRENT,0);
   double   close = iClose(Symbol(),PERIOD_CURRENT,0);

   seconds=PeriodSeconds(PERIOD_CURRENT) -(int)(TimeCurrent()-time);

   h = seconds/3600;
   m = (seconds - h*3600)/60;
   s = (seconds - h*3600 - m*60);

   string text="  >>> "
               +StringFormat("%02d",h)+":"
               +StringFormat("%02d",m)+":"
               +StringFormat("%02d",s);

   if(LabelPosition==FOLLOW_PRICE)
   {
      if(ObjectFind(0,OBJ_NAME)!=0)
      {
         ObjectCreate(0,OBJ_NAME,OBJ_TEXT,0,time,close+_Point);
         ObjectSetString(0,OBJ_NAME,OBJPROP_TEXT,text);
         ObjectSetString(0,OBJ_NAME,OBJPROP_FONT,FONT_NAME);
         ObjectSetInteger(0,OBJ_NAME,OBJPROP_COLOR,LabelColor);
         ObjectSetInteger(0,OBJ_NAME,OBJPROP_SELECTABLE,false);
         ObjectSetInteger(0,OBJ_NAME,OBJPROP_FONTSIZE,12);

      }
      else
      {
         ObjectSetString(0,OBJ_NAME,OBJPROP_TEXT,text);
         ObjectMove(0,OBJ_NAME,0,time,close+_Point);
      }
   }
   else if(LabelPosition==FIXED_POSITION)
   {
      if(ObjectFind(0,OBJ_NAME)!=0)
      {
         ObjectCreate(0,OBJ_NAME,OBJ_LABEL,0,0,0);
         ObjectSetInteger(0,OBJ_NAME,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
         ObjectSetInteger(0,OBJ_NAME,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
         ObjectSetInteger(0,OBJ_NAME,OBJPROP_XDISTANCE,200);
         ObjectSetInteger(0,OBJ_NAME,OBJPROP_YDISTANCE,2);
         ObjectSetString(0,OBJ_NAME,OBJPROP_TEXT,text);
         ObjectSetString(0,OBJ_NAME,OBJPROP_FONT,FONT_NAME);
         ObjectSetInteger(0,OBJ_NAME,OBJPROP_COLOR,LabelColor);
         ObjectSetInteger(0,OBJ_NAME,OBJPROP_SELECTABLE,true);
         ObjectSetInteger(0,OBJ_NAME,OBJPROP_FONTSIZE,12);
      }
      else
         ObjectSetString(0,OBJ_NAME,OBJPROP_TEXT,text);
   }
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//datetime iTime(string sym,ENUM_TIMEFRAMES tf,int index)
//  {
//   datetime t[1]={0};
//   if(CopyTime(sym,tf,index,1,t)!=1)
//      return(0);
//   return(t[0]);
//  }
////+------------------------------------------------------------------+
////|                                                                  |
////+------------------------------------------------------------------+
//double iClose(string sym,ENUM_TIMEFRAMES tf,int index)
//  {
//   double cls[1]={0.0};
//   if(CopyClose(sym,tf,index,1,cls)!=1)
//      return(0.0);
//   return(cls[0]);
//  }
////+------------------------------------------------------------------+

Comments