> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tplus.cx/llms.txt
> Use this file to discover all available pages before exploring further.

# Order preflight

> Use simulate for projected margin, then monitor liquidation risk with streams.

`POST /account/simulate/{user_id}` is non-mutating. It clones the sub-account, applies optional pending transfers, applies a synthetic trade, and returns projected margin fields.

The simulated order is not reduce-only aware. Do not use simulate to prove a reduce-only order will pass. The handler currently returns `liquidation_price: None`, so simulate is not usable for liquidation-price monitoring.

## Raw simulate

```bash theme={null}
curl -X POST https://oms.tplus.cx/account/simulate/$USER_ID \
  -H "Authorization: Bearer $TOKEN" \
  -H "User-Id: $USER_ID" \
  -H "Content-Type: application/json" \
  --data '{
    "sub_account": 1,
    "trade": {
      "asset": "200",
      "is_buy": true,
      "size": "50.0",
      "limit_price": "100.0",
      "trade_type": "margin"
    },
    "pending_transfers": [{"asset": "0", "amount": "5000.0"}]
  }'
```

Supported scenarios:

| Scenario               | Behavior                                                                                    |
| ---------------------- | ------------------------------------------------------------------------------------------- |
| Buy                    | Adds base credits and quote liabilities for margin; spot buys add base and debit USD spot.  |
| Sell                   | Adds quote credits and base liabilities for margin; spot sells debit base and add USD spot. |
| Pending transfer       | Positive spot balances are applied before the simulated trade.                              |
| Stale or missing price | Margin computation can reject because the risk engine lacks usable prices.                  |
| Input cap              | Requests with `size * limit_price` over 50000000 reject before storage lookup.              |

## tpluspy

```python theme={null}
payload = {
    "sub_account": 1,
    "trade": {
        "asset": "200",
        "is_buy": True,
        "size": "50.0",
        "limit_price": "100.0",
        "trade_type": "margin",
    },
    "pending_transfers": [{"asset": "0", "amount": "5000.0"}],
}
result = await client._request("POST", f"/account/simulate/{user.public_key}", json_data=payload)
print(result["available_margin"], result["mm_surplus"], result["is_solvent"])
```

The current SDK uses the authenticated client request layer for simulate; it does not expose a typed simulate helper.

## Worked example

Example verified from the simulate handler tests:

```text theme={null}
Initial inventory: 100 base credits, no quote balance
Price: 100 USD
CF: 80%
LF: 80%
Trade: margin buy 50 base at 100

Post-trade base credits: 150
Post-trade quote liabilities: 5000 USD
Account equity at mark: 150 * 100 - 5000 = 10000 USD
Utilized margin: 5000 USD
Available margin: 7000 USD
MM surplus: 7000 USD
Account leverage: 1.5
Trade margin_required: 1000 USD
```

The paired sell example with the same starting state returns `available_margin = 9000`, `mm_surplus = 9000`, `utilized_margin = 0`, `account_leverage = 0.5`, and `margin_required = 1000`.

## Liquidation monitoring

Use account margin endpoints and the authenticated at-risk stream, not simulate liquidation price:

```text theme={null}
wss://oms.tplus.cx/liquidation/at_risk/ws
```

Client indicators:

| Indicator                      | Source                                                |
| ------------------------------ | ----------------------------------------------------- |
| Can place order                | simulate `is_solvent` and `available_margin`          |
| Remaining trading capacity     | simulate `available_margin`                           |
| Liquidation distance           | `/margin/user/{user_id}` `maintenance_margin_surplus` |
| Liquidation aggregate          | `/liquidation/at_risk/ws`                             |
| Stale or circuit-breaker state | order rejection plus prices and risk endpoints        |
