How to close a partial position using standard functions

 

I open a trade using the below code:

MqlTradeRequest request = {};
request.action   = TRADE_ACTION_DEAL; // Specify a market order
request.symbol   = _Symbol;
request.volume   = lot;
request.type     = ORDER_TYPE_BUY;
request.price    = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
request.deviation = 30; // Maximum allowed slippage
request.magic    = 123;
request.type_filling = ORDER_FILLING_IOC;
request.type_time = ORDER_TIME_GTC;   // Good till canceled order
request.sl = sl;
request.tp = tp;

// Create MqlTradeResult structure to store the result of the order
MqlTradeResult result = {};

if (OrderSend(request, result))
{
  Alert("Buy trade opened at ", TimeCurrent());
  break;
}
else
{
  Print("Error sending order. ", result.comment, " Code:", GetLastError());
}

When I try to partially close the same position, I get the Unsupported filling mode error with error code 4756.

My partial trade closing code is below:

MqlTradeRequest request = {};
MqlTradeResult result = {};
// Initialize the trade request structure
ZeroMemory(request);
ZeroMemory(result);

request.action   = TRADE_ACTION_DEAL;
request.symbol   = _Symbol;
request.volume   = myLot;
request.type     = ORDER_TYPE_SELL;
request.price    = SymbolInfoDouble(_Symbol, SYMBOL_BID);
request.deviation = 30; // Maximum allowed slippage

if (OrderSend(request, result))
{
  Print(PositionGetInteger(POSITION_TICKET), " closed partial. Result: ", result.retcode);
  break;
}
else
{
  Print("Error closing partial position: ", result.comment, " Code:", GetLastError());
}

How can I fix this issue using standard MQL5 functions?

 

The error seems clear enough, you are using the wrong type filling. The best way is to retreive the filling mode of the Symbol you are trading, and apply it to your trade requests.

Or use CTrade classes to avoid handling all this kind of errors manually :)

 
verynewuser:

I open a trade using the below code:

When I try to partially close the same position, I get the Unsupported filling mode error with error code 4756.

My partial trade closing code is below:

How can I fix this issue using standard MQL5 functions?

use this function to get the filling mode :

ENUM_ORDER_TYPE_FILLING GetFillingA( const string Symb, const uint Type = ORDER_FILLING_FOK )
{
  const ENUM_SYMBOL_TRADE_EXECUTION ExeMode = (ENUM_SYMBOL_TRADE_EXECUTION)::SymbolInfoInteger(Symb, SYMBOL_TRADE_EXEMODE);
  const int FillingMode = (int)::SymbolInfoInteger(Symb, SYMBOL_FILLING_MODE);

  return((FillingMode == 0 || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ?
         (((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ?
           ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) :
          (ENUM_ORDER_TYPE_FILLING)Type);
}
 
Fabio Cavalloni #:

The error seems clear enough, you are using the wrong type filling. The best way is to retreive the filling mode of the Symbol you are trading, and apply it to your trade requests.

Or use CTrade classes to avoid handling all this kind of errors manually :)

Lorentzos Roussos #:

use this function to get the filling mode :

I have verified using Print() statements. The filling mode is correct. It is `ORDER_FILLING_IOC`. I have noticed something. If I write 
request.type_filling = ORDER_FILLING_IOC;

statement while trying to partially close in my above snippet, instead of closing the existing position, a new opposite position is opened. If I don't write this statement, I receive the "Unsupported filling mode" error.

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
senior developers!! please help.
 

 
Example of the TRADE_ACTION_DEAL trade operation for closing positions:

#define EXPERT_MAGIC 123456   // MagicNumber of the expert
//+------------------------------------------------------------------+
//| Closing all positions                                            |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request;
   MqlTradeResult  result;
   int total=PositionsTotal(); // number of open positions   
//--- iterate over all open positions
   for(int i=total-1; i>=0; i--)
     {
      //--- parameters of the order
      ulong  position_ticket=PositionGetTicket(i);                                      // ticket of the position
      string position_symbol=PositionGetString(POSITION_SYMBOL);                        // symbol 
      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS);              // number of decimal places
      ulong  magic=PositionGetInteger(POSITION_MAGIC);                                  // MagicNumber of the position
      double volume=PositionGetDouble(POSITION_VOLUME);                                 // volume of the position
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);    // type of the position
      //--- output information about the position
      PrintFormat("#%I64u %s  %s  %.2f  %s [%I64d]",
                  position_ticket,
                  position_symbol,
                  EnumToString(type),
                  volume,
                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
                  magic);
      //--- if the MagicNumber matches
      if(magic==EXPERT_MAGIC)
        {
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         request.action   =TRADE_ACTION_DEAL;        // type of trade operation
         request.position =position_ticket;          // ticket of the position
         request.symbol   =position_symbol;          // symbol 
         request.deviation=5;                        // allowed deviation from the price
         request.magic    =EXPERT_MAGIC;             // MagicNumber of the position

         request.volume   =volume;                   // volume of the position
         //bool CTrade::SetTypeFillingBySymbol(const string symbol)
         uint filling=(uint)SymbolInfoInteger(Symbol(),SYMBOL_FILLING_MODE);
         if((filling&SYMBOL_FILLING_FOK)==SYMBOL_FILLING_FOK)
           {
            request.type_filling =ORDER_FILLING_FOK;
           }
         if((filling&SYMBOL_FILLING_IOC)==SYMBOL_FILLING_IOC)
           {
            request.type_filling =ORDER_FILLING_IOC;
           }

         //--- set the price and order type depending on the position type
         if(type==POSITION_TYPE_BUY)
           {
            request.price=SymbolInfoDouble(position_symbol,SYMBOL_BID);
            request.type =ORDER_TYPE_SELL;
           }
         else
           {
            request.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
            request.type =ORDER_TYPE_BUY;
           }
         //--- output information about the closure
         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation   
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
         //---
        }
     }
  }
//+------------------------------------------------------------------+

This is a sample code from the documentation, edited to include type_filling. Please adjust the request.volume accordingly.