trying to turn full closure to partial closure

 

I am trying to figure out how two different conditions will close partial or full a position.

k if true will close full being Lots the size of the operation, kp if true will close partially the operation in Small_lots size. but if I am able to close partial the expert gets blocked, I appreciate if someone is able to help me, I provide my thought code but still not work.


while(k > 0  || kp>0)
   {   
   
vol=0;
for(i=0;i<PositionsTotal();i++)
   {
    if(Symbol()==PositionGetSymbol(i) && PositionGetInteger(POSITION_MAGIC) == Magic && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) 
      {
       vol = PositionGetDouble(POSITION_VOLUME);
       tip = PositionGetTicket(i);

       ZeroMemory(request);
       ZeroMemory(result);

       request.action = TRADE_ACTION_DEAL;
       request.symbol          =Symbol();
       request.position =      tip;
       if (kp>0 && vol>=Lots) request.volume = Small_Lots;
       else request.volume          =vol;
       request.type = ORDER_TYPE_BUY;             
       if(fok == true) request.type_filling    =ORDER_FILLING_FOK; 
       if(ret == true) request.type_filling    =ORDER_FILLING_RETURN;
       if(ioc == true) request.type_filling    =ORDER_FILLING_IOC;   
       request.deviation       =Slipp; 

       ResetLastError();
       c = OrderSend(request,result);                  
       if(result.retcode!=10009 || result.retcode!=10008)  
         {
          Print(ResultRetcodeDescription(result.retcode));
          Sleep(Sec*1000);
         }
      }
   }
      
k=0;
for(i=0;i<PositionsTotal();i++)
     {
      if(Symbol()==PositionGetSymbol(i) && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && PositionGetInteger(POSITION_MAGIC) == Magic) k++;
     }
           
    }
 

I propose a solution in the form of a script (a script is a one-time program).

The code:

//+------------------------------------------------------------------+
//|                                      Full or partial closure.mq5 |
//|                              Copyright © 2022, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2022, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.001"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
//---
#property script_show_inputs
//+------------------------------------------------------------------+
//| Enum Full Or Partial                                             |
//+------------------------------------------------------------------+
enum ENUM_FULL_OR_PARTIAL
  {
   full=0,        // Full
   partial=1,     // Partial
  };
//--- input parameters
input string               InpSymbol         = "EURUSD";       // Close positions 'Symbol'
input ulong                InpMagic          = 200;            // Close positions 'Magic number'
input ENUM_FULL_OR_PARTIAL InpFullOrPartial  = full;           // Close ...
input double               InpFullLots       = 0.6;            // Full lots
input double               InpPartialLots    = 0.2;            // Partial lots
input ulong                InpMagicEA        = 379013688;      // 'Full or partial closure' 'Magic number'
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(Symbol());
//---
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==InpSymbol && m_position.Magic()==InpMagic)
           {
            if(InpFullOrPartial==full)
              {
               if(CompareDoubles(m_position.Volume(),InpFullLots))
                  if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                     Print(__FILE__," ",__FUNCTION__,", ERROR Close ... Full: ","CTrade.PositionClose ",m_position.Ticket());
              }
            else
              {
               if(m_position.Volume()>InpPartialLots)
                  if(!m_trade.PositionClosePartial(m_position.Ticket(),InpPartialLots)) // close a position by the specified m_symbol
                     Print(__FILE__," ",__FUNCTION__,", ERROR Close ... Partial: ","CTrade.PositionClose ",m_position.Ticket());
              }
           }
  }
//+------------------------------------------------------------------+
//| Compare doubles                                                  |
//+------------------------------------------------------------------+
bool CompareDoubles(double number1,double number2)
  {
   if(NormalizeDouble(number1-number2,8)==0)
      return(true);
   else
      return(false);
  }
//+------------------------------------------------------------------+