Add time_msc in MqlTIck

 

Hey,

I was adding ticks to my custom symbol through CustomTicksAdd() which is adding successfully when time_msc = 0 (default)

But when I try to add time_msc with milliseconds/microseconds, it returns incorrect tick data errors

How to feed milliseconds with time_msc

Kindly Help!

Here's My Code

void AddTick(double priceData){
   ArrayResize(tickData,(int)tickData.Size() + 1,0);
   int location = (int)tickData.Size() - 1;
   tickData[location].bid = priceData;
   tickData[location].ask = priceData;
   tickData[location].time = TimeLocal();
   tickData[location].time_msc = 0;
   
   int resultAdd = CustomTicksAdd("SBI_NEW",tickData,1);
   Print("RESELT : ",resultAdd," PRICE : ",priceData);
   ArrayFree(tickData);
}
 
nithinpiratez: I was adding ticks to my custom symbol through CustomTicksAdd() which is adding successfully when time_msc = 0 (default). But when I try to add time_msc with milliseconds/microseconds, it returns incorrect tick data errors. How to feed milliseconds with time_msc.Kindly Help! Here's My Code

Then show us the code that actually fails when you add with milliseconds (not microseconds). Showing us instead the code that works, does not help.

EDIT: Also, using the current Local Time does not seem to the best option. You may need to explain why you are doing that.

 
Fernando Carreiro #:

Then show us the code that actually fails when you add with milliseconds (not microseconds). Showing us instead the code that works, does not help.

EDIT: Also, using the current Local Time does not seem to the best option. You may need to explain why you are doing that.

My main objective is to add custom ticks every 500 milliseconds (2 data's per second)

This is the only code. Only thing which I really want is...I need to provide milliseconds for the tick data which I have inserted.

If I add 2 data per second without inserting milliseconds then the tick data will be replaced with the previous data because the default millisecond is always 000

I want something like this

tickData[location].time_msc = (long)
GetMicroSeconds() ; // Anyway this not gonna work because I need to pass milliseconds. I have also tried converting this to  milliseconds (Dividing by 1000)

I hope this makes more sense. If not kindly let me know!

 
nithinpiratez #: My main objective is to add custom ticks every 500 milliseconds (2 data's per second). This is the only code. Only thing which I really want is...I need to provide milliseconds for the tick data which I have inserted. If I add 2 data per second without inserting milliseconds then the tick data will be replaced with the previous data because the default millisecond is always 000.I want something like this. I hope this makes more sense. If not kindly let me know!

There is no such MQL function called "GetMicroSeconds". Where are you getting that from?

Also, you should net be using the Local Time. Use the time and the milliseconds from the source of the tick data.

 
Fernando Carreiro #:

There is no such MQL function called "GetMicroSeconds". Where are you getting that from?

Also, you should net be using the Local Time. Use the time and the milliseconds from the source of the tick data.

Sorry, I have just typed that function here. Actually its GetMicrosecondCount().

I get data from my own python server. 

If I use TimeCurrent(), the milliseconds are generated automatically. But I don't want the server time for my custom symbol. I want local time of my system.

 
nithinpiratez #: Sorry, I have just typed that function here. Actually its GetMicrosecondCount(). I get data from my own python server. If I use TimeCurrent(), the milliseconds are generated automatically. But I don't want the server time for my custom symbol. I want local time of my system.

As per the documentation, GetMicrosecondCount() returns the number of microseconds that have elapsed since the start of MQL5-program.

So it will obviously fail even if you convert to milliseconds because it will not be aligned with local time that you are using for the other field. The two fields must be consistent.

If you are getting data from your Python server, then have it provide the time, either from the source, or generated in a consistent form.

Documentation on MQL5: Common Functions / GetMicrosecondCount
Documentation on MQL5: Common Functions / GetMicrosecondCount
  • www.mql5.com
GetMicrosecondCount - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

As per the documentation, GetMicrosecondCount() returns the number of microseconds that have elapsed since the start of MQL5-program.

So it will obviously fail even if you convert to milliseconds because it will not be aligned with local time that you are using for the other field. The two fields must be consistent.

If you are getting data from your Python server, then have it provide the time, either from the source, or generated in a consistent form.

I got it. For Example...I can get the time from my server as "2022.11.16 06:34:54"

I can convert that time to datetime format in mql5. But what about the milliseconds that I have to pass?

Even If I get the milliseconds from python. How should I pass it through time_msc?

Kindly Help!

 
nithinpiratez #:

I got it. For Example...I can get the time from my server as "2022.11.16 06:34:54"

I can convert that time to datetime format in mql5. But what about the milliseconds that I have to pass?

Even If I get the milliseconds from python. How should I pass it through time_msc?

Kindly Help!

You must maintain the order of time in the custom symbol . 

Other than that 

long timestamp=((long)TimeLocal())*1000

will produce a millisecond timestamp , with 000 essentially in milliseconds from a datetime structure .

Then , your concern is the milliseconds count . An example : 

You assume a starting timestamp in milliseconds and then you know it's error based on the seconds of the local time , so you allow it to adjust ending up with a custom milliseconds timestamp 

bool synced=false,has_timer=false;
int ms=0;
datetime start_time=0;
uint desired_interval_ms=100;
//test timestamp
long mstimestamp=0;
long millis=0;
int OnInit()
  {
//--- create timer
  mstimestamp=0;
  millis=0;
  has_timer=false;synced=false;
  has_timer=EventSetMillisecondTimer(desired_interval_ms);
  if(!has_timer){return(INIT_FAILED);}
  start_time=TimeLocal();
  //capture custom timestamp for the first time 
  mstimestamp=((long)start_time)*1000;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
  //add to the custom timestamp based on the interval  
    mstimestamp+=desired_interval_ms;
  //in theory this timestamp should match the local time , so we can detect deviations as timer progresses
    long ltms=(((long)TimeLocal())*1000);
    millis=mstimestamp-ltms;
  //so when the above is negative we know we are off
    if(millis<0){
    Comment("Not synced\n"+TimeToString(TimeLocal(),TIME_MINUTES|TIME_SECONDS)+"."+IntegerToString(millis)+"\n"+IntegerToString(ltms)+"\n"+IntegerToString(mstimestamp));
    mstimestamp-=millis;
    }else if(millis>=1000){
    Comment("Not synced\n"+TimeToString(TimeLocal(),TIME_MINUTES|TIME_SECONDS)+"."+IntegerToString(millis)+"\n"+IntegerToString(ltms)+"\n"+IntegerToString(mstimestamp));
    mstimestamp-=millis;
    }
    else{
    Comment("Synced\n"+TimeToString(TimeLocal(),TIME_MINUTES|TIME_SECONDS)+"."+IntegerToString(millis)+"\n"+IntegerToString(ltms)+"\n"+IntegerToString(mstimestamp));   
    }
  }  
 
Lorentzos Roussos #:

You must maintain the order of time in the custom symbol . 

Other than that 

will produce a millisecond timestamp , with 000 essentially in milliseconds from a datetime structure .

Then , your concern is the milliseconds count . An example : 

You assume a starting timestamp in milliseconds and then you know it's error based on the seconds of the local time , so you allow it to adjust ending up with a custom milliseconds timestamp 

Thank you so much! It works.

Finally can you please explain me how to add hours to datetime variable for adding in MqlTick time...

This is the final code which works.

void AddTick(double priceData){
   timestamp += adder;
   ArrayResize(tickData,(int)tickData.Size() + 1,0);
   int location = (int)tickData.Size() - 1;
   tickData[location].bid = priceData;
   tickData[location].ask = priceData;
   tickData[location].time = TimeLocal();
   tickData[location].time_msc = timestamp;
   
   int resultAdd = CustomTicksAdd("SBI_NEW",tickData,1);
   Print("RESELT : ",resultAdd," PRICE : ",priceData);
   ArrayFree(tickData);
}

For Example : If I want to add hours to this TimeLocal() means...How to do that...

tickData[location].time = TimeLocal();

I have tried using PeriodSeconds(PERIOD_H1) It adds the time but I think it's not supported by MqlTick..

It throws error. How to do that!

By the way...Thanks for your help for milliseconds!

 
nithinpiratez #:

Thank you so much! It works.

Finally can you please explain me how to add hours to datetime variable for adding in MqlTick time...

This is the final code which works.

For Example : If I want to add hours to this TimeLocal() means...How to do that...

I have tried using PeriodSeconds(PERIOD_H1) It adds the time but I think it's not supported by MqlTick..

It throws error. How to do that!

By the way...Thanks for your help for milliseconds!

you turn to int add hours*3600 seconds and back to datetime . 

What error are you getting , it must be tick related .

(The datetime is a seconds timestamp essentially)

datetime time=TimeLocal();
int hours=3;
time=(datetime)(((int)time)+hours*3600);
 

Forum on trading, automated trading systems and testing trading strategies

Acceptance of SL/TP orders

fxsaber, 2020.12.11 09:17

#include <WinAPI\WinAPI.mqh>

long TimeLocalMsc()
{
  SYSTEMTIME SystemTime;
  
  kernel32::GetLocalTime(SystemTime);

  MqlDateTime time;
  
  time.year = SystemTime.wYear;
  time.mon = SystemTime.wMonth;
  time.day = SystemTime.wDay;
  time.hour = SystemTime.wHour;
  time.min = SystemTime.wMinute;
  time.sec = SystemTime.wSecond;


  return((StructToTime(time) * 1000 + SystemTime.wMilliseconds));
}


tickData[location].time_msc = TimeLocalMsc();
Reason: