Which time controls bar plot

To add comments, please log in or register
samjesse
643
samjesse  

Hi

A mql5 code needs to know when a new bar arrives. various methods use "TimeCurrent()" which can remain fixed "not changing" for a long time till a new tick is received by the termnal from the server.

How does that affect the ploting of a new price bar? i.e. does the terminal plot the bar based on "passing of" the TimeLocal(), or waits for the server's TimeCurrent() to plot the new bar?


Thanks

Carl Schreiber
Moderator
7067
Carl Schreiber  

You can do it this way:

bool isNewBar(const string sym, const ENUM_TIMEFRAMES tf, const datetime t) {
   static datetime actOpn=0,nxtOpn=0;
   if (t>=nxtOpn) {
      actOpn = iTime(sym,tf,iBarShift(sym,tf,t));
      nxtOpn = actOpn + PeriodSeconds(tf);
      return(true);
   }
   return(false);
}
..
if ( isNewBar(_Symbol, _Period, TimeCurrent() ) ) {
   ...
}
Anthony Garot
1817
Anthony Garot  
samjesse:

How does that affect the plotting of a new price bar? i.e. does the terminal plot the bar based on "passing of" the TimeLocal(), or waits for the server's TimeCurrent() to plot the new bar?

I would guess that Terminal uses the price embedded with the tick itself in the MQLTick struct.

struct MqlTick
  {
   datetime     time;          // Time of the last prices update
   double       bid;           // Current Bid price
   double       ask;           // Current Ask price
   double       last;          // Price of the last deal (Last)
   ulong        volume;        // Volume for the current Last price
   long         time_msc;      // Time of a price last update in milliseconds
   uint         flags;         // Tick flags
   double       volume_real;   // Volume for the current Last price with greater accuracy
  };
samjesse
643
samjesse  
Carl Schreiber:

You can do it this way:


Then the question becomes, where would you call it? in OnTick() or OnTimer()?

Anthony Garot
1817
Anthony Garot  
samjesse:


Then the question becomes, where would you call it? in OnTick() or OnTimer()?

OnTick().

OnTick() does not fire when no ticks have come in, thus no price changes, thus no bar is formed. Like on a weekend.

samjesse
643
samjesse  
Anthony Garot:

OnTick().

OnTick() does not fire when no ticks have come in, thus no price changes, thus no bar is formed. Like on a weekend.


The problem then becomes in a slow day when ticks are not being updated every second "at least", the new bar will be ploted but the isNewBar will not fire till a tick is received, (if bar ploting depends on local time which I guess it does).

Fernando Carreiro
4262
Fernando Carreiro  
samjesse: The problem then becomes in a slow day when ticks are not being updated every second "at least", the new bar will be ploted but the isNewBar will not fire till a tick is received, (if bar ploting depends on local time which I guess it does).

No! If no new tick arrives then no new bar is plotted. The a new bar is only plotted when a tick arrives and if more then one bar's worth of time goes by then that bar is skipped.

samjesse
643
samjesse  
Fernando Carreiro:

No! If no new tick arrives then no new bar is plotted. The a new bar is only plotted when a tick arrives and if more then one bar's worth of time goes by then that bar is skipped.


Sorry for the bad explanation; I ment to say;

In a slow day when ticks are not being received passed the time when the current bar timer ends, Will the terminal close the current bar as index 1 or will it still be index 0 till a new tick is received well passed the time for a new bar?

Fernando Carreiro
4262
Fernando Carreiro  
samjesse: Sorry for the bad explanation; I ment to say; In a slow day when ticks are not being received passed the time when the current bar timer ends, Will the terminal close the current bar as index 1 or will it still be index 0 till a new tick is received well passed the time for a new bar?
It will remain "open" at bar shift 0 until a new tick arrives to close it and start the new bar.
To add comments, please log in or register