Unsupported filling mode

 

I‘m trying to close a position but I get always the ERROR: invalid fill.


The code is basically as follows:


...
CpositionInfo PInfo;
CTrade Trade;
...
for(uint i=0; i<PositionsTotal(); i++)
 {
 if(PInfo.SelectByIndex(i))
  {
  if(...)
   {
   Trade.PositionClose(PInfo.Ticket());
   }
  }
 }

Which produces the log-entry:


Core 1 2017.03.22 18:00:00 failed market sell 14.00 BMW [Unsupported filling mode]

Core 1 2017.03.22 18:00:00 CTrade::OrderSend: market sell 14.00 position #27 BMW [invalid fill]


I also tried

Trade.SetTypeFilling(ORDER_FILLING_RETURN);
Trade.SetTypeFilling(ORDER_FILLING_FOK);
Trade.SetTypeFilling(ORDER_FILLING_IOC);
directly before the
Trade.PositionClose(PInfo.Ticket());

statement.


But I always get the same massage. So what am I doing wrong?

 

I already looked there.


As I said I tried to set each of the three fill methods.

Do I set it correctly?

 

I took a look in “Trade.mqh”


In the Function “Ctrade::PositionClose” there is a call to “ClearStructures();” which sets all members of “m_request” to 0.

Afterwords some values are reset correctly but “m_request.type_filling” is not set.


Then the order is send using “OrderSend(m_request,m_result)”.


How can this function work for anybody?

There is no way of to set the filling method from outside when closing position, is there?

Is this a bug?

 

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Особенности языка mql5, тонкости и приёмы работы

fxsaber, 2017.02.25 16:12

// Возвращает тип исполнения ордера, равный Type, если он доступен на символе Symb, иначе - корректный вариант.
ENUM_ORDER_TYPE_FILLING GetFilling( 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);
}
Применение
Request.type_filling = GetFilling(Request.symbol);
 

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Библиотеки: MT4Orders

fxsaber, 2017.02.08 14:19

// Скрипт позволяет быстро разобраться, как в MT5 формировать соответствующий MT4-аналогу MqlTradeRequest-запрос в MT5-OrderSend.

#define TOSTRING(A)  #A + " = " + (string)(A) + "\n"
#define TOSTRING2(A) #A + " = " + EnumToString(A) + " (" + (string)(A) + ")\n"

string ToString( const MqlTradeRequest &Request )
{
  return(TOSTRING2(Request.action) + TOSTRING(Request.magic) + TOSTRING(Request.order) +
         TOSTRING(Request.symbol) + TOSTRING(Request.volume) + TOSTRING(Request.price) +
         TOSTRING(Request.stoplimit) + TOSTRING(Request.sl) +  TOSTRING(Request.tp) +
         TOSTRING(Request.deviation) + TOSTRING2(Request.type) + TOSTRING2(Request.type_filling) +
         TOSTRING2(Request.type_time) + TOSTRING(Request.expiration) + TOSTRING(Request.comment) +
         TOSTRING(Request.position) + TOSTRING(Request.position_by));
}

string ToString( const MqlTradeResult &Result )
{
  return(TOSTRING(Result.retcode) + TOSTRING(Result.deal) + TOSTRING(Result.order) +
         TOSTRING(Result.volume) + TOSTRING(Result.price) + TOSTRING(Result.bid) +
         TOSTRING(Result.ask) + TOSTRING(Result.comment) + TOSTRING(Result.request_id) +
         TOSTRING(Result.retcode_external));
}

#include <MT4Orders.mqh>

#define PRINT(A) A; Print(#A + "\n" + ToString(MT4ORDERS::LastTradeRequest) + ToString(MT4ORDERS::LastTradeResult));

#define Point _Point
#define Bid (::SymbolInfoDouble(_Symbol, ::SYMBOL_BID))
#define Ask (::SymbolInfoDouble(_Symbol, ::SYMBOL_ASK))

void OnStart()
{
  Print(TOSTRING(AccountInfoString(ACCOUNT_SERVER)));
  
  // Открыли позицию
  const int TicketPosition = PRINT(OrderSend(NULL, OP_BUY, 1, Ask, 100, 0, 0, "My Position"))

  if (OrderSelect(TicketPosition, SELECT_BY_TICKET))
  {
    // Установили SL/TP
    PRINT(OrderModify(OrderTicket(), OrderOpenPrice(), Bid - 100 * Point, Bid + 100 * Point, 0))

    // Закрыли позицию
    PRINT(OrderClose(OrderTicket(), OrderLots(), Bid, 100))
  }

  // Установили отложенный ордер
  const int TicketOrder = PRINT(OrderSend(NULL, OP_BUYLIMIT, 1, Ask - 100 * Point, 100, 0, 0, "My Order"))

  if (OrderSelect(TicketOrder, SELECT_BY_TICKET))
  {
    // Удалили ордер
    PRINT(OrderDelete(OrderTicket()))
  }
}

Run the script and look at the log

OrderClose(OrderTicket(),OrderLots(),Bid,100)
Request.action = TRADE_ACTION_DEAL (1)
Request.magic = 0
Request.order = 0
Request.symbol = EURUSD
Request.volume = 1.0
Request.price = 1.0645
Request.stoplimit = 0.0
Request.sl = 0.0
Request.tp = 0.0
Request.deviation = 100
Request.type = ORDER_TYPE_SELL (1)
Request.type_filling = ORDER_FILLING_IOC (1)
Request.type_time = ORDER_TIME_GTC (0)
Request.expiration = 1970.01.01 00:00:00
Request.comment = 
Request.position = 57775
Request.position_by = 0
Result.retcode = 10009
Result.deal = 9321
Result.order = 57776
Result.volume = 1.0
Result.price = 0.0
Result.bid = 0.0
Result.ask = 0.0
Result.comment = Request executed
Result.request_id = 591
Result.retcode_external = 0
 
Pawelos:

I‘m trying to close a position but I get always the ERROR: invalid fill.

The code is basically as follows:

...
CpositionInfo PInfo;
CTrade Trade;
...
for(uint i=0; i<PositionsTotal(); i++)
 {
 if(PInfo.SelectByIndex(i))
  {
  if(...)
   {
   Trade.PositionClose(PInfo.Ticket());
   }
  }
 }

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Скрипты: CloseAllPositions

fxsaber, 2017.03.30 14:11

// MQL4&5-code

#include <MT4Orders.mqh>

#define _CS(A) ((!IsStopped()) && (A))

bool CloseAllPositions( const bool AllSymbols = true, const int Slippage = 0 )
{
  bool Res = true;
  
  MqlTick Tick;
  
  for (int i = OrdersTotal() - 1; _CS(i >= 0); i--)
    if (OrderSelect(i, SELECT_BY_POS) && (OrderType() <= OP_SELL) &&
        (AllSymbols ? true : (OrderSymbol() == Symbol())) && SymbolInfoTick(OrderSymbol(), Tick))
      Res &= OrderClose(OrderTicket(), OrderLots(), (OrderType() == OP_BUY) ? Tick.bid : Tick.ask, Slippage);
      
  return(Res);
}

 
fxsaber:


For those who have trouble with Ternary statements:


ENUM_ORDER_TYPE_FILLING GetFilling( 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);
        if ( 
                FillingMode == 0 || 
                (Type >= ORDER_FILLING_RETURN) || 
                ((FillingMode & (Type + 1)) != Type + 1) 
        ) {
                if ( 
                        (ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || 
                (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)
                ) {
                        return ORDER_FILLING_RETURN ;
                } else {
                        if (FillingMode == SYMBOL_FILLING_IOC) {
                                return ORDER_FILLING_IOC;
                        } else {
                                return ORDER_FILLING_FOK;
                        }
                }
        } else {
                return Type;
        }
}