Partial close with variable size position

 

Hi all, 

I am trying to partially close my position once a certain risk reward is reached. Both proportion of the position to be close, and the risk reward needed to be reached are inputs.

I am struggling regarding the volume, as I want the partial cloture to happen only once, so when the volume of the position is the initial opening volume. I am using a function to calculate the volume for each position, depending on the risk and the distance of the stoploss (which changes for each trades). 

Here is my code, which is currently closing all the position once the rr is reached for partial. Any one would have an idea how to get the partial close to only happen once? 

   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   bid = NormalizeDouble(bid, _Digits);
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);    
   ask = NormalizeDouble(ask, _Digits);
 

   if (Partial){
      for(int i=PositionsTotal()-1; i >= 0; i--){
         ulong ticket = PositionGetTicket(i);
         if(PositionSelectByTicket(posticket)){
            ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
            double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); 
            double currentTP = PositionGetDouble(POSITION_TP); 
            double slPosition = PositionGetDouble(POSITION_SL);
            double volume = PositionGetDouble(POSITION_VOLUME);
            double LotToClose = volume * PartialCloture;
            LotToClose = NormalizeDouble(LotToClose, 2);   
                  
            if (posType == POSITION_TYPE_BUY && bid > openPrice                     
            && ((bid - openPrice) >= (openPrice - slPosition) * PartialRR) ){       
               trade.PositionClosePartial(posticket, LotToClose); }
               
            if (posType == POSITION_TYPE_SELL && ask < openPrice 
            && ((openPrice - ask) >= (slPosition - openPrice) * PartialRR) ){
               trade.PositionClosePartial(posticket, LotToClose); }   
               }  
  }}

Thank you! 

 
Raphael A:

Hi all, 

I am trying to partially close my position once a certain risk reward is reached. Both proportion of the position to be close, and the risk reward needed to be reached are inputs.

I am struggling regarding the volume, as I want the partial cloture to happen only once, so when the volume of the position is the initial opening volume. I am using a function to calculate the volume for each position, depending on the risk and the distance of the stoploss (which changes for each trades). 

Here is my code, which is currently closing all the position once the rr is reached for partial. Any one would have an idea how to get the partial close to only happen once? 

Thank you! 

You can set a flag. FOR example you save that Level ( target price where you take partial profit) into a GlobalVariable(). You set a condition that if Variable> 0 and price is beyond your Level you close partially that trade. And after you partially close your trade, you set that variable to 0. Like this you will only take partial profit only once . Another way to do it is to move your SL to break even … and next time you test that… if SL has been moved means that profits already been taken … with the first version you can take profits several times at different levels … once you take first profit you can either set that variable to 0, or you can set new target for more profits … also, bid and ask prices no need to normalize…. LoTSize need to be rounded to lotstep… if you choose a symbol where lotstep is 0.1 your LotToClose normalization will fail..
 
Raphael A:

Hi all, 

I am trying to partially close my position once a certain risk reward is reached. Both proportion of the position to be close, and the risk reward needed to be reached are inputs.

I am struggling regarding the volume, as I want the partial cloture to happen only once, so when the volume of the position is the initial opening volume. I am using a function to calculate the volume for each position, depending on the risk and the distance of the stoploss (which changes for each trades). 

Here is my code, which is currently closing all the position once the rr is reached for partial. Any one would have an idea how to get the partial close to only happen once? 

Thank you! 

Logic:

if the position volume is equal to the initial Lots setting or Lot risk allocation then splitting is allowed.

if the position volume is less then the Lots setting or Lot risk allocation it has already been split.

That is the process for a single split.   


You can check per cent of initial Lots if you wanted two or more splits.  eg. if (position_volume  > 0.5 * Lots) split()   

 
Raphael A:

Hi all, 

I am trying to partially close my position once a certain risk reward is reached. Both proportion of the position to be close, and the risk reward needed to be reached are inputs.

I am struggling regarding the volume, as I want the partial cloture to happen only once, so when the volume of the position is the initial opening volume. I am using a function to calculate the volume for each position, depending on the risk and the distance of the stoploss (which changes for each trades). 

Here is my code, which is currently closing all the position once the rr is reached for partial. Any one would have an idea how to get the partial close to only happen once? 

Thank you! 

Just look in ...\MQL5\Include\Trade\Trade.mqh

for PositionClosePartial(..) to see how it is done correctly.

 
Raphael A: I want the partial cloture to happen only once,

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.) Can't be done in US brokers due to FIFO.
 

Thank you all

 

Forum on trading, automated trading systems and testing trading strategies

Partial close with variable size position

Raphael A, 2023.08.29 10:17

         ulong ticket = PositionGetTicket(i);
         if(PositionSelectByTicket(posticket)){

In these two lines you select different positions. Delete the second line.