MQL5 Closing part of the Position by using 'PositionClosePartial()'

Hi,

I’d like to close part of the pocition. Eg. the opened position = 1 LOT. And I would like to close 0.7 LOT.

I am using the function PositionClosePartial, but it does not work for me.

Maybe because of the last parameter “Deviation”, as I could not find what are the units of deviation (But I have tried to increase the deviation to the really huge number and it didn’t helped). Or from another reason, but I don’t know why.

Please could you help?

CPositionInfo  m_position; 
CTrade         m_trade;

int OnInit()
{
   for(int i=PositionsTotal()-1;i>=0;i--)
   {
      if(m_position.SelectByIndex(i))
      {
         if(m_position.Symbol()==Symbol())
         {
            m_trade.PositionClosePartial(m_position.Ticket(),0.1,20);
         }
      }
   }
}

Move your code out of OnInit to OnTick,OnEvent, or OnTimer

I’ve had it in OnInit just for quick test, bu t I have just moved it to OnTick, but It did not helped :frowning:

You’re probably not on a hedge account then, and if that’s the case you need to send an opposite order.

You could also try this instead of CTrade…

Files:

TradeAlt.mqh 5 kb

This CTradeAlt works properly. Thank you very much. (y)