Errors
The library reports problems by throwing exceptions. This page lists the types, when they appear, and the broker status codes.
All exception types are in the namespace mtapi.mt5.
Exception types
| Type | Base | When |
|---|---|---|
ServerException | Exception | The broker or login server answered with an error. Read Code (a Msg). |
ConnectException | Exception | Connect failed before the broker answered, or you used a member that needs a connection (Symbols.Exist, ServerBuild, AccountMethod). Also the text from the login service, such as KazaTrade: no ApiKey set (set api.ApiKey to the key from your Developer page). |
ConnectionException | ApplicationException | The client certificate (PfxFile) has no usable private key. |
ServerNotFoundException | Exception | The server-name constructor could not find the broker server. |
InvalidSymbolException | Exception | The symbol does not exist on this broker. |
InvestorModeException | Exception | You tried to trade on a read-only (investor password) login. |
DoubleRequestException | Exception | A history or bar download is already running on this client. Wait and try again. |
ProxyException | Exception | The proxy refused or failed. |
TimeoutException | system | A quote, bar or history wait ran out, or OrderSend, OrderClose or OrderModify got no answer within ExecutionTimeout. After a trade timeout the trade may still have happened: check GetOpenedOrders before you retry. |
Exception itself is thrown for a few cases, for example trading switched off for the account or the symbol (or the symbol only allows one direction or closing), "Not connected", "Symbol not found: ...", "Account not loaded" and "Request Lots = 0 ". Catch it last.
ServerException.Code
ServerException.Code is a Msg. ex.Message has a readable text for it. Common values:
Login
| Code | Meaning |
|---|---|
INVALID_ACCOUNT | Wrong login number or password |
ACCOUNT_DISABLED | The account is disabled |
UNKNOWN_ACCOUNT | The server does not know this account |
INVALID_PASSWORD | Password refused |
INVALID_ONETIME_PASSWORD | One-time password wrong or missing (set OtpPassword) |
PASSWORD_MUST_CHANGED | The broker requires a new password first |
SERVER_BUSY, SERVER_SHUTDOWN | The broker server is busy or stopping. Try later. |
OLD_SERVER, INVALID_SERVER | The server version or address does not fit |
Trading
| Code | Meaning |
|---|---|
REQUOTE, PRICE_CHANGED | The price moved. Get a new quote and retry, or raise deviation. |
NO_PRICES | No price for the symbol |
MARKET_CLOSED | The market is closed |
TRADE_DISABLED, AUTOTRADE_DISABLE_SERVER, AUTOTRADE_DISABLE_CLIENT | Trading is not allowed |
NO_MONEY | Not enough free margin |
INVALID_VOLUME | Lots outside min, max or step |
INVALID_PRICE, INVALID_STOPS, INVALID_EXPIRATION, INVALID_REQUEST | A price, stop level, expiry or the request is not valid |
UNSUPPORTED_FILLING_MODE | Try another fillPolicy (or Any) |
ORDER_LIMIT_REACHED_, POSITION_LIMIT_REACHED, VOLUME_LIMIT_REACHED | A broker limit was reached |
POSITION_NOT_EXISTS | The position is already closed |
VOLUME_CLOSE_EXCEEDS_POSITION | You tried to close more than the position holds |
NO_CHANGES | Nothing to modify: the values are the same |
MODIFICATION_FAILED | The change was refused |
TOO_MANY_TRADE_REQUESTS | Slow down |
HEDGE_PROHIBITED, FIFO_CLOSE, ONLY_LONG_POSITION, ONLY_SHORT_POSITION, ONLY_CLOSE_POSITION | A broker rule blocks this trade |
REQUEST_REJECTED, REQUEST_CANCELLED, REQUEST_ERROR, REQUEST_TIMEOUT | The request did not complete |
NETWORK_PROBLEM | The link dropped |
The progress codes REQUEST_ON_WAY, REQUEST_ACCEPTED, REQUEST_EXECUTED, ORDER_PLACED and DONE are normal steps, not errors.
Msg has more values than listed here. Use ex.Code.ToString() for the name.
Handling errors
try
{
api.OrderSend("EURUSD", 0.10, price, OrderType.Buy);
}
catch (InvestorModeException) { /* read-only login */ }
catch (InvalidSymbolException) { /* wrong symbol name */ }
catch (TimeoutException) { /* unknown outcome: check open positions */ }
catch (ServerException ex) when (ex.Code == Msg.REQUOTE)
{
// get a fresh quote and try once more
}
catch (ServerException ex) { Console.WriteLine($"Broker refused: {ex.Code}"); }
catch (Exception ex) { Console.WriteLine(ex.Message); /* e.g. trading off for this symbol */ }