# Check an order

Source: https://docs.pipsocket.com/api/check-an-order

MT5's dry run: whether it would pass, and its margin.

All calls: `https://api.pipsocket.com`, with `Authorization: Bearer <your workspace key>`.

## Check an order

`POST /v1/accounts/{account}/orders/check`

MT5's `OrderCheck`: whether the order would pass, and the margin it would take. Nothing is sent to the trade server. Takes the same body as Place an order.

### Path

- `account` (string, required): The account's id, `acc_` and 26 characters, as the dashboard shows it.

### Body

A market or pending order. Fields left out are sent as MT5's empty values (0, "", `gtc`); there is no other default. Fields not listed here are refused.

- `symbol` (string, required, MT5 `symbol`): The symbol exactly as the broker names it, such as `EURUSD`.
- `type` (enum, required, MT5 `type`): What to send. `buy` and `sell` fill now; the others wait at `price`. `close_by` is refused here: use Close by. One of: `buy`, `sell`, `buy_limit`, `sell_limit`, `buy_stop`, `sell_stop`, `buy_stop_limit`, `sell_stop_limit`, `close_by`.
- `volume` (number, required, MT5 `volume`): Lots. MT5 rejects a volume outside the symbol's minimum, maximum and step.
- `type_filling` (enum, required, MT5 `type_filling`): How the order may fill. Required: each symbol allows its own modes (its `filling_modes`). One of: `fok`, `ioc`, `return`, `boc`.
- `price` (number, optional, MT5 `price`): Required for pending orders: the price the order waits for. For a market order it is sent as given (0 if left out): brokers with instant or request execution need it, those with market or exchange execution ignore it.
- `stoplimit` (number, optional, MT5 `stoplimit`): Required for `buy_stop_limit` and `sell_stop_limit`: the price of the limit order placed once `price` is reached.
- `sl` (number, optional, MT5 `sl`): Stop loss price, set in the same request. 0 or left out: none.
- `tp` (number, optional, MT5 `tp`): Take profit price, set in the same request. 0 or left out: none.
- `deviation` (integer, optional, MT5 `deviation`): The largest difference from `price` MT5 accepts, in points, for instant or request execution.
- `type_time` (enum, optional, MT5 `type_time`): How long a pending order lasts. `gtc` if left out. One of: `gtc`, `day`, `specified`, `specified_day`.
- `expiration` (integer, optional, MT5 `expiration`): When a pending order expires: broker server time, seconds since 1970. Required for `type_time` `specified` and `specified_day`.
- `magic` (integer, optional, MT5 `magic`): Your own number for the order, kept by MT5 on the order, its deals and its position.
- `comment` (string, optional, MT5 `comment`): Up to 31 characters, kept by MT5 on the order. Brokers may replace it.

### Response

- `sent` (object): MT5's `MqlTradeRequest`, field for field, exactly as recorded to send to the terminal: the request's `state` and `result` say whether it reached MT5. Fields the call does not use carry MT5's empty values (0, "", `fok`, `gtc`).
  - `action` (enum, MT5 `action`): What the request does. One of: `deal`, `pending`, `sltp`, `modify`, `remove`, `close_by`.
  - `symbol` (string, MT5 `symbol`): The symbol, exactly as the broker names it.
  - `type` (enum, MT5 `type`): The order type. One of: `buy`, `sell`, `buy_limit`, `sell_limit`, `buy_stop`, `sell_stop`, `buy_stop_limit`, `sell_stop_limit`, `close_by`.
  - `volume` (number, MT5 `volume`): Lots.
  - `price` (number, MT5 `price`): The price: a pending order's, or the one instant and request execution trade at.
  - `stoplimit` (number, MT5 `stoplimit`): A stop-limit order's limit price, placed once `price` is reached.
  - `sl` (number, MT5 `sl`): Stop loss price; 0 for none.
  - `tp` (number, MT5 `tp`): Take profit price; 0 for none.
  - `deviation` (integer, MT5 `deviation`): The largest difference from `price` MT5 accepts, in points.
  - `type_filling` (enum, MT5 `type_filling`): How the order may fill. One of: `fok`, `ioc`, `return`, `boc`.
  - `type_time` (enum, MT5 `type_time`): How long a pending order lasts. One of: `gtc`, `day`, `specified`, `specified_day`.
  - `expiration` (integer, MT5 `expiration`): Broker server time, seconds since 1970, as MT5 takes it.
  - `order` (integer, MT5 `order`): The pending order the request is about (modify, remove).
  - `position` (integer, MT5 `position`): The position the request is about (close, stops, close by).
  - `position_by` (integer, MT5 `position_by`): The opposite position, for close by.
  - `magic` (integer, MT5 `magic`): Your own number, kept by MT5 on the order, its deals and its position.
  - `comment` (string, MT5 `comment`): Up to 31 characters, kept by MT5 on the order.
- `result` (object): MT5's `MqlTradeCheckResult`.
  - `passed` (boolean): Whether `OrderCheck` passed the request.
  - `retcode` (integer, MT5 `retcode`): MT5's return code: 0 when the check passed.
  - `retcode_name` (enum): The return code's name; `UNKNOWN` for a code MT5 doesn't document. One of: `OK`, `TRADE_RETCODE_REQUOTE`, `TRADE_RETCODE_REJECT`, `TRADE_RETCODE_CANCEL`, `TRADE_RETCODE_PLACED`, `TRADE_RETCODE_DONE`, `TRADE_RETCODE_DONE_PARTIAL`, `TRADE_RETCODE_ERROR`, `TRADE_RETCODE_TIMEOUT`, `TRADE_RETCODE_INVALID`, `TRADE_RETCODE_INVALID_VOLUME`, `TRADE_RETCODE_INVALID_PRICE`, `TRADE_RETCODE_INVALID_STOPS`, `TRADE_RETCODE_TRADE_DISABLED`, `TRADE_RETCODE_MARKET_CLOSED`, `TRADE_RETCODE_NO_MONEY`, `TRADE_RETCODE_PRICE_CHANGED`, `TRADE_RETCODE_PRICE_OFF`, `TRADE_RETCODE_INVALID_EXPIRATION`, `TRADE_RETCODE_ORDER_CHANGED`, `TRADE_RETCODE_TOO_MANY_REQUESTS`, `TRADE_RETCODE_NO_CHANGES`, `TRADE_RETCODE_SERVER_DISABLES_AT`, `TRADE_RETCODE_CLIENT_DISABLES_AT`, `TRADE_RETCODE_LOCKED`, `TRADE_RETCODE_FROZEN`, `TRADE_RETCODE_INVALID_FILL`, `TRADE_RETCODE_CONNECTION`, `TRADE_RETCODE_ONLY_REAL`, `TRADE_RETCODE_LIMIT_ORDERS`, `TRADE_RETCODE_LIMIT_VOLUME`, `TRADE_RETCODE_INVALID_ORDER`, `TRADE_RETCODE_POSITION_CLOSED`, `TRADE_RETCODE_INVALID_CLOSE_VOLUME`, `TRADE_RETCODE_CLOSE_ORDER_EXIST`, `TRADE_RETCODE_LIMIT_POSITIONS`, `TRADE_RETCODE_REJECT_CANCEL`, `TRADE_RETCODE_LONG_ONLY`, `TRADE_RETCODE_SHORT_ONLY`, `TRADE_RETCODE_CLOSE_ONLY`, `TRADE_RETCODE_FIFO_CLOSE`, `TRADE_RETCODE_HEDGE_PROHIBITED`, `UNKNOWN`.
  - `comment` (string, MT5 `comment`): 
  - `balance` (number, MT5 `balance`): The balance after the trade.
  - `equity` (number, MT5 `equity`): The equity after the trade.
  - `profit` (number, MT5 `profit`): The floating profit after the trade.
  - `margin` (number, MT5 `margin`): The margin the trade takes.
  - `margin_free` (number, MT5 `margin_free`): The free margin after the trade.
  - `margin_level` (number, MT5 `margin_level`): The margin level after the trade, in percent.
- `message` (string): The return code's meaning, in a sentence.

### Answers

- 200: MT5's check result.
- 401: - `missing_api_key`: No API key: send it as `Authorization: Bearer <key>`.; `invalid_api_key`: The key is not a workspace's current key (a rolled key stops working within about a minute).
- 404: - `no_such_account`: No such account, or one the key's workspace doesn't have: the two look the same.
- 422: - `invalid_request`: A field, query parameter or header is missing, malformed or not allowed, or the body is not JSON. Nothing was sent.
- 503: - `account_not_connected`: The account's terminal is not connected to the engine (stopped, starting, or restarting). Nothing was sent, except by the kill switch: engaging it still engages it, and may already have deleted pending orders.; `bridge_outdated`: The account's terminal runs an older bridge that can't trade: restart the account to update it. Nothing was sent.; `bad_command`: The terminal refused the command, for example a negative `expiration`, or a history range MT5 can't select.; `engine_busy`: The account's engine host has too many calls in flight (256). Nothing was done; try again.; `engine_unavailable`: The account's engine host is not connected to Pipsocket, or disconnected while the call waited. A trade it already had is still settled: look it up.
- 504: - `terminal_timeout`: The account's terminal did not answer within 5 seconds.; `engine_timeout`: The account's engine did not answer within 30 seconds.

### Example

```bash
curl -X POST "https://api.pipsocket.com/v1/accounts/acc_01m6tx3k9f2qw8r7bznd5yvh4c/orders/check" \
  -H "Authorization: Bearer $PIPSOCKET_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "sl": 1.079,
    "symbol": "EURUSD",
    "type": "buy",
    "type_filling": "ioc",
    "volume": 0.1
  }'
```

200 passed:

```json
{
  "message": "The request passed the check.",
  "result": {
    "balance": 10000,
    "comment": "Done",
    "equity": 10000,
    "margin": 108.41,
    "margin_free": 9891.59,
    "margin_level": 9224.24,
    "passed": true,
    "profit": 0,
    "retcode": 0,
    "retcode_name": "OK"
  },
  "sent": {
    "action": "deal",
    "comment": "",
    "deviation": 0,
    "expiration": 0,
    "magic": 0,
    "order": 0,
    "position": 0,
    "position_by": 0,
    "price": 0,
    "sl": 1.079,
    "stoplimit": 0,
    "symbol": "EURUSD",
    "tp": 0,
    "type": "buy",
    "type_filling": "ioc",
    "type_time": "gtc",
    "volume": 0.1
  }
}
```
