How to modify second order

I open two orders for a buy position, one of them hits TP and one’s left open. How do I write an EA to modify the open trade to move SL to break even then begin trailing?

I’ve tried solving this for days, but it seems very tricky. I need help.

1 Like

Some of this will be just pseudocode to get the idea.
Assume you want essentially two trades with one lot each…but for this we will only open one.

order_symbol = EURUSD;
order_type = OP_BUY;
order_lots = 2.0;
order_open_price = 1.23456;  // <---------
order_slippage,
order_stop_loss,
order_take_profit,
order_comment = 1.23556; // < ------ OrderOpenPrice + 100 Points
order_magic_number,
order_expiration,
order_color); 


ticket = OrderSend(order_symbol,
order_type,
order_lots,
order_open_price,
order_slippage,
order_stop_loss,
order_take_profit,
order_comment,
order_magic_number,
order_expiration,
order_color); 

The OrderSend will place your order and you have two lots in this Buy position.

.....make a loop and find your order to test

if(order_lots >= 2 && Bid > = StringToDouble(OrderComment())
{
close_result = OrderClose(OrderTicket, 1.0, EURUSD, MODE_BID);  // note that this closed only 1.0 lots of the 2.0 placed, leaving 1.0 lots
}

// make another loop through the orders
if( OrderLots() <= 1.0 && OrderStopLoss() < OrderOpenPrice() )
{
modify_stop_loss = OrderOpenPrice();  // set this before we get to the modify
                                                               // use the same style to make the trailing stop

modify_result = OrderModify(
order_ticket,
modify_open_price,
modify_stop_loss,   // <--------------- this will be set to break even...
                                // after a winning half of the trade is removed
modify_take_profit,
modify_expiration,
order_color);
}

// at this point you have closed half in profit and are now putting the remaining half at break even
// from here you can do many kinds of trailing stops to check the delta between Points in Profit and distance below to move the Stop Loss to.

This is very simplified and may need to be adjusted for your exact needs.
If any of it helps, then use it but if any of it is more confusion than help, keep looking.

The MQL5.com site has some example code for both MT4 and MT5.
Google or other search could be helpful.
I know when I started this was all so hard and I failed to get any comprehension in at least 6 big pushes.
As it is, I still struggle to make it do what is in my mind.

Best wishes.