Download MetaTrader 5

Identify remaining order of an OrderCloseBy request - page 2

To add comments, please log in or register

My online help in MetaEditor is telling me something else!

int GetLastError( )

The function returns the last occurred error, then the value of special last_error variable where the last error code is stored will be zeroized. So, the next call for GetLastError() will return 0.

then update ur help file unless u using old ver of MT4
Ian Venner
Ian Venner  

Dirk I did some testing and I found the order close times and the order open time of the new order do match in tester, were you testing your code on live trades or in tester ? Actually scratch that question I can see from your ticket numbers you did not do that in tester. So yes that has to means you cant use the times to identify the order in live trading.

Here is my test code, meant for the tester only.

I used explicit ticket numbers because we know tester will create ticket numbers sequentially starting with 1.

EA Code is designed to trade 2 tickets then wait a few bars (so time will be different from open time) then OrderCloseby() then cease trading.

  void OnTick()
 {static bool traded = 0;
  static bool closedby = 0;
  static int ticket1;
  static int ticket2;
  if(traded == 0)
   ticket1 = OrderSend(Symbol(),0,1.0,Ask,0,Bid - 5000*Point,Bid + 5000*Point,"",777,0,Gold);
   ticket2 = OrderSend(Symbol(),1,2.0,Bid,0,Ask + 5000*Point,Ask - 5000*Point,"",777,0,Gold);
   if(OrderSelect(ticket1,SELECT_BY_TICKET)) Print("ticket1 open time = ",OrderOpenTime());
   if(OrderSelect(ticket2,SELECT_BY_TICKET)) Print("ticket2 open time = ",OrderOpenTime());
   traded = 1;
  if(closedby == 0)
   {if(OrderMagicNumber() == 777)
    {if(iBarShift(Symbol(),0,OrderOpenTime()) > 4)
      if(OrderSelect(3,SELECT_BY_TICKET)) Print("ticket3 open time = ",OrderOpenTime());
      if(OrderSelect(2,SELECT_BY_TICKET)) Print("ticket2 close time = ",OrderCloseTime());
      if(OrderSelect(1,SELECT_BY_TICKET)) Print("ticket1 close time = ",OrderCloseTime());
      closedby = 1;

Tester Journal Output:

2014.02.19 02:34:45.618 2014.01.02 23:59 Tester: order #3 is closed
2014.02.19 02:34:45.578 2014.01.02 04:00 Test OrderCloseBy EURUSD,H1: ticket1 close time = 1388635200
2014.02.19 02:34:45.578 2014.01.02 04:00 Test OrderCloseBy EURUSD,H1: ticket2 close time = 1388635200
2014.02.19 02:34:45.578 2014.01.02 04:00 Test OrderCloseBy EURUSD,H1: ticket3 open time = 1388635200
2014.02.19 02:34:45.578 2014.01.02 04:00 Test OrderCloseBy EURUSD,H1: order #1 buy was closed by order #2
2014.02.19 02:34:45.508 2014.01.01 23:00 Test OrderCloseBy EURUSD,H1: ticket2 open time = 1388617200
2014.02.19 02:34:45.508 2014.01.01 23:00 Test OrderCloseBy EURUSD,H1: ticket1 open time = 1388617200
2014.02.19 02:34:45.486 2014.01.01 23:00 Test OrderCloseBy EURUSD,H1: open #2 sell 2.00 EURUSD at 1.37550 sl: 1.42580 tp: 1.32580 ok
2014.02.19 02:34:45.453 2014.01.01 23:00 Test OrderCloseBy EURUSD,H1: open #1 buy 1.00 EURUSD at 1.37580 sl: 1.32550 tp: 1.42550 ok



In tester that might be like you wrote, but in real life it must be different.

IMHO in real life, the remaining order must have the same opening price as the original (partially closed) order and therefore it must also be the same opening time. Otherwise price and time does not fit together.

Take a look at the opening prices of ticket1 and ticket3 - they must be the same. Otherwise the profits are not calculated as expected.

If they differ, there is a problem in the tester.

Example (without spread):

You buy 3 lots @ 1000 and sell 1 lot @ 1005. Order close by each other.

Afterwards you close the remaining order (2 lots) @ 1010.

Profit for 1 lot is 5.

Profit for 2 lots is 10 each.

Total profit: 25

This is how i expect it.

But when open time of the remaining order is set to close time of the OrderCloseBy, the remaining order open price must be also the price at this moment (1005)

Now we have

Profit for 1 lot is 5.

Profit for 2 lots is 5 each.

Total Profit: 15

And this is not what i expect.

Best regards


Alain Verleyen
Alain Verleyen  


Please EDIT your post and use the SRC button when you post code. Thank you.


hi i will like to commend the coders in this forum and would like to give them a thumbs up. i have a little issue with this ordercloseby function,i have followed the laid out steps but something is not right.

here is my code:

Last_Price=Bid,ticketb=buy ticket and tickets=sell ticket just incase.

 int count = 0;
bool CloseTicket=false;
bool ModifyTicket=false;

for (int i = 0; i < OrdersTotal(); i++)
if (!OrderSelect(i, SELECT_BY_POS)) continue;
if (OrderSelect(i, SELECT_BY_POS))
case OP_BUY:
if(Bid < Last_Price) CloseTicket=OrderCloseBy(tickets,ticketb,CLR_NONE);
if(Ask < OrderOpenPrice()) ModifyTicket=OrderModify(OrderTicket(), Ask, 0, OrderTakeProfit(), OrderExpiration(), Red);
case OP_SELL:
if(Bid > Last_Price) CloseTicket=OrderCloseBy(ticketb,tickets,CLR_NONE);
if(Bid > OrderOpenPrice()) ModifyTicket=OrderModify(OrderTicket(), Bid, 0, OrderTakeProfit(), OrderExpiration(), Blue);



This is build 646 of metaeditor coding. I attach to backtest but i don't get any errors at all and my trades get filled very well on backtest and goes as planned but when i attached to Live testing(Demo forward test),i see errors in my expert log "invalid ticket for ordercloseby function". where in the pasted code did i go wrong? If it can be rectified,please help me........... i am still learning mql4 coding. Thanks a lot in advance....



Please EDIT your post and use the SRC button when you post code. Thank you.

I have updated he code now to using the SRC. Please look into it and help me..........

Thanks in advance


here are my trades i manually use ordercloseby function in ordermodify tab of my mt4. Cos of this "invalid ticket for ordercloseby function",my ea doesn't respond nor remember its ticket number.

146161842014.05.16 16:50:12buy1.00de309620.000.000.002014.05.16 16:50:539622.500.
146165182014.05.16 16:50:13sell0.00de309619.500.000.002014.05.16 16:50:539619.500.
146165202014.05.16 16:50:15buy1.00de309620.500.000.002014.05.16 16:50:539624.500.
146165482014.05.16 16:50:23buy1.00de309620.800.000.002014.05.16 16:50:539624.500.
146165452014.05.16 16:50:24sell0.00de309619.800.000.002014.05.16 16:50:539619.800.
146165522014.05.16 16:50:26buy1.00de309621.000.000.002014.05.16 16:50:539627.800.
146165562014.05.16 16:50:28buy1.00de309621.000.000.002014.05.16 16:50:539628.
146165662014.05.16 16:50:31sell0.00de309621.800.000.002014.05.16 16:50:539621.800.
146165692014.05.16 16:50:32buy1.00de309622.800.000.002014.05.16 16:51:109632.300.
146165792014.05.16 16:50:33sell0.00de309621.800.000.002014.05.16 16:50:539621.800.
146165772014.05.16 16:50:33sell0.00de309621.500.000.002014.05.16 16:50:539621.500.
146165802014.05.16 16:50:34buy1.00de309622.800.000.002014.05.16 16:51:109631.500.
146165902014.05.16 16:50:36sell0.00de309622.500.000.002014.05.16 16:50:539622.500.
146165922014.05.16 16:50:36buy1.00de309623.500.000.002014.05.16 16:51:109632.
146165952014.05.16 16:50:36sell0.00de309622.500.000.002014.05.16 16:50:539622.500.
146166012014.05.16 16:50:38buy1.00de309623.800.000.002014.05.16 16:51:109632.
146166022014.05.16 16:50:39sell0.00de309624.500.000.002014.05.16 16:50:539624.500.
146166132014.05.16 16:50:41sell0.00de309624.500.000.002014.05.16 16:50:539624.500.
146166162014.05.16 16:50:44buy1.00de309626.000.000.002014.05.16 16:51:109632.300.
146166272014.05.16 16:50:47sell0.00de309627.800.000.002014.05.16 16:50:539627.800.
146166322014.05.16 16:50:47sell0.00de309628.000.000.002014.05.16 16:50:539628.
146166402014.05.16 16:50:50buy1.00de309627.800.000.002014.05.16 16:51:109632.500.
To add comments, please log in or register