Discussion of article "Orders, Positions, and Deals in MetaTrader 5"

 

New article Orders, Positions, and Deals in MetaTrader 5 is published:

Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.

Author: MetaQuotes

 

After reading, I have not found the best answer to this question.

How to load into the cache, for example, only 1 last order from the history (to complicate the task, let the last one by symbol).

For example, the advisor has different frequency of orders. It can make 30 orders per day and can be "silent" for several days or weeks.

There are variants after sending an order to the server to remember its ticket somewhere and then to retrieve this order by ticket - no problem. It is possible to memorise both in global variables and inside the EA logic. But here is the problem if, for example, the Expert Advisor connects to the account from another terminal and does not have a ticket of the last order.

I have to load history by means of HistorySelect. The end date is more or less clear, but how to calculate the initial date.

Because even in a small range can get several orders - not a problem, but not optimal in terms of loading unnecessary data. Or not one of them may not fall into the range.

You can take even smaller date range and loop (shift the range into history) to fill the cache - not optimal in terms of logic of work - extra loops, queries to the history database.

Proposal - to organise loading of the necessary quantity by orders/deals by instrument.

And more: if the purpose of the article was to show the optimal algorithms of access to orders/deals/positions. I think that: if we go further in terms of optimisation, it would be good to have a mode of loading data from history into the cache by fields for example, only tickets and the time of sending the order to the server are necessary, why then load all other data from this range to these orders (magick, comment and much more).

select * from HistiryOrder where a_date_send>@datestart and a_date_send<@dateend

Those who develop DBMS applications, I think such a query would not be approved for production use. True, there are situations when all the data are further involved in some actions, but this is an exception rather than a rule.


 
olyakish:

After reading, I have not found the best answer to this question.

How to load into the cache, for example, only 1 last order from the history (to complicate the task, let the last one by symbol).

For example, the advisor has different frequency of orders. It can make 30 orders per day and can be "silent" for several days or weeks.

There are variants after sending an order to the server to remember its ticket somewhere and then to retrieve this order by ticket - no problem. It is possible to memorise both in global variables and inside the EA logic. But here is the problem if, for example, the Expert Advisor connects to the account from another terminal and does not have a ticket of the last order.

I have to load history by means of HistorySelect. The end date is more or less clear, but how to calculate the initial date.

Because even in a small range can get several orders - not a problem, but not optimal in terms of loading unnecessary data. Or not one of them may not fall into the range.

You can take even smaller date range and loop (shift the range into history) to fill the cache - not optimal in terms of logic of work - extra loops, queries to the history database.

Proposal - to organise loading of the necessary quantity by orders/deals by instrument.

And more: if the purpose of the article was to show the optimal algorithms of access to orders/deals/positions. I think that: if we go further in terms of optimisation, it would be good to have a mode of loading data from history into the cache by fields for example, only tickets and the time of sending the order to the server are necessary, why then load all other data from this range to these orders (magick, comment and much more).

select * from HistiryOrder where a_date_send>@datestart and a_date_send<@dateend

Those who develop DBMS applications, I think such a query would not be approved for production use. True, there are situations when all the data are further involved in some actions, but this is an exception rather than a rule.


Loading and processing even a very large history will not take much time. Another thing is that if such loading is performed on every tick, it is already a problem.

Even a very large history will be processed in a few seconds. Hence the first conclusion is to reduce the number of full loads.

Start full loading of history in OnInit(). Remember the necessary dates. Then you can spin the story "like a gypsy with the sun" in OnTicket().

 
Urain:

Loading and processing even a very large history will not take much time. Another thing is that if such loading is performed on every tick, it is already a problem.

Even a very large history will be processed in a few seconds. Hence the first conclusion is to reduce the number of full loads.

Start full loading of history in OnInit(). Remember the necessary dates. After that you can spin the story "like a gypsy with the sun" in OnTicket().

To be more specific, the history should be fully loaded only in the initialisation block and on weekends (on weekends you should also optimise parameters).

 

One thing that confused me was that the text contains a lot of warnings about the need to load history into the cache sparingly (thoughtfully), but there is no real example of how to implement this task. This frankly upset me:

//--- set the initial boundary to 3 days ago
   datetime start=end-3*PeriodSeconds(PERIOD_D1);

Will this code (approach) really be used in any normal Expert Advisor? Unless we are talking about a specific simple task (for example, to find orders for several days), and then - with reservations (in this example, weekends are not taken into account, so the code is not suitable for processing several trading days), the approach does not stand up to any criticism.

Although, there are all the tools to implement economical loading. And, as far as I am concerned, an example of such implementation should be in the article.

For example, using this template:

  1. OnInit() - loading the entire history into the cache, searching for the last order significant for the Expert Advisor (for example, by meijik or just by instrument), saving its time into a variable.
  2. OnTrade() - update the history loaded into the cache (starting from the memorised time), update the time of the last order (if a new significant order has appeared).
  3. OnTick() - working with the current loaded cache or, if necessary, loading the cache from the remembered time.

This approach already claims to stability and universality. Besides - in terms of resource utilisation, it may turn out to be even more economical than "selecting the last 3 days".


Anyway, thanks again for the article. Such "specifications from developers" are necessary, otherwise there will be no normal code.

 

The article gives an example of loading trading history for one day (one code has an example of loading history for 3 days). Yes, this is a limitation and the example is not universal. But if the reader understands this peculiarity while reading the article, he/she will be able to decide for himself/herself the question - for what interval and from what moment he/she needs to load trading history into the cache.

The reader has received the simplest examples and algorithms and can now independently apply them in the necessary event processing functions. He can independently create his own trading history base and do its initialisation and synchronisation, and so on.

An attempt to give specific recipes and functions for optimal work with trading history for all cases will require at least one more article. More precisely, not the examples themselves, but approaches to solving certain tasks. This article was aimed at understanding how trading functions work and what nuances should be paid attention to so as not to waste your own time on research.

I am sure that after reading this article, everything will be simple.

 

Can you tell me if there is a description of terminal operation in OfLine mode somewhere? The thing is that I could not load the terminal due to lack of updating of chart data. I wanted to test it at work (there is no internet there), but I couldn't: charts are waiting for updating, and there is not a single symbol in the tester. The article says that after starting the terminal, data synchronisation with the server is performed. But what happens if there is no connection (actually it is not supposed). Maybe it makes sense to explicitly tell the terminal that it is supposed to work in OfLine and not to spin this unfortunate wheel. Perhaps there will be fewer failures for the tester's work. To be fair, it should be said that I haven't had this problem for a long time, but people complain about it on the forum. Maybe there are some tricks (well, there is some file to remove) to solve the situation (I tried - nothing helped until I established connection with the server at home).

Документация по MQL5: Получение рыночной информации / SymbolIsSynchronized
Документация по MQL5: Получение рыночной информации / SymbolIsSynchronized
  • www.mql5.com
Получение рыночной информации / SymbolIsSynchronized - Документация по MQL5
 

After transferring the terminal catalogue to new equipment, part of configuration databases (symbols, account settings, history of transactions, etc.) is specially deleted because it is encrypted by hardwired keys. The history of charts is not affected.

This means that after migration you need to connect to any trading account at least once to let the terminal adjust the market environment. After that you can erase the password, disconnect from the Internet and work with the tester offline.

 
Renat:

After transferring the terminal catalogue to new equipment, part of configuration databases (symbols, account settings, history of transactions, etc.) is specially deleted because it is encrypted by hardwired keys. The history of charts is not affected.

This means that after migration you need to connect to any trading account at least once to let the terminal adjust the market environment. After that you can erase the password, disconnect from the Internet and work with the tester offline.


I haven't moved or changed anything. I just came to work with my laptop and wanted to test the Expert Advisor. I have one account and naturally I tried to log in, but the log says that there is no connection with the server. Maybe it was just a random failure, but I could not do anything.

 
Erm955:

I didn't move or change anything. I just came to work with my laptop and wanted to test the EA. I have one account and naturally I tried to log in, but the log says that there is no connection with the server. Maybe just a random failure, but I could not do anything.

Please provide a full screenshot of the entire terminal window, including all Market Watch, chart and tester zones.

Try to connect to the account at home, pump out the data completely and run at least one test. Then disconnect from the internet, restart the terminal and try again.

 
Renat:

Please provide a full screenshot of the entire terminal window, including all Market Watch, chart and tester zones.

Try to connect to the account at home, fully download the data and run at least one test. After that disconnect from the internet, restart the terminal and try again.

This does not require anything as everything works as before. Apparently it was an accidental failure. Earlier the terminal asked for authorisation, now it starts without it. Checked about 10 times with and without restarting the computer. Everything is fine, thanks.