• Get access

Discussion of article "Library for easy and quick development of MetaTrader programs (part XXVI): Working with pending trading requests - first implementation (opening positions)"

To add comments, please log in or register
MetaQuotes
Moderator
214761
MetaQuotes  

New article Library for easy and quick development of MetaTrader programs (part XXVI): Working with pending trading requests - first implementation (opening positions) has been published:

In this article, we are going to store some data in the value of the orders and positions magic number and start the implementation of pending requests. To check the concept, let's create the first test pending request for opening market positions when receiving a server error requiring waiting and sending a repeated request.

Compile and launch the EA. Turn off the Internet and wait till the following image appears in the lower right corner of the terminal:



After disabling the Internet and clicking Sell, the trade server returns the error and the following entries are displayed in the journal:

2019.11.26 15:34:48.661 CTrading::OpenPosition<uint,uint>: Invalid request:
2019.11.26 15:34:48.661 No connection with the trade server
2019.11.26 15:34:48.661 Correction of trade request parameters ...
2019.11.26 15:34:48.661 Trading attempt #1. Error: No connection with the trade server

After receiving the error, the library creates a pending request with the parameters set during the unsuccessful attempt to open a short position.
The pending request also features the number of attempts and the waiting time of 20 seconds.

Then enable the Internet to restore the connection to the trade server:


As soon as the connection is restored, the library starts handling a pending request sending it to the server. As a result, we have an open position with journal entries:

2019.11.26 15:35:00.853 CTrading::OpenPosition<double,double>: Invalid request:
2019.11.26 15:35:00.853 Trading is prohibited for the current account
2019.11.26 15:35:00.853 Correction of trade request parameters ...
2019.11.26 15:35:00.853 Trading operation aborted
2019.11.26 15:35:01.192 CTrading::OpenPosition<double,double>: Invalid request:
2019.11.26 15:35:01.192 Trading is prohibited for the current account
2019.11.26 15:35:01.192 Correction of trade request parameters ...
2019.11.26 15:35:01.192 Trading operation aborted
2019.11.26 15:35:01.942 - Position is open: 2019.11.26 10:35:01.660 -
2019.11.26 15:35:01.942 EURUSD Opened 0.10 Sell #486405595 [0.10 Market-order Sell #486405595] at price 1.10126, sl 1.10285, tp 1.09985, Magic number 17629307 (123), G1: 13
2019.11.26 15:35:01.942 OnDoEasyEvent: Position is open

As we can see, after restoring the connection to the trade server, trading for the current account was enabled with a delay.
But the pending request did the trick anyway
...

Also, we can see the real magic number 17629307 in the journal followed by the magic number defined in the EA settings in brackets (123) plus another entry G1: 13 informing that the first group ID is equal to 13, while the second group ID is absent — its value turned out to be equal to zero, therefore there was no second entry with the G2: XX second group ID.

Author: Artyom Trishkin

amrali
2251
amrali  
//--- Data location in the magic numberber int value
      //-----------------------------------------------------------
      //  bit   32|31       24|23       16|15        8|7         0|
      //-----------------------------------------------------------
      //  byte    |     3     |     2     |     1     |     0     |
      //-----------------------------------------------------------
      //  data    |   uchar   |   uchar   |         ushort        |
      //-----------------------------------------------------------
      //  descr   |pend req id| id2 | id1 |          magic        |
      //-----------------------------------------------------------      
//--- Return (1) the specified magic numberber, the ID of (2) the first group, (3) second group, (4) pending request from the magic numberber value
   ushort            GetMagicID(void)                                const { return ushort(this.Magic() & 0xFFFF);                                 }
   uchar             GetGroupID1(void)                               const { return uchar(this.Magic()>>16) & 0x0F;                                }
   uchar             GetGroupID2(void)                               const { return uchar((this.Magic()>>16) & 0xF0)>>4;                           }
   uchar             GetPendReqID(void)                              const { return uchar(this.Magic()>>24) & 0xFF;                                }

There exists a convenient function for extracting bits from a number.

//--- extract cnt bits at the kth bit position
   uint              _getBits(const uint number,uint pos,uint cnt=1) const { return (number >> pos) & ((1 << cnt) - 1);                            }
      
   ushort            GetMagicID(void)                                const { return ushort(_getBits(this.Magic(),0,16));                           }
   uchar             GetGroupID1(void)                               const { return uchar(_getBits(this.Magic(),16,4));                            }
   uchar             GetGroupID2(void)                               const { return uchar(_getBits(this.Magic(),20,4));                            }
   uchar             GetPendReqID(void)                              const { return uchar(_getBits(this.Magic(),24,8));                            }
Artyom Trishkin
Moderator
49636
Artyom Trishkin  
amrali:

There exists a convenient function for extracting bits from a number.

Thanks
To add comments, please log in or register