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:4800:03:48####################### OK: place ORDER_TYPE_SELL_STOP_LIMIT00:03:48127 eurusd 100000:03:48 penStpLimit Sell Mag: 100000:03:48 SELL-STOP with SL & TP r.ID: 0 d.O: 0 r.O: 2 r.ret: 1000900:06:33 order modified [#2sell stop limit 1 EURUSD at 0.99600 (0.99900)]
00:06:3300:06:33####################### OK: CHG ST || TP openOrders
00:06:33200 eurusd 00:06:33 MODIF ORD SL & TP r.ID: 1 d.O: 0 r.O: 0 r.ret: 1000900:08:53 failed close by 0.3 EURUSD at 0.99800 [Invalid request]
00:08:5300:08:53################ FAIL: ORDER_TYPE_CLOSE_BY########
00:08:53155ORDER_TYPE_CLOSE_BYOrderSendAsync error 4756 res.order 1001300:08:53 tC 2022.10.0500:08:53 ask: 0.9989000:08:53158struct REQUEST[0]:
00:08:53 action[5] TRADE_ACTION_PENDING00:08:53 Mag.Nr. 105000:08:53 Order Ticket 000:08:53Symbol EURUSD
00:08:53 Vol. 0.3000:08:53 Preis 0.9980000:08:53 StopLimit 0.0000000:08:53 SL 0.0000000:08:53 TP 0.0000000:08:53 Deviat. 000:08:53 Typ order[8] ORDER_TYPE_CLOSE_BY00:08:53 Typ exec ORDER_FILLING_FOK00:08:53 Typ Ablauffrist ORDER_TIME_GTC00:08:53 Zeit Ablauffrist 1970.01.0100:00:0000:08:53 Kommentar penTrailing TGT Mag: 100000:08:53 Position ticket 000:08:53 Ticket of opposite position 200:08:5300:08:53159struct RESULT[0]
00:08:53 retCode 1001300:08:53 Deal-Ticket 000:08:53 Order-Ticket 000:08:53 conf. Vol 0.0000:08:53 conf. Preis 0.0000000:08:53 akt.Bid (Requote Preis) 0.0000000:08:53 akt.Ask (Requote Preis) 0.0000000:08:53 Kommentar Invalid request
00:08:53 Term.ID 200:08:53 exter. RestCode 000:08:5305:47:56 order [#2sell stop limit 1 EURUSD at 0.99600 (0.99900)] triggered
09:37:58 order [#2sell limit 1 EURUSD at 0.99900] triggered
09:37:58 deal #2sell 1 EURUSD at 0.99903 done (based on order #2)
09:37:58 deal performed [#2sell 1 EURUSD at 0.99903]
09:37:58 order performed sell 1 at 0.99903 [#2sell limit 1 EURUSD at 0.99900]
09:37:58 failed close by 0.4 EURUSD at 0.99830 [Invalid request]
09:37:5809:37:58################ FAIL: POS _CLOSE_BY Pen.Ord. ########
09:37:58267ORDER_TYPE_CLOSE_BYOrderSendAsync error 4756 res.order 1001309:37:58 tC 2022.10.0509:37:58 ask: 0.9990609:37:58270struct REQUEST[0]:
09:37:58 action[5] TRADE_ACTION_PENDING09:37:58 Mag.Nr. 120009:37:58 Order Ticket 009:37:58Symbol EURUSD
09:37:58 Vol. 0.4009:37:58 Preis 0.9983009:37:58 StopLimit 0.0000009:37:58 SL 0.0000009:37:58 TP 0.0000009:37:58 Deviat. 009:37:58 Typ order[8] ORDER_TYPE_CLOSE_BY09:37:58 Typ exec ORDER_FILLING_FOK09:37:58 Typ Ablauffrist ORDER_TIME_GTC09:37:58 Zeit Ablauffrist 1970.01.0100:00:0009:37:58 Kommentar penTrailing TGT Mag: 009:37:58 Position ticket 009:37:58 Ticket of opposite position 209:37:5809:37:58271struct RESULT[0]
09:37:58 retCode 1001309:37:58 Deal-Ticket 009:37:58 Order-Ticket 009:37:58 conf. Vol 0.0009:37:58 conf. Preis 0.0000009:37:58 akt.Bid (Requote Preis) 0.0000009:37:58 akt.Ask (Requote Preis) 0.0000009:37:58 Kommentar Invalid request
09:37:58 Term.ID 309:37:58 exter. RestCode 009:37:5809:40:01 position modified [#2sell 1 EURUSD 0.99903 sl: 1.00102 tp: 0.99202]
09:40:0109:40:01####################### OK: CHG StTP Position
09:40:01314 eurusd 100009:40:01 MODIF POS SL & TP r.ID: 4 d.O: 0 r.O: 0 r.ret: 1000913:05:23 take profit triggered #2sell 1 EURUSD 0.99903 sl: 1.00102 tp: 0.99202 [#3buy 1 EURUSD at 0.99202]
13:05:23 deal #3buy 1 EURUSD at 0.99199 done (based on order #3)
13:05:23 deal performed [#3buy 1 EURUSD at 0.99199]
13:05:23 order performed buy 1 at 0.99199 [#3buy 1 EURUSD at 0.99202]
13:06:2513:06:252022.10.0513:06:25 Time to kill me..
13:06:2513:06:25 Printout of Deals
13:06:25 D[2] 3DEAL_ENTRY_OUT m 1000 o 3 p 2 R DEAL_REASON_TP T DEAL_TYPE_BUY t 2022.10.0513:05:2313:06:25 D[1] 2DEAL_ENTRY_IN m 1000 o 2 p 2 R DEAL_REASON_EXPERT T DEAL_TYPE_SELL t 2022.10.0509:37:5813:06:25ExpertRemove() 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 :(
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.
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:
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++:
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 :(