//+------------------------------------------------------------------+ //| 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.01" #property strict //+------------------------------------------------------------------+ //| Generic / Utility functions | //+------------------------------------------------------------------+ double round_down( double v, double to){ return to * MathFloor(v / to); } double round_up( double v, double to){ return to * MathCeil( v / to); } double round_nearest( double v, double to){ return to * MathRound(v / to); } string as_string(ENUM_TIMEFRAMES period){ if(period == PERIOD_CURRENT) period = (ENUM_TIMEFRAMES) _Period; string period_xxx = EnumToString(period); // PERIOD_XXX return StringSubstr(period_xxx, 7); // XXX } void alert_id(string msg){ Alert(StringFormat("%s %s %s %s", WindowExpertName(), _Symbol, as_string(PERIOD_CURRENT), msg) ); } //+------------------------------------------------------------------+ //} Generic code | //+------------------------------------------------------------------+ //| Specific code | //+------------------------------------------------------------------+ #property indicator_chart_window #property indicator_buffers 0 enum FontSelect{Arial, Times_New_Roman, Courier}; static string font2string[]={"Arial", "Times New Roman", "Courier"}; //--- input parameters input int dispWindow = 0; input ENUM_BASE_CORNER corner = CORNER_LEFT_LOWER; input FontSelect selectedFont = Times_New_Roman; input int textSize = 16; input bool bold = true; input color fntcolor = clrYellow; input int XDistance = 15; input int YDistance = 15; input bool Beep = true; string sFontType; #define HR2400 86400 // int(PERIOD_D1 * 60) int gLocalToServer = HR2400; bool isTimerOn = false; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit(){ //--- indicator buffers mapping sFontType = font2string[selectedFont] + (bold ? " Bold" : ""); #define TIMER "BarTimer" ObjectCreate(TIMER, OBJ_LABEL, dispWindow, 0, 0); ObjectSet(TIMER, OBJPROP_CORNER, corner); ObjectSet(TIMER, OBJPROP_XDISTANCE, XDistance); ObjectSet(TIMER, OBJPROP_YDISTANCE, YDistance); ObjectSetInteger(dispWindow, TIMER, OBJPROP_SELECTABLE, false); //--- return INIT_SUCCEEDED; } void OnDeinit(const int reason){ EventKillTimer(); isTimerOn = false; ObjectDelete(TIMER); } //+------------------------------------------------------------------+ //| 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[]){ if(!isTimerOn){ isTimerOn = EventSetTimer(1); if(!isTimerOn){alert_id(StringFormat("EST:%i", _LastError) ); return prev_calculated; } } static datetime time0; if(gLocalToServer == HR2400) time0 = Time[0]; // First tick, suppress alert gLocalToServer = (int)round_nearest(TimeCurrent() - TimeLocal(), 1800); if(time0 != Time[0]){ time0 = Time[0]; if(Beep) PlaySound("Alert2.wav"); } return rates_total; } //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ void OnTimer(){ if(gLocalToServer == HR2400) return; // No first tick, no Time[0] datetime servertime = TimeLocal() + gLocalToServer; int remaining = int(Time[0] + PeriodSeconds() - servertime); int seconds = remaining % HR2400; int days = (remaining - seconds)/HR2400; string display = (days > 0 ? string(days)+"-" : "") + TimeToString(remaining, TIME_SECONDS); ObjectSetText(TIMER, display, textSize, sFontType, fntcolor); } //+------------------------------------------------------------------+