BarTimer
Download scriptThis script is designed to provide traders with a real-time visual progress tracker directly on their trading charts. It serves as a dynamic "countdown" or "progress bar" that shows exactly how much time has elapsed within the current active candle (bar) and how much remains until the next one begins.
How it Works
The logic follows a straightforward process to translate raw market time into a user-friendly display:
1. Time Calculation The script constantly monitors the difference between the current market time and the opening time of the current candle. It calculates this duration in seconds.
2. Progress Mapping To make this information useful, the tool performs two main calculations:
- Visual Representation: It takes the elapsed seconds and maps them against the total duration of the specific timeframe you are using (e.g., a 1-hour chart vs. a 5-minute chart). This determines where the "cursor" should be in your custom progress string.
- Percentage Tracking: It calculates the exact percentage of completion for the current candle, allowing you to see at a glance how far through the trading period you have progressed.
3. Dynamic Display The script then constructs a text string on your screen. It takes your custom "base" format (which uses placeholders like dots) and updates it in real-time.
- The section of the dots before the current position represents the time already passed.
- The section of the dots after the current position represents the time remaining.
- Finally, it appends the calculated percentage to the end of the string.
Key Features for the User
- Customizable Placement: You can choose where on the screen the timer appears (corners and distances) to ensure it doesn't obstruct your view of the price action.
- Visual Formatting: The tool allows you to customize the font style, size, and color so that the timer integrates perfectly with your existing chart aesthetics.
- Automatic Updates: The logic runs automatically, refreshing the text every time the market moves, providing a live heartbeat of the current candle's lifecycle.
In short, this script transforms abstract time into a clear, visual progress indicator, helping you manage entries and exits by knowing exactly how much time is left before a candle closes and a new one opens.
//+------------------------------------------------------------------+
//| BarTimer.mq4 |
//| Copyright © 2008, Art Royal s.r.o. |
//| Author: Vasyl Gumenyak |
//| http://www.jiport.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Art Royal s.r.o."
#property link "http://www.jiport.com/"
#property indicator_chart_window
#property indicator_buffers 0
extern string FontName="Arial";
extern int FontSize=14;
extern color FontColor=White;
extern int Corner=0;// values [0-3] - îïîðíûé óãîë
extern int XDistance=250;// x ðàññòîÿíèå îò îïîðíîãî óãëà
extern int YDistance=0;// y ðàññòîÿíèå îò îïîðíîãî óãëà
int lenbase;
string s_base=":...:...:...:...:";// ñòðîêà äëÿ ôîðìèðîâàíèÿ èíäèêàòîðà ñ ïîëçóíêîì
int init()
{
lenbase=StringLen(s_base);
return(0);
}
int deinit()
{
if (ObjectFind("BarTimer") != -1) ObjectDelete("BarTimer");
return(0);
}
//+------------------------------------------------------------------+
//| ïîëçóíîê îòìå÷àåò òåêóùèåå ïîëîæåíèå ïî âðåìåíè |
//| îòíîñèòåëüíî íà÷àëà è êîíöà òåêóùåãî áàðà, |
//| äîïîëíèòåëüíî äàåòñÿ äîëÿ âðåìåíè |
//| ïðîøåäøåãî îò íà÷àëà áàðà â ïðîöåíòàõ |
//+------------------------------------------------------------------+
int start()
{
int i=0,sec=0;
double pc=0.0;
string time="",s_end="",s_beg="";
if (ObjectFind("BarTimer") == -1) {// åñëè îáúåêò íå íàéäåí - ñîçäàåì è çàäàåì ïàðàìåòðû
ObjectCreate("BarTimer" , OBJ_LABEL,0,0,0);
ObjectSet("BarTimer", OBJPROP_XDISTANCE, XDistance);
ObjectSet("BarTimer", OBJPROP_YDISTANCE, YDistance);
ObjectSet("BarTimer", OBJPROP_CORNER, Corner);
}
sec=TimeCurrent()-Time[0];// âðåìÿ â ñåêóíäàõ îò íà÷àëà áàðà
i=(lenbase-1)*sec/(Period()*60);// ïîçèöèÿ ïîëçóíêà
pc=100.0*sec/(Period()*60);// âðåìÿ îò íà÷àëà áàðà â ïðîöåíòàõ
if(i>lenbase-1) i=lenbase-1;// âîçìîæíî èçëèøíèé êîíòðîëü ãðàíèöû
if(i>0) s_beg=StringSubstr(s_base,0,i);
if(i<lenbase-1) s_end=StringSubstr(s_base,i+1,lenbase-i-1);
time=StringConcatenate(s_beg,"|",s_end," ",DoubleToStr(pc,0),"%");
ObjectSetText("BarTimer", time, FontSize, FontName, FontColor);
return(0);
}
//+------------------------------------------------------------------+
Comments