MQL4 Real Time Clock (second by second) - page 3

 
brewmanz:

A) Don't bother using MT4 as a real time clock. It's a bit like getting a dog to walk on its hind legs - interesting to see it done, but doesn't actually achieve much.

B) Why so critical to process each second? If nothing much is happening with ticks, then nothing much is happening to prices.

(each comment worth 1 cent)


A) Why shouldn't we! Is just either pc or server time; I guess we can get info accurately.

B) Why is it critical ... let me see- high frequency trading - not based on asynchronous information where tick-by-tick is ! that should be a valid reason I guess.

I am still waiting for my 2 cents.

 
Ais:

MQL4 Reference : https://docs.mql4.com/runtime/start : "Scripts and experts work in their own thread. Custom indicators work in the main interface thread."


So your indicator displays time on the Interface of the user in the GUI ? real time / second by second.
 

To work "second by second" we use "Sleep()" function.

To do this, we measure run time of main routine.

For example, let run time will be stored in variable "int Runtime;", and let "RunTime < 1000".

Then we invoke "Sleep (1000 - RunTime) ;" to make sure that next run will take place in 1000 ms, with given precision of course.

But.

MQL4 Reference : https://docs.mql4.com/common/Sleep : "The Sleep() function cannot be called from custom indicators since they calculate in the interface thread and may not decelerate it."

 

Simple solution:

Include the calculations in your EA and your are fine.

Pros:

No need to call icustom

No indicator

Cons:

No Indicator. But for high-freq trading you *must* use an EA because humans are to slow.

 

"High-frequency" and "low-latency" are slightly differ.

And, visualisations often used for analysis or for monitoring, not only for trading.

 
Ais:

"High-frequency" and "low-latency" are slightly differ.

And, visualisations often used for analysis or for monitoring, not only for trading.

If you really need monitoring you can rebuild the indictor behavior with lines. Sure high performance is something different.

 

What if we want to analyse and monitor events on time intervals basis?
For example, every 1 second.

 
Ais:

What if we want to analyse and monitor events on time intervals basis?
For example, every 1 second.

As soon as you talk about tick-by-tick you mean ultra high frequency ....

'Analysis of ultra-high-frequency financial data using advanced Fourier transforms' (2009) Finance Research Letters Vol 6, pp 47-53 (with I.Giampaoli and W.L.Ng)

Sampling in predefined intervals can be defined as high-frequency sampling where T<t

 
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Black
#property indicator_color2 Black
#property indicator_color3 Red
#property indicator_color4 Blue
#property indicator_color5 Red
#property indicator_color6 Red
#property indicator_color7 Red
#property indicator_color8 Red
//---- buffers
double open[];
double close[];
double high[];
double low[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];
 
extern int MaxHistory=200;
extern int TimeInSeconds=5;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
double a.open[];
double a.close[];
double a.high[];
double a.low[];
 
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,high);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,low);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,5);
   SetIndexBuffer(2,open);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,5);
   SetIndexBuffer(3,close);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(6,ExtMapBuffer7);
   SetIndexStyle(7,DRAW_LINE);
   SetIndexBuffer(7,ExtMapBuffer8);
   ArrayResize(a.high,MaxHistory);
   ArrayResize(a.low,MaxHistory);
   ArrayResize(a.open,MaxHistory);
   ArrayResize(a.close,MaxHistory);      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void redraw(){
   for(int i=200;i>=0;i--){
      if(i==200){
      open[i]=0;
      close[i]=0;
      high[i]=0;
      low[i]=0;
      }else{
      open[i]=a.open[i];
      close[i]=a.close[i];
      high[i]=a.high[i];
      low[i]=a.low[i];      
      }
   }
}
 
void shift(){
   for(int i=200;i>0;i--){
      a.open[i]=a.open[i-1];
      a.close[i]=a.close[i-1];
      a.high[i]=a.high[i-1];
      a.low[i]=a.low[i-1];
   }
}
 
extern int sleep=100;
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   bool running=true;
   static double price=0;
   static int t=0;
   static datetime t.r=0; 
   static bool updates=false;
   //t=TimeCurrent();
   //while(running){
      updates=false;
      if(TimeCurrent()>(t+TimeInSeconds)){
 
         shift();
         if(a.open[1]==EMPTY || a.open[1]==EMPTY_VALUE || a.open[1]==0){
            a.open[0]=Bid;
            a.close[0]=Bid;
            a.high[0]=Bid;
            a.low[0]=Bid;
         }else{
         a.open[0]=a.close[1];
         a.high[0]=a.close[1];
         a.low[0]=a.close[1];
         a.close[0]=a.close[1];
         }
         Print("shift "+a.open[0]+" - "+a.open[1]);
         updates=true;
         t=TimeCurrent();
      }
      if(price!=Bid){
            price=Bid;
            a.close[0]=price;
            a.high[0]=MathMax(price,a.high[0]);
            a.low[0]=MathMin(price,a.low[0]);
            updates=true;
      }       
      if(Time[0]!=t.r){
         t.r=Time[0];
         redraw();
      }else{
         if(updates){
            redraw();
         }
      }
      if(IsStopped()) running=false;
   //   Sleep(sleep);
   //}
//----
   return(0);
  }
easily re-codable to an EA to use the proper sleep function. Already did this.
 
zzuegg:
easily re-codable to an EA to use the proper sleep function. Already did this.


Nice program . TimeCurrent() is the tick-by-tick price we need to get the computer price!

Maybe we can build a synthetic clock though from example:

if(Time[0]!=t.r){
t.r=Time[0];
redraw();

give us a hint! zzuegg :)

Reason: