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:

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? 

 
ddiall :

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? 

Ohoh ... Too broad a topic to tell in a nutshell like this ...

Almost every object will have its own graphic object, which allows displaying all its information about its "parent". Each object will be accessed from other objects - just like we now have access to different objects. All graphic objects will be able to interact with other objects. Each object will have its own type of graphical representation. There will be many types of graphic objects from which you can compose your own complex graphic objects. Each object will have the ability to interact with the mouse cursor and, accordingly, different reactions and handlers. As a result, it will be possible to create your own types of graphical representations for various objects and how they interact.

Reason: