TimeToNextCandle

Download script
Author: Yaroslav Krasovsky
TimeToNextCandle

This script is designed to provide traders with a real-time visual countdown on their trading charts. Its primary purpose is to monitor and display exactly how much time remains before the current candle closes and the next one begins, allowing for better precision in timing market entries and exits.

How it Works

The logic follows a continuous cycle to ensure the information stays updated second-by-second:

  • Continuous Monitoring: The script runs on a high-frequency timer. Instead of waiting for a new price movement (tick) to update, it actively calculates the remaining time every second, ensuring the display is always accurate even during periods of low market activity.
  • Dynamic Time Calculation: The tool looks at the specific timeframe you are currently viewing (e.g., 1-minute, 15-minute, or 4-hour charts). It determines the total duration of one candle for that timeframe and subtracts the elapsed time from the current candle's start point.
  • Smart Formatting: To keep the display clean and readable, the script automatically formats the countdown based on how much time is left:
    • If there are only a few minutes remaining, it shows the minutes and seconds.
    • If the timeframe is larger (like 4-hour charts), it intelligently adjusts to show hours, minutes, and seconds.
    • For very long timeframes, it will include the number of days remaining.
  • Percentage Progress: Alongside the actual time countdown, the script calculates a percentage (%) of the candle's completion. This gives you an immediate visual cue of how much of the current trading period has passed.

Visual Presentation

The information is displayed as a text label on your chart. You can customize several aesthetic elements:

  • Font Style: Choose the typeface that best fits your screen layout.
  • Size and Color: Adjust how large the countdown appears and what color it should be (e.g., red for high visibility).
  • Positioning: The script is designed to anchor itself to the bottom-right corner of your chart, ensuring it stays out of the way of price action while remaining easily accessible.

In short, this tool removes the guesswork from "time management" on a chart, providing a live, formatted clock that tells you exactly how much time you have left in the current period.

3 Views
0 Downloads
0 Favorites
TimeToNextCandle
//+------------------------------------------------------------------+
//|                                             TimeToNextCandle.mq4 |
//|                                               Yaroslav Krasovsky |
//|                       https://www.mql5.com/en/users/rock-n-rolla |
//+------------------------------------------------------------------+
#property copyright "Yaroslav Krasovsky"
#property link      "https://www.mql5.com/en/users/rock-n-rolla"
#property version   "1.00"
#property strict
#property indicator_chart_window
//--- input parameters
input string LabelFont = "Arial";
input int LabelSize = 15;
input color LabelColor = clrRed;
input int LabelDistance = 15;
const string LabelName = "TimeToNextCandle";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   EventSetTimer(1);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   EventKillTimer();
   ObjectDelete(0, LabelName);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   CalcTime();
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   CalcTime();
  }
//+------------------------------------------------------------------+
void CalcTime(void)
  {
   // checking is there output label. create it if necessary
   if (ObjectFind(LabelName) == -1)
   {
      ObjectCreate(0, LabelName, OBJ_LABEL, 0, 0, 0);
      ObjectSetString(0, LabelName, OBJPROP_FONT, LabelFont);
      ObjectSetInteger(0, LabelName, OBJPROP_FONTSIZE, LabelDistance);
      ObjectSetInteger(0, LabelName, OBJPROP_COLOR, LabelColor);
      ObjectSetInteger(0, LabelName, OBJPROP_ANCHOR, ANCHOR_RIGHT_LOWER);
      ObjectSetInteger(0, LabelName, OBJPROP_CORNER, CORNER_RIGHT_LOWER);
      ObjectSetInteger(0, LabelName, OBJPROP_XDISTANCE, LabelDistance);
      ObjectSetInteger(0, LabelName, OBJPROP_YDISTANCE, LabelDistance);
   }
   // calculating remaining time to next candle
   datetime TimeTo = PeriodSeconds() - (TimeCurrent() - Time[0]);
   // assembling the output string depending on current period on the chart
   string Out = StringFormat("%.2d", TimeSeconds(TimeTo));
   if (TimeTo >= 3600)
   {
      Out = StringFormat("%.2d:%s", TimeMinute(TimeTo), Out);
      if (TimeTo >= 86400)
        Out = StringFormat("%d day(s) %.2d:%s", int(TimeTo / 86400), TimeHour(TimeTo), Out);
      else
        Out = StringFormat("%d:%s", TimeHour(TimeTo), Out);
   }
   else
     Out = StringFormat("%d:%s", TimeMinute(TimeTo), Out);
   ObjectSetString(0, LabelName, OBJPROP_TEXT, StringFormat("%s (%.0f%s)", Out, 100.0 / PeriodSeconds() * TimeTo, "%"));
  }
//+------------------------------------------------------------------+

Comments