CLOSE_BY ..?

 

I refer to the article: https://www.mql5.com/en/articles/2299 (from 2016):

New trading constants

Due to the addition of the new Close By operation type, the new trading properties have appeared as well:

  • TRADE_ACTION_CLOSE_BY — new trading operation type — close a position by an opposite one.
  • ORDER_TYPE_CLOSE_BY — new order type — close a position by an opposite one.
  • ORDER_POSITION_BY_ID — new order propertiy — ticket of an opposite position used for closing the current one.
  • DEAL_ENTRY_OUT_BY — new deal type — close a position by an opposite one.

My goal (without any specific trading strategy in my mind, but just to see and check what might work) is to place a pending order that partially closes another pending order (after it will have become an open position of course) or to close an open position with a pending order.

SL and TP of a position always close the whole position. It is possible to (partially) close an open position partially with another open position, see Trade.mqh.

Now I would like to have placed pending orders that (partially) close the open position and that I could push up and down depending on the market development - as I said, an academic scenario.

Attached is a test EA tuned on the period D'2022.10.05 00:00' to D'2022.10.06 00:00' and EURUSD so that the StopLimit order does not fail on prices.

There is a nice profit, but I know the price trend - honi soit qui mal y pense ;)


Here is the debugger journal 'designed' in N++:

00:03:48   sell stop limit 1 EURUSD at 0.99600 (0.99900) (0.99846 / 0.99880)
00:03:48   
00:03:48   #######################  OK: place ORDER_TYPE_SELL_STOP_LIMIT
00:03:48   127  eurusd 1000  00:03:48  penStpLimit Sell Mag: 1000
00:03:48        SELL-STOP with SL & TP  r.ID: 0  d.O: 0  r.O: 2  r.ret: 10009


00:06:33   order modified [#2 sell stop limit 1 EURUSD at 0.99600 (0.99900)]
00:06:33   
00:06:33   #######################  OK: CHG ST || TP openOrders
00:06:33   200  eurusd 00:06:33    MODIF ORD SL & TP  r.ID: 1  d.O: 0  r.O: 0  r.ret: 10009



00:08:53   failed close by 0.3 EURUSD at 0.99800 [Invalid request]
00:08:53   
00:08:53   ################  FAIL: ORDER_TYPE_CLOSE_BY  ########
00:08:53   155  ORDER_TYPE_CLOSE_BY OrderSendAsync error 4756 res.order 10013
00:08:53   tC 2022.10.05 00:08:53 ask: 0.99890
00:08:53   158 struct REQUEST[0]:
00:08:53   action[5] TRADE_ACTION_PENDING
00:08:53   Mag.Nr. 1050
00:08:53   Order Ticket 0
00:08:53   Symbol EURUSD
00:08:53   Vol. 0.30
00:08:53   Preis 0.99800
00:08:53   StopLimit 0.00000
00:08:53   SL 0.00000
00:08:53   TP 0.00000
00:08:53   Deviat. 0
00:08:53   Typ order[8] ORDER_TYPE_CLOSE_BY
00:08:53   Typ exec ORDER_FILLING_FOK
00:08:53   Typ  Ablauffrist ORDER_TIME_GTC
00:08:53   Zeit Ablauffrist 1970.01.01 00:00:00
00:08:53   Kommentar penTrailing TGT Mag: 1000
00:08:53   Position ticket 0
00:08:53   Ticket of opposite position 2
00:08:53   
00:08:53   159 struct RESULT[0]
00:08:53   retCode 10013
00:08:53   Deal-Ticket 0
00:08:53   Order-Ticket 0
00:08:53   conf. Vol 0.00
00:08:53   conf. Preis 0.00000
00:08:53   akt.Bid (Requote Preis) 0.00000
00:08:53   akt.Ask (Requote Preis) 0.00000
00:08:53   Kommentar Invalid request
00:08:53   Term.ID 2
00:08:53   exter. RestCode 0
00:08:53   


05:47:56   order [#2 sell stop limit 1 EURUSD at 0.99600 (0.99900)] triggered


09:37:58   order [#2 sell limit 1 EURUSD at 0.99900] triggered
09:37:58   deal #2 sell 1 EURUSD at 0.99903 done (based on order #2)
09:37:58   deal performed [#2 sell 1 EURUSD at 0.99903]
09:37:58   order performed sell 1 at 0.99903 [#2 sell limit 1 EURUSD at 0.99900]
09:37:58   failed close by 0.4 EURUSD at 0.99830 [Invalid request]
09:37:58   
09:37:58   ################  FAIL: POS _CLOSE_BY Pen.Ord. ########
09:37:58   267  ORDER_TYPE_CLOSE_BY OrderSendAsync error 4756 res.order 10013
09:37:58   tC 2022.10.05 09:37:58 ask: 0.99906
09:37:58   270 struct REQUEST[0]:
09:37:58   action[5] TRADE_ACTION_PENDING
09:37:58   Mag.Nr. 1200
09:37:58   Order Ticket 0
09:37:58   Symbol EURUSD
09:37:58   Vol. 0.40
09:37:58   Preis 0.99830
09:37:58   StopLimit 0.00000
09:37:58   SL 0.00000
09:37:58   TP 0.00000
09:37:58   Deviat. 0
09:37:58   Typ order[8] ORDER_TYPE_CLOSE_BY
09:37:58   Typ exec ORDER_FILLING_FOK
09:37:58   Typ  Ablauffrist ORDER_TIME_GTC
09:37:58   Zeit Ablauffrist 1970.01.01 00:00:00
09:37:58   Kommentar penTrailing TGT Mag: 0
09:37:58   Position ticket 0
09:37:58   Ticket of opposite position 2
09:37:58   
09:37:58   271 struct RESULT[0]
09:37:58   retCode 10013
09:37:58   Deal-Ticket 0
09:37:58   Order-Ticket 0
09:37:58   conf. Vol 0.00
09:37:58   conf. Preis 0.00000
09:37:58   akt.Bid (Requote Preis) 0.00000
09:37:58   akt.Ask (Requote Preis) 0.00000
09:37:58   Kommentar Invalid request
09:37:58   Term.ID 3
09:37:58   exter. RestCode 0
09:37:58   


09:40:01   position modified [#2 sell 1 EURUSD 0.99903 sl: 1.00102 tp: 0.99202]
09:40:01   
09:40:01   #######################   OK: CHG StTP Position
09:40:01   314  eurusd 1000  09:40:01    MODIF POS SL & TP  r.ID: 4  d.O: 0  r.O: 0  r.ret: 10009



13:05:23   take profit triggered #2 sell 1 EURUSD 0.99903 sl: 1.00102 tp: 0.99202 [#3 buy 1 EURUSD at 0.99202]
13:05:23   deal #3 buy 1 EURUSD at 0.99199 done (based on order #3)
13:05:23   deal performed [#3 buy 1 EURUSD at 0.99199]
13:05:23   order performed buy 1 at 0.99199 [#3 buy 1 EURUSD at 0.99202]
13:06:25   
13:06:25   2022.10.05 13:06:25  Time to kill me..
13:06:25   
13:06:25   Printout of Deals
13:06:25   D[2] 3 DEAL_ENTRY_OUT m 1000 o 3 p 2 R DEAL_REASON_TP T DEAL_TYPE_BUY t 2022.10.05 13:05:23
13:06:25   D[1] 2 DEAL_ENTRY_IN m 1000 o 2 p 2 R DEAL_REASON_EXPERT T DEAL_TYPE_SELL t 2022.10.05 09:37:58
13:06:25   ExpertRemove() function called

My question: is it possible to use pending orders as kind of SL & TP (how? I failed what ever I tried) to close positions partially?

Or are the 'new' options from 2016 just for queries to the trade history?

PS: Unfortunately MQ makes no difference in their English documentation: order can mean a pending order or any order send to the server :(

MetaTrader 5 features hedging position accounting system
MetaTrader 5 features hedging position accounting system
  • www.mql5.com
In order to expand possibilities of retail Forex traders, we have added the second accounting system — hedging. Now, it is possible to have multiple positions per symbol, including oppositely directed ones. This paves the way to implementing trading strategies based on the so-called "locking" — if the price moves against a trader, they can open a position in the opposite direction.
Files:
Reason: