How can I close some of volume(lot) a position?

 

Hi

How can I close some of volume(lot) a position?

Equivalent   OrderClose(OrderTicket(),0.01,Ask,3,Violet)  in mql4.

 

Just send an opposite direction order for the volume you want to close.
 
enivid:

Just send an opposite direction order for the volume you want to close.

mean, if was position sell send position buy

It's a big weakness for MQL5

 
Maryam:

Hi

How can I close some of volume(lot) a position?

Equivalent   OrderClose(OrderTicket(),0.01,Ask,3,Violet)  in mql4.

Your question is about mql4, and it should be searched first  (click here or here) and then asked at mql4 forum https://www.mql5.com/forum/en - there's plenty discussion about partial close over there

In mql4 use OrderCloseBy() (https://docs.mql4.com/trading/ordercloseby), that is if your broker allow to close some part of position.

Its much easier in mql5, say you have 1 lot open buy then to close partial of it just send - say 0.7 lot or 0.33 lot of open sell. 

 
Maryam:

Hi

How can I close some of volume(lot) a position?

Equivalent   OrderClose(OrderTicket(),0.01,Ask,3,Violet)  in mql4.

To close a percentage of a position, you can replace the Trade file with this modified version. It is located in MQL5>>Include>>Trade>>Trade. This modification to the .trade file will also allow a 'partial close' of a position, when calling PositionClose().  Notice a modification to the PositionClose() method on line 95:

bool    PositionClose(const string symbol,const ulong percent = 100,const ulong deviation=ULONG_MAX);

Const ulong 'percent' stores the percentage value of the position to close out first. When calling PositionClose() without stating a percent of the total volume to close, PositionClose()  will attempt close out 100%, as normal. Also refer to line 329 and line 367. You will need to modify your EA ( the order close loop ) and perhaps include a scalar to block or permit the partial close. See the code segments below:

   
if(GetPositionInfo(symbol,pos)) // save data about pos      {       //--- close by time       if(close_all)         {             Print("close_all:",close_all);             if(m_trade.PositionClose(symbol))                 {                 close_all=false;                 alreadyPartClosed = false;                 recalc=true;             }              return;         }                if(part_close)         {             Print("part_close:",part_close);             if(m_trade.PositionClose(symbol, partially_exit_param.exit_percent))             {                 part_close=false;                 alreadyPartClosed = true;                 recalc=true;             }              return;         }
    //--- close percent of position volume by first trigger line
         if(bid>=BuyCloseBuffer[0] && !alreadyPartClosed)
           {
               if (partially_exit_param.scale_out && ScaleOutSignal) 
                   
               {            
                   part_close = true;
                   return;
               }
               else
               {
                   close_all=true;
                   return;
               }               
           }
    //--- close remaining position volume by second trigger line  
         if(alreadyPartClosed && bid>=BuyCloseBuffer2[0])
           {                   
                   close_all = true;
                   return;
           }

 

The above code is just an example, but I run a very similar EA and it works fine. 

Chris_V 

Files:
Trade.mqh  44 kb
 

Thank Chris_V very helpful


 

Thank you all
Reason: