Position gets closed entirely when it should be a partial close (PositionClosePartial - Ctrade)

 

Hello guys,

I have an expert advisor running in the market from an hedge account of Icmarkets. I am struggling with a situation which I can not solve, despite the research I have already made online:

During an open position, at some point I move my stop loss to break-even and try to partially close a certain amount of volume, using PositionClosePartial operation. Well, sometimes it works properly but in most of the cases, it closes the entire position... It makes no sense. At least, either it should always work or not at all.

Moreover, the weirdest thing is when I backtest all theses trades in the strategy tester, all of the trades are correctly closed partially, as written in the EA.

Here is my code:

 for(int i=PositionsTotal()-1; i>=0; i--)
    {

      ulong Position_Magic = PositionGetInteger(POSITION_MAGIC);
      ulong PositionTicket = PositionGetTicket(i);
      double Position_open_price = PositionGetDouble(POSITION_PRICE_OPEN);
      double Position_SL_price = PositionGetDouble(POSITION_SL);
      double Position_TP_price = PositionGetDouble(POSITION_TP);
      ulong Position_Type = PositionGetInteger(POSITION_TYPE);
      double Current_Price = PositionGetDouble(POSITION_PRICE_CURRENT); 
      double Position_Volume = PositionGetDouble(POSITION_VOLUME);
      long Position_Identifier = PositionGetInteger(POSITION_IDENTIFIER);
      
      double HalphTP1_Buy = (Position_open_price + (Position_open_price - Position_SL_price));
      double HalphTP1_Sell = (Position_open_price - (Position_SL_price - Position_open_price));

        if ((Position_Type == POSITION_TYPE_BUY) && (Current_Price >= HalphTP1_Buy) && (Position_SL_price != Position_open_price ) &&(Position_Magic == magic_number3)) 
         { 
   
           request.action = TRADE_ACTION_SLTP;
           request.position = PositionTicket;
           request.sl = Position_open_price; 
           request.tp = Position_TP_price; 
           bool change_sl = OrderSend(request,result);
      
               
                 if (Position_Volume*0.2 < SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN))
                   
                 {  
                  trade.PositionClosePartial(PositionTicket,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN),ULONG_MAX); 
                 } 
                 
               else  
               
               {
               trade.PositionClosePartial(PositionTicket,NormalizeDouble(Position_Volume*0.2,2),ULONG_MAX); 
               }                         
            }
         }

Note: Usually the position volume is 0.03 or 0.04 and the SYMBOL_VOLUME_MIN = 0.01.

Many thanks in advance :)


 

 
filipe197:

Hello guys,

I have an expert advisor running in the market from an hedge account of Icmarkets. I am struggling with a situation which I can not solve, despite the research I have already made online:

During an open position, at some point I move my stop loss to break-even and try to partially close a certain amount of volume, using PositionClosePartial operation. Well, sometimes it works properly but in most of the cases, it closes the entire position... It makes no sense. At least, either it should always work or not at all.

Moreover, the weirdest thing is when I backtest all theses trades in the strategy tester, all of the trades are correctly closed partially, as written in the EA.

Here is my code:

Note: Usually the position volume is 0.03 or 0.04 and the SYMBOL_VOLUME_MIN = 0.01.

Many thanks in advance :)


 

      ulong PositionTicket = PositionGetTicket(i);

This line should be the first in your loop, because all the PositionGetInteger(), PositionGetDouble() dpend on it. Your magic number could be wrong as you set before selecting the position.

Where is your position symbol selected ? Unless you only have position on _Symbol (current chart) this will lead to problems as you are using SymbolInfoDouble() inside your loop.

Where is your error checking code ?

Please post the log files (not a screenshot), from Experts and Journal when the problem happened.

 
Alain Verleyen #:

This line should be the first in your loop, because all the PositionGetInteger(), PositionGetDouble() dpend on it. Your magic number could be wrong as you set before selecting the position.

Where is your position symbol selected ? Unless you only have position on _Symbol (current chart) this will lead to problems as you are using SymbolInfoDouble() inside your loop.

Where is your error checking code ?

Please post the log files (not a screenshot), from Experts and Journal when the problem happened.

Hello, 

The things you said make sense. I modified my code accordingly to see if this time the positions will behave as wanted. 
Thank you