You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Broker's response
Apparently, you want to see the same time in local log as in exchange log. It's high time for developers to write not only local but also server time of event into log. Then synchronization is not needed.
However, you have sent a limit, not a local time, but server time. And server time was already, as if it was 10:00. So the server time was not synchronised with the exchange time.
Only, today, after 10:00 (synchronisation was in pre-market)
local time differed from the time in the Market Watch by 7 sec. :(
A new quote has come and the market has changed - these are different events. If a bid higher or lower than ask or bid is added (removed), OnBookEvent() event is received, but it is not a new quote and it does not get into tick history, hence the last known server time does not change.
I don't know how else to explain it, are you kidding? ))
Geez, that's right, I messed up, the quote may not be the best!
Then I should useSymbolInfoTick()
Thanks, Sergei!
Added
if(SymbolInfoTick(symbol, curr_tick))
//if(CopyTicks(symbol,curr_tick,COPY_TICKS_INFO,0,1)==1)
Apparently, you want to see the same time in local log as in exchange log. It is high time for developers to write not only local but also server time into log. Then synchronization is not needed.
However, you have sent a limit, not a local time, but server time. And server time was already set to 10:00 a.m., as if it was the open time. So, the server time was not synchronized with the exchange time.
The synchronization of time will not save you from[Market closed], the exchange can start trading later and suspend trading at trading time.
You have to take a different approach here.
Apparently, you want to see the same time in local log as in exchange log. It is high time for developers to write not only local but also server time into log. Then synchronization is not needed.
However, you have sent a limit, not a local time, but server time. And server time was already, as if it was 10:00. So, the server time was not synchronized with the exchange time.
And how can I get the server time?
I check the server time like this
//| Expert Check traiding time function |
//+------------------------------------------------------------------+
bool CheckTradingTime(MqlDateTime &tick_time)
{
datetime lk_time = TimeTradeServer(tick_time);
if ( ( tick_time.day_of_week == int(FirstDay)) ||
( tick_time.day_of_week == int(SecondDay)))//выходные
{
return(false);
}
#ifdef DEBUG
if ((tick_time.hour >= 0) && (tick_time.hour < 6)) // DEBUG 6-00
{
return(false);
}
#else
if ((tick_time.hour >= 0) && (tick_time.hour < 10))
{
return(false);
}
#endif
// 13 * 3600 + 59 * 60 + 30 = 50370 - 13:59:30
// 14 * 3600 = 50400 - 14:00:00
// 14 * 3600 + 30 = 50430 - 14:00:30
// 14 * 3600 + 60 = 50460 - 14:01:00
// 18 * 3600 + 44 * 60 + 30 = 67470 - 18:44:30
// 18 * 3600 + 45 * 60 = 67500 - 18:45:00
// 18 * 3600 + 45 * 60 + 30 = 67530 - 18:45:30
// 18 * 3600 + 46 * 60 = 67560 - 18:46:00
// 19 * 3600 = 68400 - 19:00:00
// 19 * 3600 + 60 = 68460 - 19:01:00
// 23 * 3600 + 49 * 60 + 30 = 85770 - 23:49:30
// 23 * 3600 + 50 * 60 = 85800 - 23:50:00
// 23 * 3600 + 50 * 60 + 30 = 85830 - 23:50:30
// 23 * 3600 + 51 * 60 = 85860 - 23:51:00
//---
ulong trade_time = tick_time.hour * 3600 + tick_time.min * 60 + tick_time.sec;
//--- //10:00:02
if(((trade_time >= time_st_mon) && (trade_time < 50370)) ||
((trade_time >= time_st_day) && (trade_time < 67470)) ||
((trade_time >= time_st_evn) && (trade_time < 85770)))
{
return(true);
}
return(false);
}
AndTimeTradeServer()
gives local time, and TimeCurrent() may have "old" time :(
But the difference in my approach and yours is huge.
In my case, using an EA for a low-liquid instrument (for example UUAH) you can add ALL the futures glasses and
it is GUARANTEED to obtain a tick in the premarket, without worrying about the work of other Expert Advisors in the terminal.
I.e. all new quotes will "go" to a separate EA.
Added by
It will "deal" only with synchronization and will not interfere with anyone else's work.
Also, we can receive ticks from other symbols into a separate EA. This must be done through custom events.
But I think it is unnecessary, at least, I don't need it.
It is also possible to receive ticks from other instruments in a separate EA, this should be done through custom events.
But I think it is unnecessary, at least, I don't need it.
And you should try mine (it is not liquidated).
Synchronisation before trading every day
//| Time_sync_forts.mq5 |
//| Copyright 2017 prostotrader |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017 prostotrader"
#property link "https://www.mql5.com"
#property version "1.01"
//---
struct _SYSTEMTIME
{
ushort wYear;
ushort wMonth;
ushort wDayOfWeek;
ushort wDay;
ushort wHour;
ushort wMinute;
ushort wSecond;
ushort wMilliseconds;
};
_SYSTEMTIME loc_time;
#import "kernel32.dll"
void GetLocalTime(_SYSTEMTIME &sys_time);
bool SetLocalTime(_SYSTEMTIME &sys_time);
#import
//---
bool is_sync;
string symb_rts;
string symb_si;
string symb_gold;
string symb_br;
string symb_gazr;
string symb_sbrf;
//
input string BrInstr = "BR-2.17"; //Brent
input ENUM_DAY_OF_WEEK FirstDay = SATURDAY; //Первый выходной
input ENUM_DAY_OF_WEEK SecondDay = SUNDAY; //Второй выходной
//+------------------------------------------------------------------+
//| Expert set second symbol function |
//+------------------------------------------------------------------+
string SetSecSymbol(const string a_symbol,const string prefix)
{
int str_tire=0;
ushort let_symbol;
int str_size=StringLen(a_symbol);
for(int i=0; i<str_size; i++)
{
let_symbol=StringGetCharacter(a_symbol,i);
if(let_symbol=='-')
{
str_tire=i;
break;
}
}
if(str_tire>0)
{
return(prefix + StringSubstr(a_symbol, str_tire, str_size - str_tire));
}
return("");
}
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
is_sync=false;
MarketBookAdd(Symbol());
//---
symb_rts=SetSecSymbol(Symbol(),"RTS");
if(!SymbolSelect(symb_rts,true))
{
MessageBox(symb_rts+" not found in the Market watch!","Error",MB_OK|MB_ICONERROR);
return(INIT_FAILED);
}
else MarketBookAdd(symb_rts);
//---
symb_si=SetSecSymbol(Symbol(),"Si");
if(!SymbolSelect(symb_si,true))
{
MessageBox(symb_si+" not found in the Market watch!","Error",MB_OK|MB_ICONERROR);
return(INIT_FAILED);
}
else MarketBookAdd(symb_si);
//---
symb_gold=SetSecSymbol(Symbol(),"GOLD");
if(!SymbolSelect(symb_gold,true))
{
MessageBox(symb_gold+" not found in the Market watch!","Error",MB_OK|MB_ICONERROR);
return(INIT_FAILED);
}
else MarketBookAdd(symb_gold);
//---
symb_gazr=SetSecSymbol(Symbol(),"GAZR");
if(!SymbolSelect(symb_gazr,true))
{
MessageBox(symb_gazr+" not found in the Market watch!","Error",MB_OK|MB_ICONERROR);
return(INIT_FAILED);
}
else MarketBookAdd(symb_gazr);
//---
symb_sbrf=SetSecSymbol(Symbol(),"SBRF");
if(!SymbolSelect(symb_sbrf,true))
{
MessageBox(symb_sbrf+" not found in the Market watch!","Error",MB_OK|MB_ICONERROR);
return(INIT_FAILED);
}
else MarketBookAdd(symb_sbrf);
//---
symb_br=BrInstr;
if(!SymbolSelect(symb_br,true))
{
MessageBox(symb_br+" not found in the Market watch!","Error",MB_OK|MB_ICONERROR);
return(INIT_FAILED);
}
else MarketBookAdd(symb_br);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
MarketBookRelease(Symbol());
MarketBookRelease(symb_rts);
MarketBookRelease(symb_br);
MarketBookRelease(symb_si);
MarketBookRelease(symb_gold);
MarketBookRelease(symb_gazr);
MarketBookRelease(symb_sbrf);
}
//+------------------------------------------------------------------+
//| Expert Convert To Time function |
//+------------------------------------------------------------------+
bool ConvertToTime(const long n_value,_SYSTEMTIME &a_time)
{
a_time.wMilliseconds=ushort(n_value%1000);
ulong new_time=ulong(double(n_value)/1000);
MqlDateTime cur_time = {0};
TimeToStruct(datetime(new_time),cur_time);
if(cur_time.year>0)
{
a_time.wDay=ushort(cur_time.day);
a_time.wDayOfWeek=ushort(cur_time.day_of_week);
a_time.wHour=ushort(cur_time.hour);
a_time.wMinute= ushort(cur_time.min);
a_time.wMonth = ushort(cur_time.mon);
a_time.wSecond= ushort(cur_time.sec);
a_time.wYear=ushort(cur_time.year);
return(true);
}
return(false);
}
//+------------------------------------------------------------------+
//| Expert On book event function |
//+------------------------------------------------------------------+
void OnBookEvent(const string &symbol)
{
loc_time.wYear=0;
GetLocalTime(loc_time);
if(loc_time.wYear>0)
{
if((int(loc_time.wDayOfWeek) == int(FirstDay)) ||
(int(loc_time.wDayOfWeek) == int(SecondDay))) return;
if((loc_time.wHour==9) && (loc_time.wMinute>=50) && (loc_time.wMinute<=59))
{
MqlTick curr_tick;
if(SymbolInfoTick(symbol, curr_tick))
//if(CopyTicks(symbol,curr_tick,COPY_TICKS_INFO,0,1)==1)
{
MqlDateTime sv_time;
TimeToStruct(curr_tick.time,sv_time);
if(!is_sync)
{
if((loc_time.wDayOfWeek==ushort(sv_time.day_of_week)) &&
(loc_time.wHour==ushort(sv_time.hour)))
{
long last_ping=long(NormalizeDouble((double(TerminalInfoInteger(TERMINAL_PING_LAST))/1000)/2,0));
long mls_time=long(curr_tick.time_msc%1000);
if((mls_time+last_ping)>999)
{
mls_time=long(curr_tick.time_msc)+last_ping;
if(!ConvertToTime(mls_time, loc_time)) return;
}
else
{
loc_time.wMinute = ushort(sv_time.min);
loc_time.wSecond = ushort(sv_time.sec);
loc_time.wMilliseconds=ushort(mls_time);
}
if(SetLocalTime(loc_time))
{
is_sync=true;
Print("Local time sync is done. Symbol = ", symbol, " Sync min = ", loc_time.wMinute,
" Sync sec = ", loc_time.wSecond, " Sync ms = ", loc_time.wMilliseconds);
}
}
}
}
}
else is_sync=false;
}
}
//+------------------------------------------------------------------+
Try mine (not eliminated)
The same code could be many times more succinct.
Of course it can, everyone has different abilities and capabilities.
It's interesting to see yours.
Ran this code in the demo
if(SymbolInfoTick(symb_gold, cur_tick))
{
int result = CopyTicks(symb_gold, ct_tick, COPY_TICKS_INFO, 0, 10);
if(result>0)
{
Print("SymbolInfoTick symbiol = ", symb_gold, " time = ", cur_tick.time, ".", cur_tick.time_msc%1000);
for(int i = result-1; i >=0; i--)
{
Print("CopyTicks symbol = ", symb_gold, " time ", string(i), " = ", ct_tick[i].time, ".", ct_tick[i].time_msc%1000);
}
}
}
and was a little puzzled by the result.
2017.01.21 00:07:41.444 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 9 = 2017.01.20 22:13:36.774
2017.01.21 00:07:41.444 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 8 = 2017.01.20 22:13:36.774
2017.01.21 00:07:41.444 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 7 = 2017.01.20 22:13:36.774
2017.01.21 00:07:41.444 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 6 = 2017.01.20 22:11:14.395
2017.01.21 00:07:41.444 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 5 = 2017.01.20 22:09:47.452
2017.01.21 00:07:41.444 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 4 = 2017.01.20 22:09:47.452
2017.01.21 00:07:41.444 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 3 = 2017.01.20 22:08:34.290
2017.01.21 00:07:41.444 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 2 = 2017.01.20 22:07:29.704
2017.01.21 00:07:41.444 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 1 = 2017.01.20 22:07:29.704
2017.01.21 00:07:41.444 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 0 = 2017.01.20 22:06:35.386
2017.01.21 00:08:28.961 Time_sync_forts (ED-3.17,M1) debugging terminated
It turns out that CopyTicks() doesn't save the last tick?
Added
if(SymbolInfoTick(symb_gold, cur_tick))
{
int result = CopyTicks(symb_gold, ct_tick, COPY_TICKS_TRADE, 0, 10);
if(result>0)
{
Print("SymbolInfoTick symbiol = ", symb_gold, " time = ", cur_tick.time, ".", cur_tick.time_msc%1000);
for(int i = result-1; i >=0; i--)
{
Print("CopyTicks symbol = ", symb_gold, " time ", string(i), " = ", ct_tick[i].time, ".", ct_tick[i].time_msc%1000);
}
}
}
Result
2017.01.21 00:15:54.670 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 9 = 2017.01.20 21:59:59.658
2017.01.21 00:15:54.670 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 8 = 2017.01.20 21:57:54.512
2017.01.21 00:15:54.670 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 7 = 2017.01.20 21:57:54.512
2017.01.21 00:15:54.670 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 6 = 2017.01.20 21:57:54.245
2017.01.21 00:15:54.670 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 5 = 2017.01.20 21:57:54.245
2017.01.21 00:15:54.670 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 4 = 2017.01.20 21:57:00.562
2017.01.21 00:15:54.670 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 3 = 2017.01.20 21:56:45.159
2017.01.21 00:15:54.670 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 2 = 2017.01.20 21:56:13.601
2017.01.21 00:15:54.670 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 1 = 2017.01.20 21:55:16.852
2017.01.21 00:15:54.670 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 0 = 2017.01.20 21:54:16.303
if(SymbolInfoTick(symb_gold, cur_tick))
{
int result = CopyTicks(symb_gold, ct_tick, COPY_TICKS_ALL, 0, 10);
if(result>0)
{
Print("SymbolInfoTick symbiol = ", symb_gold, " time = ", cur_tick.time, ".", cur_tick.time_msc%1000);
for(int i = result-1; i >=0; i--)
{
Print("CopyTicks symbol = ", symb_gold, " time ", string(i), " = ", ct_tick[i].time, ".", ct_tick[i].time_msc%1000);
}
}
}
Result
2017.01.21 00:17:55.966 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 9 = 2017.01.20 22:13:36.774
2017.01.21 00:17:55.966 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 8 = 2017.01.20 22:13:36.774
2017.01.21 00:17:55.966 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 7 = 2017.01.20 22:13:36.774
2017.01.21 00:17:55.966 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 6 = 2017.01.20 22:11:14.395
2017.01.21 00:17:55.966 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 5 = 2017.01.20 22:09:47.452
2017.01.21 00:17:55.966 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 4 = 2017.01.20 22:09:47.452
2017.01.21 00:17:55.966 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 3 = 2017.01.20 22:08:34.290
2017.01.21 00:17:55.966 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 2 = 2017.01.20 22:07:29.704
2017.01.21 00:17:55.966 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 1 = 2017.01.20 22:07:29.704
2017.01.21 00:17:55.966 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = GOLD-3.17 time 0 = 2017.01.20 22:06:35.386
Can anyone clarify?
Added
Same on RTS.
2017.01.21 00:21:11.589 Time_sync_forts (ED-3.17,M1) SymbolInfoTick symbiol = RTS-3.17 time = 2017.01.20 22:14:55.0
2017.01.21 00:21:11.589 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = RTS-3.17 time 9 = 2017.01.20 22:12:41.619
2017.01.21 00:21:11.589 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = RTS-3.17 time 8 = 2017.01.20 22:12:41.619
2017.01.21 00:21:11.589 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = RTS-3.17 time 7 = 2017.01.20 22:12:41.619
2017.01.21 00:21:11.589 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = RTS-3.17 time 6 = 2017.01.20 22:12:02.202
2017.01.21 00:21:11.589 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = RTS-3.17 time 5 = 2017.01.20 22:11:00.754
2017.01.21 00:21:11.589 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = RTS-3.17 time 4 = 2017.01.20 22:08:35.153
2017.01.21 00:21:11.589 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = RTS-3.17 time 3 = 2017.01.20 22:08:23.548
2017.01.21 00:21:11.589 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = RTS-3.17 time 2 = 2017.01.20 22:07:28.512
2017.01.21 00:21:11.589 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = RTS-3.17 time 1 = 2017.01.20 22:07:28.512
2017.01.21 00:21:11.589 Time_sync_forts (ED-3.17,M1) CopyTicks symbol = RTS-3.17 time 0 = 2017.01.20 22:07:28.512
Added by
And it's the same on the ref.
2017.01.21 00:44:21.895 Time_sync_forts (ALRS-3.17,H1) CopyTicks symbol = Si-3.17 time 9 = 2017.01.20 23:54:35.904
2017.01.21 00:44:21.895 Time_sync_forts (ALRS-3.17,H1) CopyTicks symbol = Si-3.17 time 8 = 2017.01.20 23:53:04.867
2017.01.21 00:44:21.895 Time_sync_forts (ALRS-3.17,H1) CopyTicks symbol = Si-3.17 time 7 = 2017.01.20 23:51:55.889
2017.01.21 00:44:21.895 Time_sync_forts (ALRS-3.17,H1) CopyTicks symbol = Si-3.17 time 6 = 2017.01.20 23:50:55.731
2017.01.21 00:44:21.895 Time_sync_forts (ALRS-3.17,H1) CopyTicks symbol = Si-3.17 time 5 = 2017.01.20 23:49:59.296
2017.01.21 00:44:21.895 Time_sync_forts (ALRS-3.17,H1) CopyTicks symbol = Si-3.17 time 4 = 2017.01.20 23:49:59.59
2017.01.21 00:44:21.895 Time_sync_forts (ALRS-3.17,H1) CopyTicks symbol = Si-3.17 time 3 = 2017.01.20 23:49:58.937
2017.01.21 00:44:21.895 Time_sync_forts (ALRS-3.17,H1) CopyTicks symbol = Si-3.17 time 2 = 2017.01.20 23:49:58.934
2017.01.21 00:44:21.895 Time_sync_forts (ALRS-3.17,H1) CopyTicks symbol = Si-3.17 time 1 = 2017.01.20 23:49:58.933
2017.01.21 00:44:21.895 Time_sync_forts (ALRS-3.17,H1) CopyTicks symbol = Si-3.17 time 0 = 2017.01.20 23:49:58.933
2017.01.21 00:44:24.151 Time_sync_forts (ALRS-3.17,H1) debugging terminated