Русский 中文 Español Deutsch 日本語 Português
MQL5 vs QLUA - Why trading operations in MQL5 are up to 28 times faster?

MQL5 vs QLUA - Why trading operations in MQL5 are up to 28 times faster?

MetaTrader 5Examples | 21 September 2016, 09:20
20 661 0
MetaQuotes
MetaQuotes

Have you ever wondered how quickly your order is delivered to the exchange, how fast it is executed, and how much time your terminal needs in order to receive the operation result?

Meanwhile, there is a way to improve order execution quality by receiving a quicker response and by achieving a faster transaction speed.

September 12, 2016 we simultaneously run three speed tests on a real account with the "Otkrytie" broker in MetaTrader 5 build 1415 and Quik 7.2.23. Each test was aimed to measure specific speed characteristics, which is important for algorithmic trading:
  1. Testing of synchronous operations — a series of 10 synchronous consecutive Buy operations with a confirmation of a successful execution of each transaction on the exchange. The next operation is not executed until a response is received from the trade server confirming that the previous operation has or has not been executed on the exchange. The execution speed depends on the entire chain: the terminal — the trade server — the exchange — the trade server — the terminal. The smaller the average time of the synchronous trading operation, the better.
  2. Testing of asynchronous operations — a series of 10 asynchronous Buy operations without any confirmation of a successful transaction execution. It is a pure rate test, which measures how fast orders are sent to the exchange. The best terminal is the one that can execute 10 asynchronous Buy operations faster.
  3. Testing of order book update — measuring how fast orders in the Market Depth (order book) are updated. This is a simple counting of the number of ticks (updates) in the Market Depth per a unit of time. The more frequent quotes are received by the terminal from the exchange, the faster the order book will be updated. Therefore, the more ticks per second are received by the automated trading application, the quicker it can respond to changes in the market demand/supply structure. The best terminal is the one with the higher speed of order book update.


Test Conditions

The two terminals are installed on a hosted VPS in Moscow, like the trade servers of the "Otkryite" broker. Trading operations were performed using the same real account on Moscow Exchange's Derivatives Market; the traded instrument was Si-9.6.

We captured a video of the three tests for easier analysis:

  1. Trading operations were performed on the same real account
  2. The same financial instrument Si-9.16 was traded
  3. The same computer was used
  4. Trading operations were performed in the same time
  5. The same market conditions
  6. The speed of Market Depth (order book) update was measured on the same instrument in the same time
  7. The network latency to the broker's servers was 2 ms

Trading Operation Speed Results: MetaTrader 5 vs QUIK

The results of the three tests are featured in a summary table, additional detailed results of each test are available in further sections below.

Test
MetaTrader 5
QUIK
MT5 lead
The average time of a synchronous operation
9.59 ms277.80 ms28.96 times
The average time of an asynchronous operation0.09 ms0.30 ms 3.33 times
Market Depth update speed
42.7 times per second8.4 times per second5.08 times

As you can see, MetaTrader 5 shows much better results in all tests. You can reproduce the same tests using the attached source code. The testing process is demonstrated in the video below.


Trading Speed Comparison Video




Conclusion:  MetaTrader 5 performs trading operations up to 28 times faster than QUIK

These tests demonstrate that the MQL5 language is much faster than QLUA in performing trading operations on Moscow Exchange, as well as in scanning the Market Depth. In addition to performing calculations 50-600 times faster, trading robots in MQL5 also trade up to 28 times faster. Moreover, the entire arsenal of development tools is available without the need to use additional connectors, DLLs, etc. You can use ready trade classes from the Standard Library and a large number of articles on trading automation.

A robust trading robot must check trade sending results; i.e. it must wait for a response from the trade server. The tests have proven that MetaTrader 5 is significantly faster in performing synchronous operations. If you need to use asynchronous operations, you can do it 3 times faster with MetaTrader 5. Should you need to analyze the order book, MetaTrader 5 will provide you with an advantage of a 5-time faster quoting stream without snapshots.

These tests also prove that the MQL5 language is an ideal solution for creating fast performing automated trading systems. Even additional connectors or libraries for the QUIK terminal that are aimed to speed up calculations would not help much, since the bottleneck is the trade operation performance time.

Now let's analyze the boring yet important test details.


Detailed MetaTrader 5 vs QUICK Comparison Reports

An application in QLUA measures time by calling the operating system timer with the default timer resolution of 10…15.6 milliseconds (usually 15.6 ms). We had to improve the accuracy of time measurement in QLUA by simply reducing the system timer resolution to 1 ms. This is done by using the script SpeedupSystemTimer.mq5, which calls the timeBeginPeriod function of the Winmm.dll system library

#import "winmm.dll"
int timeBeginPeriod(uint per);
#import

void OnInit()
  {
   timeBeginPeriod(1);
  }

void OnTick()
  {
  }

We had run the script before starting tests in MetaTrader 5 additionally allowing DLL calls, which resulted in the operation measurement error in QUIK of no more than 1 ms.

The MQL5 language provides a ready function GetMicrosecondCount(), so the accuracy of speed measurement in the MetaTrader 5 terminal is 1 microsecond (1 millisecond=1000 microseconds).

#1 Synchronous Trading Operations Speed Test

The test implied measuring the speed of synchronous trading operations, i.e. a trading operation could only be executed after receiving a response from the trade server confirming that the previous operation was successfully executed with a complete confirmation from the exchange.

First, a series of trading operations was performed through the MetaTrader 5 terminal, then the same series was performed through the QUIK terminal.

The purpose of the tests was to measure the average time of 10 synchronous 1-lot market Buy operations:

  • SyncTradeTest.mq5
  • SyncTradeTest.lua

The time spent to execute a synchronous operation was measured as follows:

  • The MQL5 language provides the synchronous OrderSend function, and the time can be easily measured at the beginning and at the end of the trade series.

  • The QLUA language does not provide synchronous trading functions, and therefore the status of the transaction should be monitored separately. The time of operation start was recorded right before the call of sendTransaction() using the os.closck() function. A successful completion of the transaction was monitored in the built-in OnOrder() handler during the first call when the event about a trade execution on the exchange was received. The difference between these events is the time taken to execute a trading operation.
Measurement results show that MetaTrader 5 executed synchronous operations up to 28 times faster.


#2 Asynchronous Trading Operations Speed Test

This test is much easier. An order to buy one Si-9.16 futures contract was sent ten times in a row. This allowed us to measure the average time of the asynchronous transfer in QLUA with an accuracy of 1 ms / 10 = 0.10 ms. The MetaTrader 5 has no measurement error, while it uses a microsecond timer.

We did not wait for the operation result: sending an order to a trade server was immediately followed by sending the next order:

  • AsyncTradeTest.mq5
  • AsyncTradeTest.lua
The results showed that MetaTrader 5 executed asynchronous operations 3.33 times faster.


#3 Market Depth Update Test

Some trading strategies utilize the order book analysis. In MQL5, the Market Depth change event can be tracked in the OnBookEvent() handler; QLUA provides OnQuote() for that purpose.

The speed of Market Depth change was tested using the following programs, which are available in the attached ZIP:

  • MarketUpdateTest.mq5
  • MarketUpdateTest.lua

After running the programs in the two terminals, we recorded that the Market Depth in MetaTrader 5 was updated about 5 times more frequently. Probably, QUIK limits the frequency of order book updates and does not show some of the changes.

All the source code files are attached to the article, so anybody can reproduce these tests and check the results for themselves.


Why Is the Difference So Impressive?

We passionately strive for high performance and value every microsecond while constantly optimizing the trading platform for over years.

That is why we have achieved an amazing performance of our built-in algorithmic MQL5 language and an impressive trade transaction speed.

Translated from Russian by MetaQuotes Ltd.
Original article: https://www.mql5.com/ru/articles/2635

Attached files |
LifeHack for trader: "Quiet" optimization or Plotting trade distributions LifeHack for trader: "Quiet" optimization or Plotting trade distributions
Analysis of the trade history and plotting distribution charts of trading results in HTML depending on position entry time. The charts are displayed in three sections - by hours, by days of the week and by months.
Cross-Platform Expert Advisor: Orders Cross-Platform Expert Advisor: Orders
MetaTrader 4 and MetaTrader 5 uses different conventions in processing trade requests. This article discusses the possibility of using a class object that can be used to represent the trades processed by the server, in order for a cross-platform expert advisor to further work on them, regardless of the version of the trading platform and mode being used.
Portfolio trading in MetaTrader 4 Portfolio trading in MetaTrader 4
The article reveals the portfolio trading principles and their application to Forex market. A few simple mathematical portfolio arrangement models are considered. The article contains examples of practical implementation of the portfolio trading in MetaTrader 4: portfolio indicator and Expert Advisor for semi-automated trading. The elements of trading strategies, as well as their advantages and pitfalls are described.
MQL5 Cookbook - Trading signals of moving channels MQL5 Cookbook - Trading signals of moving channels
The article describes the process of developing and implementing a class for sending signals based on the moving channels. Each of the signal version is followed by a trading strategy with testing results. Classes of the Standard Library are used for creating derived classes.