//+------------------------------------------------------------------+ //| eClock.mq5 | //+------------------------------------------------------------------+ #property copyright "mladen" #property link "mladenfx@gmail.com" #property version "1.00" // // // // // input color colorNormal = Gold; // Color for normal time input color colorLate = Red; // Color when bar expired input int fontSize = 10; // Font size for display input int shiftBars = 5; // Bars for time display horizontal shift // // // // // int atrHandle; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // #define clockName "eclock" int OnInit() { atrHandle = iATR(NULL,0,30); ShowClock(); EventSetTimer(1); return(0); } void OnDeinit(const int reason) { ObjectDelete(0,clockName); EventKillTimer(); } void OnTimer() { ShowClock(); ChartRedraw(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // void ShowClock() { static bool working = false; if (working) return; working = true; // // // // // int periodMinutes = periodToMinutes(Period()); int shift = periodMinutes*shiftBars*60; int currentTime = (int)TimeCurrent(); int localTime = (int)TimeLocal(); int barTime = (int)iTime(PERIOD_CURRENT); int diff = 0; // // // // // MqlDateTime ctime; TimeToStruct(currentTime,ctime); MqlDateTime ltime; TimeToStruct(localTime ,ltime); // // // // // if (ctime.year<(ltime.year-1)) { periodMinutes = 0; // // // // // if (ltime.day_of_week == 6 || ltime.day_of_week == 0) { currentTime = (int)StringToTime(TimeToString(localTime,TIME_DATE))+24*3600; if (ltime.day_of_week==6) currentTime += 24*3600; int temp = currentTime; currentTime = localTime; localTime = temp; barTime = currentTime; } } else diff = (int)round((TimeCurrent()-localTime)/3600.0)*3600; // // // // // color theColor; string time = getTime(barTime+periodMinutes*60-localTime-diff,theColor); time = (TerminalInfoInteger(TERMINAL_CONNECTED)) ? time : time+" x"; // // // // // if(ObjectFind(0,clockName) < 0) ObjectCreate(0,clockName,OBJ_TEXT,0,0,0); ObjectSetString(0,clockName,OBJPROP_TEXT,time); ObjectSetString(0,clockName,OBJPROP_FONT,"Arial bold"); ObjectSetInteger(0,clockName,OBJPROP_FONTSIZE,fontSize); ObjectSetInteger(0,clockName,OBJPROP_COLOR,theColor); if (ChartGetInteger(0,CHART_SHIFT,0)==0 && (shift >=0)) ObjectSetInteger(0,clockName,OBJPROP_TIME,iTime()-shift*3.5); else ObjectSetInteger(0,clockName,OBJPROP_TIME,iTime()+shift); // // // // // double price[]; if (CopyClose(Symbol(),0,0,1,price)<=0) { working = false; return; } double atr[]; if (CopyBuffer(atrHandle,0,0,1,atr)<=0) { working = false; return; } price[0] += atr[0]; // // // // // bool visible = ((ChartGetInteger(0,CHART_VISIBLE_BARS,0)-ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0)) > 0); if ( visible && price[0]>=ChartGetDouble(0,CHART_PRICE_MAX,0)) ObjectSetDouble(0,clockName,OBJPROP_PRICE,price[0]-2.0*atr[0]); else ObjectSetDouble(0,clockName,OBJPROP_PRICE,price[0]); // // // // // working = false; } //+------------------------------------------------------------------+ //| //+------------------------------------------------------------------+ // // // // // string getTime(int times, color& theColor) { string stime = ""; int seconds; int minutes; int hours; // // // // // if (times < 0) { theColor = colorLate; times = (int)fabs(times); } else theColor = colorNormal; seconds = (times%60); hours = (times-times%3600)/3600; minutes = (times-seconds)/60-hours*60; // // // // // if (hours>0) if (minutes < 10) stime = stime+(string)hours+":0"; else stime = stime+(string)hours+":"; stime = stime+(string)minutes; if (seconds < 10) stime = stime+":0"+(string)seconds; else stime = stime+":" +(string)seconds; return(stime); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // datetime iTime(ENUM_TIMEFRAMES forPeriod=PERIOD_CURRENT) { datetime times[]; if (CopyTime(NULL,forPeriod,0,1,times)<=0) return(TimeLocal()); return(times[0]); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // int periodToMinutes(int period) { int i; static int _per[]={1,2,3,4,5,6,10,12,15,20,30,0x4001,0x4002,0x4003,0x4004,0x4006,0x4008,0x400c,0x4018,0x8001,0xc001}; static int _min[]={1,2,3,4,5,6,10,12,15,20,30,60,120,180,240,360,480,720,1440,10080,43200}; if (period==PERIOD_CURRENT) period = Period(); for(i=0;i<20;i++) if(period==_per[i]) break; return(_min[i]); }