xzl_countdown_timer

Download script
Author: Copyright 2014, William Kreider (Madhatt30)
xzl_countdown_timer

This script is designed to function as a live countdown clock directly on your trading charts. Its primary purpose is to provide traders with a real-time visual representation of how many seconds remain until the next candle (bar) opens, helping with precise timing and execution.

Key Functional Components:

1. Visual Customization The script allows users to personalize the display without needing to modify the underlying code. You can choose the font style (such as Arial, Times New Roman, or Courier), adjust the text size, toggle bold lettering, and select the color of the countdown. It also provides options to position the timer in specific corners of your screen with adjustable distances from the edges.

2. Time Synchronization Logic One of the most important features of this script is its ability to handle time discrepancies between different zones. It automatically calculates the difference between the "Server Time" (the time used by your trading platform) and your "Local Time." By identifying if the server is ahead or behind your local clock, it establishes a consistent offset. This ensures that the countdown remains accurate regardless of where in the world you are located or which broker's server you are connected to.

3. Real-Time Updates Instead of only updating when a price moves (which can sometimes be infrequent), this script utilizes an internal timer. This means the countdown updates every second, providing a smooth and fluid ticking motion on your screen.

4. Dynamic Calculation The logic works by identifying the start time of the current trading period and subtracting the elapsed local time. It then converts this difference into seconds. The result is displayed as a live string of numbers on your chart, which decreases steadily until it reaches zero, signaling the birth of a new candle.

5. Clean Workspace Integration The script is designed to be non-intrusive. When you remove the indicator from your chart, it automatically cleans up after itself by deleting the timer object, ensuring that no leftover text or "ghost" graphics remain on your workspace.

3 Views
0 Downloads
0 Favorites
xzl_countdown_timer
//+------------------------------------------------------------------+
//|                                          !ZL_Countdown Timer.mq4 |
//|                      Copyright 2014, William Kreider (Madhatt30) |
//|                                http://www.metatradersoftware.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, William Kreider (Madhatt30)"
#property link      "http://www.metatradersoftware.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

enum FontSelect 
  {
   Arial=0,
   Times_New_Roman=1,
   Courier=2
  };
//--- input parameters
input int         dispWindow=0;
input int         corner=1;
input FontSelect  selectedFont=1;
input int         textSize=16;
input bool        bold=true;
input color       fntcolor=clrYellow;
input int         XDistance=15;
input int         YDistance=15;

long           thisChart;
int            iFontType;
string         sBoldType;
string         sFontType;
int            timeOffset;
datetime       ServerLocalOffset;
datetime       prevTime,myTime,localtime;
bool           newBar = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   EventSetTimer(1);
   thisChart = ChartID();
   if(bold){
      sBoldType=" Bold";
   }else if(!bold){
      sBoldType="";
   }
   iFontType=selectedFont;
   Comment("");
   switch(iFontType){
      case 0: sFontType="Arial" + sBoldType; break;
      case 1: sFontType="Times New Roman" + sBoldType; break;
      case 2: sFontType="Courier" + sBoldType; break;
   }
   ObjectCreate(thisChart,"BarTimer",OBJ_LABEL,dispWindow,XDistance,YDistance);
//---
   datetime srvtime,tmpOffset;
   // Use RefreshRates to get the current time from TimeCurrent
   // Otherwise you'll just get the last known time
   RefreshRates();
   srvtime = TimeCurrent();
   // Modified
   localtime = TimeLocal()+TimeGMTOffset();
   if(TimeHour(srvtime)>TimeHour(localtime)){
      // Server Time is still ahead of us
      int newOffset = TimeHour(srvtime)-TimeHour(localtime);
      ServerLocalOffset = (newOffset*60*60);
   }else if(TimeHour(srvtime)<TimeHour(localtime)){
      // Server Time is Behind us
      int newOffset = TimeHour(localtime)-TimeHour(srvtime);
      ServerLocalOffset = (newOffset*60*60);
   }else{
      // No modification required
      ServerLocalOffset = srvtime;
   }
   localtime = TimeLocal()-ServerLocalOffset;
   
   tmpOffset = TimeSeconds(srvtime) - TimeSeconds(localtime);
   if(tmpOffset < 30 && tmpOffset >= 0){
      timeOffset = TimeSeconds(srvtime) - TimeSeconds(localtime);
   }
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
  {
   EventKillTimer();
   ObjectDelete("BarTimer");
  }
//+------------------------------------------------------------------+
//| 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()
  {
//---
   datetime localtime;
   localtime = TimeLocal()+(TimeGMTOffset()+(60*60));
   if(ObjectFind(thisChart,"BarTimer")>=0){
      // Found object it exists
      ObjectSet("BarTimer",OBJPROP_CORNER,corner);
      ObjectSet("BarTimer",OBJPROP_XDISTANCE,XDistance);
      ObjectSet("BarTimer",OBJPROP_YDISTANCE,YDistance);
      ObjectSetText("BarTimer",TimeToStr(Time[0]+Period()*60-localtime-timeOffset,TIME_SECONDS ),textSize,sFontType,fntcolor);
   } 
  }
//+------------------------------------------------------------------+

Comments