Close order partially to get desired profit

 

Hi,

I am trying to partially close some orders (2 or more) in a way that I can get some desired profit out of the trade...let me better explain with an example:

Say that I have 2 (or more BUY orders) ongoing, say that all orders are in profit of, for example 567$, and my risk is 100$, with this, we could say that profit is a bit above 5R at the moment.

Say that, with these condition I want to partially close all orders so that I get 1R out of the trade, while letting the rest run. I got some code working

void takeProfitOut(double partial) {
   double closePrice = (inBuyTrade) ? Bid : Ask;
   for( int i = 0 ; i < OrdersTotal() ; i++ ) {
      OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
      OrderClose(OrderTicket(), OrderLots()*(partial/total_current_R), closePrice,3);
   }
}

As you can see, here I am just trying to close the desired partial per order, based on the  total_current_R calculated on each tick.

Now, while this works, as I mentioned, it is far from accurate.

Was wondering if maybe you could suggest some improvements, or maybe a better approach to this.

Thanks

 
   double closePrice = (inBuyTrade) ? Bid : Ask;
OrderClose(OrderTicket(), OrderLots()*(partial/total_current_R), closePrice,3);
  1. You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot.

    You also must check if you have already done it, to avoid repeated closing. Alternatives:

    • Move SL to Break Even+1 before the partial close. That way, you know that you already did it.
    • Set a flag in persistent storage (files, global variables w/flush)
    • Open two orders initially, and close one (manually or by TP.)
  2. MT4: You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.

  3. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)

 

Also, I think that if you loop from 0 to OrdersTotal(), not very good ideea. Not sure about this, but I think that, when you partialy close order No 1 for example, terminal will completely close that order ( with that ticket number) and will open another order with the remaining of the lotsize, with a different ticket number, and it will be added to the end. I always loop from OrdersTotal() down to 0. I believe is better. 

If use roeder's suggestion with the stoploss moving, it might not be an issue, but if dont want to move the stoploss, and you want to use a flag for the entire partialClose(), to only run once, then it might be better to loop backwards.


With regards to closing percentage of each trade,  Fernando will give you some links about the basket closing of the trades. 


https://www.mql5.com/en/forum/435955#comment_43097301

Calculate how many lots to close in order to get a certain amount of profit/loss ?
Calculate how many lots to close in order to get a certain amount of profit/loss ?
  • 2022.11.07
  • www.mql5.com
Hello, I am building my EA with the purpose of "not closing" positions in loss...
Reason: