Scripts: Simple Bar Timer

 

Simple Bar Timer:

It is a script to display the time remaining until next bar arrives.

Simple Bar Timer

Author: Conor Mcnamara

 
Automated-Trading:

Simple Bar Timer:

Author: Conor Mcnamara

It works as intended, but when I switch the time frame (HTF or LTF, doesn`t matter), it dissapears and has to be reattached. 

Any advice?

 
Alex Stefanescu #:

It works as intended, but when I switch the time frame (HTF or LTF, doesn`t matter), it dissapears and has to be reattached. 

Any advice?

Convert the Script into an Indicator, and put the timeframes switch inside the OnInit() event handler.

As alternatives, 2 indicators are attached.

 

Strictly speaking this script will not work correctly depending on the symbols selected in the Market Watch.

TimeCurrent() is returning the last update time of any symbol in the Market Watch, if they are a lot of symbols then fine, if there are only symbol(s) with low activity, your counter will not move.

Additionally, using sleep(1000), you can have a shift of almost 1 second in your counter.

 Exemple : it should be 00:37:05

 
Alain Verleyen # with low activity, your counter will not move.

My countdown timer (MT4) uses the timer to update.
          Indicators: Zero Lag Timer - Indices - Articles, Library comments - MQL5 programming forum #8 (2017)

 
Alain Verleyen #:

if there are only symbol(s) with low activity, your counter will not move.


I don't know why that would be. The OnStart function runs immediately after attaching the script, and the logic finds how much time has passed since the bar started.

If there's a problem with lag, then it's due to the time server from your broker most likely

 
Conor Mcnamara #:

I don't know why that would be. The OnStart function runs immediately after attaching the script, and the logic finds how much time has passed since the bar started.

If there's a problem with lag, then it's due to the time server from your broker most likely

This is due to your code and is not related to the broker, but to the symbol(s) selected in the Market Watch. To demonstrate the issue I had only USDCHF.

I already explained why it happens : your bar time update code is executed every second (more or less) but TimeCurrent() isn't moving, you should never use TimeCurrent() for such "Bar Timer".

Additionally William provided a link to a code where it's done correctly.

 
Alain Verleyen #:

This is due to your code and is not related to the broker, but to the symbol(s) selected in the Market Watch. To demonstrate the issue I had only USDCHF.

I already explained why it happens : your bar time update code is executed every second (more or less) but TimeCurrent() isn't moving, you should never use TimeCurrent() for such "Bar Timer".

Additionally William provided a link to a code where it's done correctly.

You're wrong.

Time current is moving because it's in this while loop:

 while(!IsStopped())
     {
      datetime srv = TimeCurrent();    // server time
     }
If TimeCurrent() is not moving for you, please email your broker
 
Conor Mcnamara #:

You're wrong.

Time current is moving because it's in this while loop:

If TimeCurrent() is not moving for you, please email your broker

The fewer symbols a user has selected in Market Watch, the less frequently the value returned by TimeCurrent() is updated.

https://www.mql5.com/en/docs/dateandtime/timecurrent

Returns the last known server time, time of the last quote receipt for one of the symbols selected in the "Market Watch" window. In the OnTick() handler, this function returns the time of the received handled tick. In other cases (for example, call in handlers OnInit(), OnDeinit(), OnTimer() and so on) this is the time of the last quote receipt for any symbol available in the "Market Watch" window, the time shown in the title of this window. The time value is formed on a trade server and does not depend on the time settings on your computer. There are 2 variants of the function.

 
Vladislav Boyko #:

The fewer symbols a user has selected in Market Watch, the less frequently the value returned by TimeCurrent() is updated.

Thanks Vlad, interesting.

Anyway, I would never use this at the weekend as I will never trade at the weekend when forex markets are closed (and I have no interest in crypto).

Still though, it is good code.

 
Conor Mcnamara #:

Thanks Vlad, interesting.

Anyway, I would never use this at the weekend as I will never trade at the weekend when forex markets are closed (and I have no interest in crypto).

Still though, it is good code.

I said the same thing as Alain Verleyen, but in different words and less detail. Stop thinking that someone is attacking or hating you; it's just sharing an experience.