Close partial profit after tot pips in gain

 

Hi!


Good evening!


I have a small code problem that is easy to solve, but slightly complicated for me. My idea is to close half the lots, when the price reaches half the profit pips of my stoploss.

Ex: If the stoploss is 12 pips and my trade goes into profit by 6 pips, to close half the lots and leave the remaining half of the lots reaching the TakeProfit or Trailing Stop.


extern int PipsInProfit = 1;
double lotx = OrderLots();


if(OrderProfit() > OrderOpenPrice()+PipsInProfit){
if (OrderClose(OrderTicket(),(lotx/2),OrderClosePrice(),0,clrYellow))return;
    }
   
   return;
}

This is the piece of code I wrote to activate the Profit Split but obviously it doesn't work, initially I used an oscillator based condition, but now I just want the profit split to happen after the price has gone into gain after n'pips.

 
  1. MT4: You can not use any Trade Functions until you first select an order. (OrderLots, OrderProfit, etc.)

  2. OrderProfit ($10) has nothing to do with OrderOpenPrice (1.2345) Perhaps you want Mathabs( OrderClosePrice() - OrderOpenPrice() ) /PIP > PipsInProfit

  3. 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.

  4. 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)