Building robust automated trading systems in MQL requires a deep understanding of order execution nuances. One frequent hurdle faced by developers, particularly when transitioning between brokers or dealing with specific order types, is the “Unsupported Filling Mode” error. This issue directly relates to how your broker processes order requests and can significantly impact your strategy’s performance and reliability.
Introduction to Filling Modes in MQL4/MQL5
What are Filling Modes and Why are They Important?
Filling modes dictate the conditions under which an order request is executed or cancelled by the broker’s server. They specify how much of the order quantity must be filled and what happens to the remaining volume if it cannot be executed immediately or in full. For algorithmic trading, selecting the appropriate filling mode is critical. It ensures that your strategy’s assumptions about execution certainty and partial fills align with reality, preventing unexpected outcomes or missed opportunities.
Types of Filling Modes: Fill or Kill, Immediate or Cancel, Return
MQL supports several standard filling modes, though their availability can vary by broker and instrument:
-
Fill or Kill (FOK): The order must be executed immediately and in full. If the entire volume cannot be matched instantly, the entire order is cancelled. There are no partial fills.
-
Immediate or Cancel (IOC): The order must be executed immediately. Any portion that can be filled instantly is executed, and the remaining volume is cancelled. Partial fills are possible.
-
Return (or Good ‘Til Cancelled/Day): This is the default behavior if no specific filling mode is requested (or if the broker doesn’t support the requested mode and reverts to a default). The order remains active until it is fully executed, cancelled by the user, or expires (e.g., at the end of the trading day for Day orders). Partial fills are common, and the order persists until completion or cancellation.
In MQL5, these are represented by the ENUM_ORDER_STATE and ENUM_ORDER_TYPE enumerations in conjunction with trade request properties. In MQL4, explicit filling mode constants are less prevalent in the standard OrderSend function signature for market orders but become relevant for pending orders or specific broker implementations.
How Filling Modes Impact Order Execution
The chosen filling mode directly influences the probability and characteristics of order execution:
- Strategies requiring immediate, full execution (like high-frequency trading or certain scalping methods) might prefer FOK or IOC to avoid unwanted partial fills or lingering orders in the market.
- Strategies more tolerant of partial fills and requiring an order to wait for favorable conditions might rely on the default (Return) behavior.
- Using an inappropriate filling mode can lead to orders being unexpectedly cancelled (IOC, FOK) or partially filled when full execution was assumed, disrupting the strategy’s logic and potentially causing discrepancies between backtest results and live trading.
Understanding the “Unsupported Filling Mode” Error
Defining the “Unsupported Filling Mode” Error
The “Unsupported Filling Mode” error, often manifested as a trade server return code (e.g., TRADE_RETCODE_UNSUPPORTED in MQL5 or a similar error in MQL4’s GetLastError() depending on context), signifies that the broker’s trading server rejected your order request because the specific filling mode you specified is not available for the requested order type, instrument, or account type.
Common Causes of the Error in MQL Programs
Several factors in your MQL code or environment can trigger this error:
- Incorrect Filling Mode Constant: Using a filling mode constant (
ORDER_FILLING_FOK,ORDER_FILLING_IOC,ORDER_FILLING_RETURNin MQL5) that is simply not supported by the broker at all. - Filling Mode Not Applicable to Order Type: Attempting to apply a filling mode like FOK or IOC to pending order types (Limit, Stop) where they are generally not valid. Filling modes are primarily for Market orders.
- Instrument Restrictions: Certain instruments (e.g., specific CFDs, futures) might have different rules or limitations on supported filling modes compared to standard Forex pairs.
- Account Type Restrictions: Some brokers might limit filling mode options based on the account type (e.g., Standard vs. ECN vs. Cent accounts).
- Broker-Specific Implementations: Even if a mode is standard (like IOC), a particular broker might not have implemented it for certain order flows or APIs.
Broker Restrictions and Filling Mode Limitations
Broker capabilities are the most frequent source of this error. While MQL platforms provide the syntax for different filling modes, the actual support depends entirely on the broker’s trade server configuration and liquidity providers. ECN brokers are more likely to support IOC and potentially FOK for market depth interaction, while dealing desk brokers might primarily support the default (Return) behavior. It is crucial to understand that the MQL standard provides the interface; the broker provides the implementation.
Troubleshooting and Resolving the Error
Checking Broker’s Supported Filling Modes
The primary step is to verify which filling modes your broker actually supports. This information is usually found in:
- The broker’s website (FAQ, trading conditions, contract specifications).
- The MetaTrader terminal itself (often in the Contract Specifications window, though not always explicitly listing filling modes).
- Contacting the broker’s support directly.
Additionally, MQL5 provides functions like SymbolInfoInteger(symbol, SYMBOL_FILLING_MODE, &value) to query the preferred filling mode for a symbol, but this doesn’t definitively list all supported modes or restrictions.
Adjusting Order Properties in MQL Code
Once you know the supported modes, adjust your order placement logic. In MQL5, this involves setting the type_filling field in the MqlTradeRequest structure before calling OrderSend:
MqlTradeRequest request = {};
MqlTradeResult result = {};
request.action = TRADE_ACTION_DEAL; // Market execution
request.symbol = _Symbol;
request.volume = 0.1;
request.type = ORDER_TYPE_BUY;
request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
request.deviation = 10; // Example slippage tolerance
// --- Check and set filling mode ---
int filling_mode = (int)SymbolInfoInteger(_Symbol, SYMBOL_FILLING_MODE);
// Example: Prefer IOC if available, otherwise use default/return
if (filling_mode == ORDER_FILLING_IOC || filling_mode == ORDER_FILLING_FOK)
{
// Broker supports specific modes, choose one or default to IOC
request.type_filling = ORDER_FILLING_IOC;
// Or check specifically if IOC is supported for this symbol/order type
// (Requires deeper broker knowledge or trial-and-error)
}
else
{
// Broker likely only supports RETURN or has no specific setting
request.type_filling = ORDER_FILLING_RETURN; // Explicitly set or rely on default
}
// -- Send the order --
if (OrderSend(request, result))
{
if (result.retcode == TRADE_RETCODE_UNSUPPORTED)
{
Print("OrderSend Error: Unsupported Filling Mode!");
// Implement logic to retry with a different mode or handle failure
} else if (result.retcode != TRADE_RETCODE_SUCCEEDED)
{
Print("OrderSend Error: ", result.retcode);
}
// ... handle success or other errors ...
}
In MQL4, explicit filling mode parameters are less common for market orders in the standard OrderSend signature. Often, the broker’s default behavior is used. For pending orders, some brokers might interpret price/stop levels relative to specific fill types, but explicit control over FOK/IOC via standard parameters is rare. MQL4 developers often rely on GetLastError() after OrderSend and might need to infer broker behavior or fall back to market orders if pending order subtleties cause issues.
Implementing Error Handling for Filling Mode Issues
Robust EAs must anticipate and handle this error. Instead of crashing or failing silently, log the error and potentially retry the order with a different, known-supported filling mode (e.g., fall back to ORDER_FILLING_RETURN if IOC/FOK fails). This makes your EA more resilient across different brokers or changing market conditions.
// Inside your order placement logic
if (OrderSend(request, result))
{
if (result.retcode == TRADE_RETCODE_UNSUPPORTED)
{
Print("Unsupported filling mode encountered: ", request.type_filling, ". Retrying with RETURN.");
// Modify request and retry
request.type_filling = ORDER_FILLING_RETURN;
if (OrderSend(request, result))
{
if (result.retcode == TRADE_RETCODE_UNSUPPORTED)
{
Print("Retry failed: RETURN mode also unsupported? Aborting.");
// Log extensively, maybe disable trading for this symbol
} else if (result.retcode != TRADE_RETCODE_SUCCEEDED)
{
Print("Retry failed with RETURN. Error: ", result.retcode);
// Log failure, investigate further
} else {
Print("Retry with RETURN succeeded.");
// Handle successful retry
}
} else {
Print("Second OrderSend call failed.");
// Handle failed API call
}
}
// ... handle initial success or other initial errors ...
}
Best Practices for Handling Filling Modes in MQL
Choosing the Correct Filling Mode for Your Trading Strategy
Align the filling mode with your strategy’s requirements:
- Need guaranteed full execution or nothing? -> FOK (if supported).
- Need immediate execution but partial fills are acceptable? -> IOC (if supported).
- Need the order to wait in the market until filled (potentially partially over time)? -> RETURN (default).
Do not blindly use FOK or IOC just because they exist; ensure they align with your intended execution logic.
Dynamic Filling Mode Selection Based on Broker Capabilities
Avoid hardcoding filling modes. Query SymbolInfoInteger(symbol, SYMBOL_FILLING_MODE) in MQL5 or, if necessary, implement trial-and-error on account initialization to detect supported modes. Store supported modes in global variables or object properties. This makes your EA more portable across different brokers.
// Example structure during OnInit or symbol handling
int preferred_filling = (int)SymbolInfoInteger(_Symbol, SYMBOL_FILLING_MODE);
int actual_filling = ORDER_FILLING_RETURN; // Default fallback
// Prioritize based on preferred and known common support
if (preferred_filling == ORDER_FILLING_IOC)
{
actual_filling = ORDER_FILLING_IOC;
} else if (preferred_filling == ORDER_FILLING_FOK)
{
// FOK is less commonly supported, be cautious or test explicitly
// actual_filling = ORDER_FILLING_FOK;
actual_filling = ORDER_FILLING_RETURN; // Safer default
}
// If preferred is RETURN or unknown, actual_filling remains RETURN
// Use actual_filling in your MqlTradeRequest
Testing Filling Mode Compatibility in Different Environments
Thoroughly backtest and, more importantly, forward-test your EA on demo accounts with the target broker. Verify that orders are executed with the expected filling modes (check the ‘History’ or ‘Journal’ tabs). If you plan to use the EA on multiple brokers, test on a demo account for each broker to confirm filling mode support and behavior.
Conclusion
Recap of Key Points
The “Unsupported Filling Mode” error arises when an MQL order request specifies a filling mode (FOK, IOC, RETURN) that the broker’s server does not support for that specific instrument, order type, or account. This is a broker-side limitation, not an MQL language error itself. Resolving it requires understanding the broker’s supported modes, adjusting the MqlTradeRequest.type_filling accordingly in MQL5, and implementing robust error handling to manage potential rejections or fall back to default behaviors.
Further Resources and Learning
- MQL5 Reference:
OrderSendfunction,MqlTradeRequeststructure,ENUM_ORDER_STATE,ENUM_ORDER_TYPE,ENUM_ORDER_FILLING. - MQL4 Reference:
OrderSendfunction,GetLastError. - Consult your broker’s documentation on order execution and types.
- Explore the MetaQuotes documentation on trade operations and error codes.