Error codes
ViiSync reports failures with MT5's return codes, the Msg enum, so error handling written for the MT5 API keeps working.
Where a code appears
| where | when |
|---|---|
ServerException.Code |
a trade, history or account call was refused or failed. ServerException.Result carries the server's typed answer when there was one |
ConnectException.Code |
connecting or logging in failed |
TradeResult.Status |
every stage reported on OnOrderProgress, successes included |
| InvestorModeException | not a code: thrown by every trade or password call on an investor (read-only) session |
| InvalidSymbolException | not a code: the symbol is not in the account's catalog |
Handling refusals
try
{
api.OrderSend("EURUSD", 100, 0, OrderType.Buy);
}
catch (ServerException ex) when (ex.Code == Msg.NO_MONEY)
{
Console.WriteLine($"not enough margin: {ex.Message}");
}
catch (ServerException ex) when (ex.Code is Msg.MARKET_CLOSED or Msg.NO_PRICES)
{
Console.WriteLine("market closed or not priced, try later");
}
catch (ServerException ex)
{
// ex.Result carries the server's typed answer when there was one (bid/ask of a REQUOTE, ...).
Console.WriteLine($"{ex.Code} ({(int)ex.Code}): {ex.Result?.Comment ?? ex.Message}");
}
catch (InvestorModeException)
{
Console.WriteLine("this session logged in with the investor (read-only) password");
}
A market order sent with a deviation is refused with REQUOTE when the price has moved further than the deviation. The
new prices come with the exception:
var q = api.GetQuote("EURUSD") ?? throw new InvalidOperationException("no price");
try
{
api.OrderSend("EURUSD", 0.10, q.Ask, OrderType.Buy, deviation: 5);
}
catch (ServerException ex) when (ex.Code == Msg.REQUOTE && ex.Result is { } r)
{
Console.WriteLine($"price moved: new bid/ask {r.Bid}/{r.Ask}");
}
Connection failures:
try
{
api.Connect();
}
catch (ConnectException ex)
{
// INVALID_ACCOUNT: credentials refused. TOO_FREQUENT_REQUEST: the server's brute-force guard has locked the
// login out for now. OPERATION_TIMEOUT / NO_CONNECTION: the server could not be reached in time.
Console.WriteLine($"{ex.Code}: {ex.Message}");
}
Codes ViiSync returns
This table is generated from the library's Msg enum on every documentation build.
| code | value | meaning in ViiSync |
|---|---|---|
| DONE | 0 | Success (generic). |
| OK | 1 | Success (generic). |
| INVALID_PARAM | 3 | An argument is invalid, e.g. Connect with no Host set. |
| INVALID_DATA | 4 | The server refused the request with a generic error that has no more specific MT5 code (e.g. a login rejected for an unstated reason). |
| NETWORK_ERROR | 7 | Network failure (generic). |
| NOT_PERMISSION | 8 | The server denied the operation for this account (order placement denied). |
| OPERATION_TIMEOUT | 9 | Connecting or logging in did not finish within ConnectTimeout. |
| NO_CONNECTION | 10 | Not connected, or the connection dropped while the call was waiting. OrderClientSafe recovers from this. |
| TOO_FREQUENT_REQUEST | 12 | Login refused without checking the credentials: the server's brute-force guard has locked the login or source address out for now (exponential backoff). Auto-reconnect keeps retrying. |
| OPERATION_CANCELED | 16 | The caller cancelled the call before the request was sent; nothing reached the server. |
| INVALID_ACCOUNT | 1001 | The login or password was refused (ConnectException.Code). |
| INVALID_PASSWORD | 3006 | A password change was refused. |
| SYMBOL_NOT_FOUND | 6001 | The symbol is not in the account's catalog. |
| REQUEST_ON_WAY | 10001 | Not an error: the request left and the answer is pending (OnOrderProgress stage InProcess). |
| REQUOTE | 10004 | A market order with a deviation would fill outside price ± deviation points. ServerException.Result carries the new bid/ask. |
| REQUEST_REJECTED | 10006 | The server rejected the request (generic rejection). |
| REQUEST_CANCELLED | 10007 | The request was cancelled. |
| ORDER_PLACED | 10008 | Success: the pending order was placed. |
| REQUEST_EXECUTED | 10009 | Success: the request was executed. |
| REQUEST_EXECUTED_PARTIALLY | 10010 | Success: the request was executed partially. |
| REQUEST_ERROR | 10011 | The request failed on the server. |
| REQUEST_TIMEOUT | 10012 | No answer within ExecutionTimeout. The request may still have executed: read the state, or use OrderClientSafe. |
| INVALID_REQUEST | 10013 | The request is malformed or not supported (e.g. OrderType.CloseBy passed to OrderSend). |
| INVALID_VOLUME | 10014 | Volume below the minimum, above the maximum or off the volume step. |
| INVALID_PRICE | 10015 | Invalid order price (also a book-or-cancel order that would execute at once). |
| INVALID_STOPS | 10016 | Invalid SL/TP levels. |
| TRADE_DISABLED | 10017 | Trading is disabled for the symbol or the account, or the session is an investor (read-only) login. |
| MARKET_CLOSED | 10018 | Outside the trade session, on a holiday, or outside office hours. |
| NO_MONEY | 10019 | Not enough free margin. |
| NO_PRICES | 10021 | No executable price or liquidity for the symbol right now. |
| INVALID_EXPIRATION | 10022 | Invalid pending-order expiration. |
| NO_CHANGES | 10025 | A modify that changes nothing. |
| UNSUPPORTED_FILLING_MODE | 10030 | An explicit FOK/IOC filling policy the symbol does not allow. |
| INVALID_ORDER | 10035 | No such working order, or the order cannot be modified/cancelled in its state. |
| POSITION_NOT_EXISTS | 10036 | No such open position. |
| ONLY_LONG_POSITION | 10042 | The symbol is long-only. |
| ONLY_SHORT_POSITION | 10043 | The symbol is short-only. |
| ONLY_CLOSE_POSITION | 10044 | The symbol is close-only: positions can be closed, not opened. |
| HEDGE_PROHIBITED | 10046 | Hedging is not allowed on this account. |
| NETWORK_PROBLEM | 2147483647 | Network problem (generic). |
The other 110 members of Msg exist for source compatibility with MT5 code and carry NotEmittedByViiSyncAttribute: ViiSync never returns them.
Timeouts are not refusals
REQUEST_TIMEOUT and NO_CONNECTION mean the answer did not arrive, not that the server refused. The request may have
executed. Read the state (GetOpenedOrders) before retrying, or let
OrderClientSafe do it: it retries closes, deletes and modifies only when the target is still
there, and never re-sends an open.