Partial Close

 
I'd like to know how i would write a function that will close a partial of all open positions that are in profit every 10 pips. I'm writing a grid EA which has sell stops placed every 10 pips below the opening price and buy stops every 10 pips above the opening price. So how could i go about this?
 
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community.
  • You can also look at the Codebase if something already similar exists, so that you may study it.
  • Finally, you also have the option to hire a programmer in the Freelance section.
 

Here is a sample code from the CodeBase ...

Code Base

closing partially script and Stop loss to Break Even point

Mehmet Bastem, 2021.06.24 16:55

closing partially script and Stop loss to Break Even point
 

Your question is not related to the section you posted so it has been moved to the appropriate section: MQL5 forum: Expert Advisors and Automated Trading

 

here is what i came up with but when it is ran in the strategy tester it freezes as soon as the first stop order is hit.

I would like to know how to close partial profits on an open position every ten pips, so for example if a position was opened at 1.10000 i would like it to close a 10% partial at 1.10100 and then another 10% at 1.0200 and another 10% at 1.10300 and so on. I would like it to do this for all positions.

void ClosePartials()
{
   double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   int totalPositions = PositionsTotal();
   for(int i=PositionsTotal()-1;i>=0;i--)
   {
      ulong ticket = PositionGetTicket(i);
      if(PositionSelectByTicket(ticket))
      {
         double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         double posVolume = PositionGetDouble(POSITION_VOLUME);
         double pipsProfit = (((openPrice-currentPrice)/_Point))/10;
         pipsProfit = NormalizeDouble(pipsProfit,_Digits);
        
         for(i; i<totalPositions; i++)
            {
               double lotsToClose = posVolume*0.1;
               if(pipsProfit>0&&(int)pipsProfit%10==0)
               {
                  trade.PositionClosePartial(ticket,lotsToClose);
               }
            }
      }
   }
}
 

I have edited your Improperly formatted code. In future, please use the CODE button (Alt-S) when inserting code.

Code button in editor