I don't fully understand why it's impossible to get around these CPU load lags. Probably incompetent to do so.
However, below is a clean test that shows the problem of price data relevance in MT5. I have provided the code with comments. Briefly, the ticks are taken via SymbolInfoTick/stack and checked against each other. Particularly, to avoid gaps and large lags between identical ticks from different sources.
// Демонстрация лагов OnTick и OnBookEvent.inputuint inMinInterval = 1000; // Минимальное время (в мкс.) лага// Структура тика, которую удобно будет выводить в ArrayPrint.struct PRICE
{
double bid;
double ask;
bool onTick; // true - источник OnTick+SymbolInfoTick, false - источник OnBookEvent+MarketBookGetulong Interval; // Временной интервал между соседними записями.void Set( constdouble &dBid, constdouble &dAsk, constbool bTick )
{
staticulong PrevTime = ::GetMicrosecondCount();
constulong NewTime = ::GetMicrosecondCount();
this.bid = dBid;
this.ask = dAsk;
this.onTick = bTick;
this.Interval = NewTime - PrevTime;
PrevTime = NewTime;
}
// Записи одинаковые, если обе соответствующие цены совпадают.booloperator ==( const PRICE &Price ) const
{
return((this.bid == Price.bid) && (this.ask == Price.ask));
}
};
PRICE Prices[1000]; // Массив для записи тиков из разных источников.int Amount = 0; // Количество записанных тиков// Возвращает bid/ask-цены из стакана.bool GetCurrentPrices( double &bid, double &ask )
{
MqlBookInfo Bands[];
constbool Res = MarketBookGet(_Symbol, Bands);
if (Res)
for (int i = ArraySize(Bands) - 2; i >= 0; i--)
if (Bands[i].type == BOOK_TYPE_SELL)
{
ask = Bands[i].price;
bid = Bands[i + 1].price;
break;
}
return(Res);
}
// Если bid или ask стакана поменялся - записываем их.bool SaveNewTick_Book()
{
staticdouble PrevBid = 0;
staticdouble PrevAsk = 0;
double bid;
double ask;
constbool Res = !IsStopped() && GetCurrentPrices(bid, ask) && ((PrevBid != bid) || (PrevAsk != ask));
if (Res)
{
PrevBid = bid;
PrevAsk = ask;
Prices[Amount++].Set(bid, ask, false);
if (Amount == ArraySize(Prices)) // Если достигли конца массива - выходим.ExpertRemove();
}
return(Res);
}
// Записываем bid и askbool SaveNewTick_Tick()
{
MqlTick Tick;
constbool Res = !IsStopped() && SymbolInfoTick(_Symbol, Tick);
if (Res)
{
Prices[Amount++].Set(Tick.bid, Tick.ask, true);
if (Amount == ArraySize(Prices)) // Если достигли конца массива - выходим.ExpertRemove();
}
return(Res);
}
// Проверка на наличие багов и задержек.void Check()
{
if (Amount > 3)
{
if ((Prices[Amount - 1].onTick == Prices[Amount - 2].onTick) && // Три подряд записи из одного источника - баг.
(Prices[Amount - 2].onTick == Prices[Amount - 3].onTick))
Alert("BUG!");
elseif ((Prices[Amount - 1] == Prices[Amount - 2]) && // Если цены подряд идущих записей совпадают
(Prices[Amount - 1].Interval > inMinInterval)) // И временной интервал между ними большой - информируем.
{
Alert((Prices[Amount - 1].onTick ? "OnTick-lag! - " : "OnBook-lag! - ") + (string)Prices[Amount - 1].Interval + " mcs.");
ArrayPrint(Prices, _Digits, NULL, Amount - 4, 4); // Выводим подробно проблему.
}
}
}
intOnInit()
{
return(!MarketBookAdd(_Symbol)); // Подписались на стакан.
}
voidOnBookEvent( conststring &Symb )
{
if ((Symb == _Symbol) && SaveNewTick_Book()) // Если записали новые цены из стакана,
Check(); // проверяем.
}
voidOnTick()
{
if (SaveNewTick_Tick()) // Если записали цены тика,
Check(); // проверяем.
}
voidOnDeinit( constint )
{
MarketBookRelease(_Symbol); // Отписались от стакана.ArrayPrint(Prices, _Digits, NULL, 0, Amount); // Вывод всех записей.
}
Result (only one MT5-b2656 running on the machine, CPU load around zero, one chart, Tester not used).
2020.10.2012:26:19.680 Test9 (AUDCAD,H1) Alert: OnTick-lag! - 9573 mcs.
2020.10.2012:26:19.680 Test9 (AUDCAD,H1) [bid] [ask] [onTick] [Interval]
2020.10.2012:26:19.680 Test9 (AUDCAD,H1) [0] 0.927770.92780true764782020.10.2012:26:19.680 Test9 (AUDCAD,H1) [1] 0.927770.92780false642020.10.2012:26:19.680 Test9 (AUDCAD,H1) [2] 0.927770.92781false505522020.10.2012:26:19.680 Test9 (AUDCAD,H1) [3] 0.927770.92781true9573// В OnTick пришел тик на 9 мс позже, чем он был в стакане.
...
2020.10.2012:26:33.899 Test9 (AUDCAD,H1) Alert: OnBook-lag! - 22153 mcs.
2020.10.2012:26:33.899 Test9 (AUDCAD,H1) [bid] [ask] [onTick] [Interval]
2020.10.2012:26:33.899 Test9 (AUDCAD,H1) [0] 0.927760.92783true3584032020.10.2012:26:33.899 Test9 (AUDCAD,H1) [1] 0.927760.92783false23612020.10.2012:26:33.899 Test9 (AUDCAD,H1) [2] 0.927760.92784true22155062020.10.2012:26:33.899 Test9 (AUDCAD,H1) [3] 0.927760.92784false22153// В стакан пришел тик на 22 мс позже, чем он был в OnTick.
The SymbolInfoTick brakes are closed. The upshot is this.
If the CPU is overloaded (like Optimize not even on all cores), SymbolInfoTick may take up to tens of milliseconds to complete. There is no answer why this most popular function in the Terminal doesn't snapshot regularly. If possible, make snapshots of it. And do not forget that even at zero CPU load the OnTick function may work tens of milliseconds later than a tick that comes to the Terminal.
All in all, rather sad, but it can't worry those who trade primitively.
The SymbolInfoTick brakes are closed. The upshot is this.
If the CPU is overloaded (like Optimize not even on all cores), SymbolInfoTick may take up to tens of milliseconds to complete. There is no answer why this most popular function in the Terminal doesn't snapshot regularly. If possible, make snapshots of it. And do not forget that even at zero CPU load the OnTick function may work tens of milliseconds later than a tick that comes to the Terminal.
All in all, quite unfortunate, but it cannot bother those who trade primitively.
For those who trade "not primitive": the hardware has to be up to the task.
"not even on all cores" - are you talking about "6/8"? i.e. 6 processes, and each of them 100% loads one CPU core for the duration of the test? And there are only 4 physical cores? And sincerely surprised that on such background the test "lags"?
If this is indeed your level of understanding, then: "Learn, learn and learn again".
If you want to rape CPU with 16-20 threads - buy CPU with 20 physical cores at least.
For those trading "not primitive": the hardware has to be up to the task.
"not even on all cores" - are you talking about "6/8"? I mean 6 processes, and each of them 100% loads one CPU core for the whole test time? And there are only 4 physical cores? And sincerely surprised that on such background the test "lags"?
If this is indeed your level of understanding, then: "Learn, learn and learn again".
If you want to rape CPU with 16-20 threads - buy CPU with 20 physical cores at least.
I'm sure I can prove that getting your current prices is very slow. The CPU load creates such sluggishness only because of incorrect implementation of the most important function in MQL5.
I'm sure I can prove that your implementation of getting the current prices is very slow. The CPU load only creates such slows down due to incorrect implementation of the main function in MQL5.
Test code:
voidOnTick()
{
MqlTick Tick;
ulong gstart=GetMicrosecondCount();
int count=10000;
for(int i=0; i<count; i++)
{
SymbolInfoTick(_Symbol, Tick);
}
ulong gend=GetMicrosecondCount()-gstart;
Print(count," iterations, total time ", DoubleToString(gend/1000.0,3)," ms; avr time: ",DoubleToString(gend/1000.0/count,3)," ms");
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
How do you do that?
Just reproduced the brakes when 6/8 Agents were running.
Just reproduced the brakes when 6/8 Agents were running.
I.e. problem when CPU is loaded.
MQ-Demo, 20 charts, b2656. Only Terminal is running on the machine, Tester is not running.
Just reproduced the brakes when 6/8 Agents were running.
I.e. the problem is when the CPU is loaded.
Is this new information for you?
Is this new information for you?
I don't fully understand why it's impossible to get around these CPU load lags. Probably incompetent to do so.
However, below is a clean test that shows the problem of price data relevance in MT5. I have provided the code with comments. Briefly, the ticks are taken via SymbolInfoTick/stack and checked against each other. Particularly, to avoid gaps and large lags between identical ticks from different sources.
Result (only one MT5-b2656 running on the machine, CPU load around zero, one chart, Tester not used).
Please confirm the playback.
Intermediate total for a fast unloaded machine.
Question to the Developers.
Suppose SymbolInfoTick was executed for 5 ms. Will the tick correspond to the current time, or 5 ms before that?
The SymbolInfoTick brakes are closed. The upshot is this.
If the CPU is overloaded (like Optimize not even on all cores), SymbolInfoTick may take up to tens of milliseconds to complete. There is no answer why this most popular function in the Terminal doesn't snapshot regularly. If possible, make snapshots of it. And do not forget that even at zero CPU load the OnTick function may work tens of milliseconds later than a tick that comes to the Terminal.
All in all, rather sad, but it can't worry those who trade primitively.
The SymbolInfoTick brakes are closed. The upshot is this.
If the CPU is overloaded (like Optimize not even on all cores), SymbolInfoTick may take up to tens of milliseconds to complete. There is no answer why this most popular function in the Terminal doesn't snapshot regularly. If possible, make snapshots of it. And do not forget that even at zero CPU load the OnTick function may work tens of milliseconds later than a tick that comes to the Terminal.
All in all, quite unfortunate, but it cannot bother those who trade primitively.
For those who trade "not primitive": the hardware has to be up to the task.
"not even on all cores" - are you talking about "6/8"? i.e. 6 processes, and each of them 100% loads one CPU core for the duration of the test? And there are only 4 physical cores? And sincerely surprised that on such background the test "lags"?
If this is indeed your level of understanding, then: "Learn, learn and learn again".
If you want to rape CPU with 16-20 threads - buy CPU with 20 physical cores at least.
For those trading "not primitive": the hardware has to be up to the task.
"not even on all cores" - are you talking about "6/8"? I mean 6 processes, and each of them 100% loads one CPU core for the whole test time? And there are only 4 physical cores? And sincerely surprised that on such background the test "lags"?
If this is indeed your level of understanding, then: "Learn, learn and learn again".
If you want to rape CPU with 16-20 threads - buy CPU with 20 physical cores at least.
I'm sure I can prove that getting your current prices is very slow. The CPU load creates such sluggishness only because of incorrect implementation of the most important function in MQL5.
I'm sure I can prove that your implementation of getting the current prices is very slow. The CPU load only creates such slows down due to incorrect implementation of the main function in MQL5.
Test code:
Prove it.