Discussion of article "Library for easy and quick development of MetaTrader programs (part XXXIV): Pending trading requests - removing and modifying orders and positions under certain conditions" - page 2

 
Artyom Trishkin:
Yes, they will. Because there the approach to storing your account data has been changed. This is written at the end of the article as a warning:
-----
For your information:
since we changed the structure of the account object (changed the size of the uchar arrays to store the string properties of the account and added one more integer property), all previously saved files of the account objects will no longer be loaded correctly. If they are in the common folder of terminals in the \Files\DoEasy\Accounts\ directory, then they must all be deleted before starting this test advisor - they will be recreated when switching from one account to another with a new size of the object structure.
-----

I delete all Account BIN files in the common folder to be sure your code load completely. it loads with no error about `CAccount::Load` (but main problem was wrong event ignoring and still exists)

Then I opened some trades manually and close them by a script as fast as possible. I got this error again:

CEventsCollection::CreateNewEvent, Line 768: This event already in the list

we know those events were from different position closing in MT4, but `engine` code can not understand it. Is the engine able to understand multiple trade close / events in a short time at all?


Here is all log messages:

2020.08.21 21:23:50.604 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.21 21:23:50.600 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.21 19:51:47.000 -

AUDCAD Closed Buy #573142748 at price 0.94405, Profit -0.22 USD

2020.08.21 21:23:50.368 Script CloseTradesFast4Test AUDCAD,H1: removed

2020.08.21 21:23:50.364 CloseTradesFast4Test AUDCAD,H1: uninit reason 0

2020.08.21 21:23:50.364 CloseTradesFast4Test AUDCAD,H1: close #573142747 buy 0.01 AUDCAD at 0.94404 at price 0.94375

2020.08.21 21:23:50.148 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.21 21:23:50.146 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.21 19:51:47.000 -

AUDCAD Closed Buy #573142748 at price 0.94405, Profit -0.22 USD

2020.08.21 21:23:49.942 CloseTradesFast4Test AUDCAD,H1: close #573142748 buy 0.01 AUDCAD at 0.94405 at price 0.94376

2020.08.21 21:23:49.725 mhdbzr-34 AUDCAD,H1: CEventsCollection::CreateNewEvent, Line 768: This event already in the list

2020.08.21 21:23:49.520 CloseTradesFast4Test AUDCAD,H1: close #573142752 buy 0.01 AUDCAD at 0.94406 at price 0.94376

2020.08.21 21:23:49.257 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.21 21:23:49.255 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.21 19:51:52.000 -

AUDCAD Closed Sell #573142755 at price 0.94366, Profit -0.37 USD

2020.08.21 21:23:49.086 CloseTradesFast4Test AUDCAD,H1: close #573142755 sell 0.01 AUDCAD at 0.94366 at price 0.94414

2020.08.21 21:23:48.815 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.21 21:23:48.812 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.21 19:51:53.000 -

AUDCAD Closed Sell #573142757 at price 0.94366, Profit -0.37 USD

2020.08.21 21:23:48.661 CloseTradesFast4Test AUDCAD,H1: close #573142756 sell 0.01 AUDCAD at 0.94366 at price 0.94415

2020.08.21 21:23:48.352 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.21 21:23:48.350 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.21 19:51:53.000 -

AUDCAD Closed Sell #573142757 at price 0.94366, Profit -0.37 USD

2020.08.21 21:23:48.237 CloseTradesFast4Test AUDCAD,H1: close #573142757 sell 0.01 AUDCAD at 0.94366 at price 0.94415

2020.08.21 21:23:47.537 CloseTradesFast4Test AUDCAD,H1: initialized

2020.08.21 21:23:47.520 Script CloseTradesFast4Test AUDCAD,H1: loaded successfully

2020.08.21 21:23:31.762 Automated trading enabled

 
Mohammad Bazrkar :

I delete all Account BIN files in the common folder to be sure your code load completely. it loads with no error about `CAccount::Load` (but main problem was wrong event ignoring and still exists)

Then I opened some trades manually and close them by a script as fast as possible. I got this error again:

CEventsCollection::CreateNewEvent, Line 768: This event already in the list

we know those events were from different position closing in MT4, but `engine` code can not understand it. Is the engine able to understand multiple trade close / events in a short time at all?

Please describe step by step what you are doing to get this error.

 
Artyom Trishkin:

Please describe step by step what you are doing to get this error.

 I modified your EA code to only get events. (I attached the modified version in previous messages, it is named `mhdbzr-34`)

  1. run the EA to start watching.
  2. open some buy/sell trades in demo account with same lots, fast. (by clicking fast on one-click buy/sell button on the chart)
  3. run a script to close those trades as fast as possible. (my script name is `CloseTradesFast4Test`)
  4. check log messages, you see the engine missed some position closing probably with that red error message I wrote here.
 
Mohammad Bazrkar :

 I modified your EA code to only get events. (I attached the modified version in previous messages, it is named `mhdbzr-34`)

  1. run the EA to start watching.
  2. open some buy/sell trades in demo account with same lots, fast. (by clicking fast on one-click buy/sell button on the chart)
  3. run a script to close those trades as fast as possible. (my script name is ` CloseTradesFast4Test `)
  4. check log messages, you see the engine missed some position closing probably with that red error message I wrote here.

Give your script to close positions.

 
Artyom Trishkin:

Give your script to close positions.

here is the code:

void OnStart()
{
   for(int i=OrdersTotal()-1; i>-1; i--)
   {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ continue; }
      if(OrderSymbol()!= _Symbol){ continue;}
      //--
      RefreshRates();
      int res = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 100);
   }
}
 

here is another test; this time it didn't recognize closed positions, it missed them with previous ones in two cases.

2020.08.27 20:30:18.880 Expert mhdbzr-34 AUDCAD,H1: removed

2020.08.27 20:30:18.869 mhdbzr-34 AUDCAD,H1: uninit reason 1

2020.08.27 20:28:35.617 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.27 20:28:35.617 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.27 20:28:35.617 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.27 18:58:03.000 - AUDCAD Closed Buy #573721560 at price 0.95218, Profit -2.29 USD

2020.08.27 20:28:35.614 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.27 18:58:04.000 - AUDCAD Closed Buy #573721562 at price 0.95218, Profit -2.29 USD

2020.08.27 20:28:35.582 Script CloseTradesFast4Test AUDCAD,H1: removed

2020.08.27 20:28:35.577 CloseTradesFast4Test AUDCAD,H1: uninit reason 0

2020.08.27 20:28:35.577 CloseTradesFast4Test AUDCAD,H1: close #573721560 buy 0.10 AUDCAD at 0.95218 at price 0.95188

2020.08.27 20:28:35.177 CloseTradesFast4Test AUDCAD,H1: close #573721562 buy 0.10 AUDCAD at 0.95218 at price 0.95188

2020.08.27 20:28:35.161 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.27 20:28:35.158 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.27 18:58:06.000 - AUDCAD Closed Sell #573721573 at price 0.95190, Profit -2.06 USD

2020.08.27 20:28:34.739 CloseTradesFast4Test AUDCAD,H1: close #573721571 buy 0.10 AUDCAD at 0.95224 at price 0.95191

2020.08.27 20:28:34.695 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.27 20:28:34.691 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.27 18:58:06.000 - AUDCAD Closed Sell #573721573 at price 0.95190, Profit -2.06 USD

2020.08.27 20:28:34.306 CloseTradesFast4Test AUDCAD,H1: close #573721573 sell 0.10 AUDCAD at 0.95190 at price 0.95217

2020.08.27 20:28:34.180 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.27 20:28:34.177 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.27 18:58:09.000 - AUDCAD Closed Sell #573721582 at price 0.95190, Profit -2.14 USD

2020.08.27 20:28:33.883 CloseTradesFast4Test AUDCAD,H1: close #573721577 sell 0.10 AUDCAD at 0.95190 at price 0.95217

2020.08.27 20:28:33.664 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position closed

2020.08.27 20:28:33.660 mhdbzr-34 AUDCAD,H1: - Position closed: 2020.08.27 18:58:09.000 - AUDCAD Closed Sell #573721582 at price 0.95190, Profit -2.14 USD

2020.08.27 20:28:33.440 CloseTradesFast4Test AUDCAD,H1: close #573721582 sell 0.10 AUDCAD at 0.95190 at price 0.95218

2020.08.27 20:28:32.946 CloseTradesFast4Test AUDCAD,H1: initialized

2020.08.27 20:28:32.928 Script CloseTradesFast4Test AUDCAD,H1: loaded successfully

2020.08.27 20:28:09.413 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position opened

2020.08.27 20:28:09.411 mhdbzr-34 AUDCAD,H1: - Position opened: 2020.08.27 18:58:09.000 - AUDCAD Opened 0.10 Sell #573721582 [0.10 Market-order Sell #573721582] at price 0.95190

2020.08.27 20:28:07.664 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position opened

2020.08.27 20:28:07.659 mhdbzr-34 AUDCAD,H1: - Position opened: 2020.08.27 18:58:07.000 - AUDCAD Opened 0.10 Sell #573721577 [0.10 Market-order Sell #573721577] at price 0.95190

2020.08.27 20:28:06.755 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position opened

2020.08.27 20:28:06.752 mhdbzr-34 AUDCAD,H1: - Position opened: 2020.08.27 18:58:06.000 - AUDCAD Opened 0.10 Sell #573721573 [0.10 Market-order Sell #573721573] at price 0.95190

2020.08.27 20:28:05.911 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position opened

2020.08.27 20:28:05.909 mhdbzr-34 AUDCAD,H1: - Position opened: 2020.08.27 18:58:05.000 - AUDCAD Opened 0.10 Buy #573721571 [0.10 Market-order Buy #573721571] at price 0.95224

2020.08.27 20:28:04.587 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position opened

2020.08.27 20:28:04.582 mhdbzr-34 AUDCAD,H1: - Position opened: 2020.08.27 18:58:04.000 - AUDCAD Opened 0.10 Buy #573721562 [0.10 Market-order Buy #573721562] at price 0.95218

2020.08.27 20:28:04.101 mhdbzr-34 AUDCAD,H1: OnDoEasyEvent: Position opened

2020.08.27 20:28:04.099 mhdbzr-34 AUDCAD,H1: - Position opened: 2020.08.27 18:58:03.000 - AUDCAD Opened 0.10 Buy #573721560 [0.10 Market-order Buy #573721560] at price 0.95218

2020.08.27 20:27:59.245 mhdbzr-34 AUDCAD,H1: initialized

2020.08.27 20:27:59.244 mhdbzr-34 AUDCAD,H1: Account 13278180: mhdbzr test (Alpari) 5537.01 USD, 1:100, Hedge, Demo account MetaTrader 4

2020.08.27 20:27:49.794 Expert TestDoEasy\Part34\mhdbzr-34 AUDCAD,H1: loaded successfully

I opened those trades manually on live market in demo account, not using tester environment. (and closed with the script as I did before)

(here is the screenshot to be sure it happened)

 
Mohammad Bazrkar :

here is another test; this time it didn't recognize closed positions, it missed them with previous ones in two cases .

I opened those trades manually on live market in demo account , not using tester environment. (and closed with the script as I did before)

(here is the screenshot to be sure it happened)

I am looking for the cause of the problem. It won't be fast. Thanks for the tests.

 
Artyom Trishkin:

I am looking for the cause of the problem. It won't be fast. Thanks for the tests.

(here is my opinion)

those closed positions are in the same second, one of them is handled in previous tick time, so in new tick, it has `new_history_orders=1` but two positions in the `list` and sorted probably wrong, because two items have same close time in second.

            //--- Get the list of closed positions (EventsCollection.mqh lines 205-230)
            CArrayObj* list=this.GetListHistoryPositions(list_history);
            if(list!=NULL)
              {
               //--- Sort the new list by a position close time
               list.Sort(SORT_BY_ORDER_TIME_CLOSE);
               //--- Take the number of positions equal to the number of newly closed positions from the end of the list in a loop (the last N events)
               int total=list.Total(), n=new_history_orders;
               for(int i=total-1; i>WRONG_VALUE && n>0; i--,n--)
                 {
                  //--- Receive a position from the list. If this is a position, search for opening order data and set a trading event
                  COrder* position=list.At(i);
                  if(position!=NULL && position.Status()==ORDER_STATUS_HISTORY_ORDER)
                    {
                     //--- If there is a control order of a closed position
                     COrderControl* ctrl=this.GetOrderControlByTicket(list_control,position.Ticket());
                     if(ctrl!=NULL)
                       {
                        //--- Set (1) a type of an order that led to opening a position and a (2) position ID, and create a position closure event
                        this.m_type_first=(ENUM_ORDER_TYPE)ctrl.TypeOrder();
                        this.m_position_id=position.Ticket();
                        this.CreateNewEvent(position,list_history,list_market,list_control);
                       }
                    }
                 }
              }

if the order is already in the list and throwing error message, then it delete position object and resume counting in this for loop, but I think it should not count n, when order has been delete inside the CreateNewEvent function, to catch next valid position.

also we need to understand recently handled positions, to skip wrong sorted orders and not counting down for `n`.

 
Mohammad Bazrkar :

(here is my opinion)

those closed positions are in the same second, one of them is handled in previous tick time, so in new tick, it has ` new_history_orders =1` but two positions in the ` list ` and sorted probably wrong, because two items have same close time in second.

if the order is already in the list and throwing error message, then it delete position object and resume counting in this for loop, but I think it should not count n, when order has been delete inside the CreateNewEvent function , to catch next valid position.

also we need to understand recently handled positions, to skip wrong sorted orders and not counting down for `n`.

Thanks, I'll figure it out.

 

Hello Artyom, I remember you mentioning this future graphical shell the first time I read these articles some weeks ago... Can you provide more details about your vision for this and what it will allow us to do in practice?