> For the complete documentation index, see [llms.txt](https://docs.extended.exchange/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.extended.exchange/extended-resources/trading/order-cost.md).

# Order Cost

### Order Placement

When a user places a new order, the system checks whether the relevant Available Balance for Trading is greater than or equal to the order cost.

Order cost is calculated differently for perpetual and spot markets.

**For perpetual markets**:

* [Available Balance for Trading Perps](/extended-resources/trading/unified-margin-and-balances.md#account-balances) = Equity - Margin Reserved for Perps - Margin Reserved for Spot Buy Orders
* Order Cost for Buy Orders = max(Initial Margin Rate \* New Order Price \* (New Order Size + min(0, 2 \* (Open Position Size + Triggered Buy Order Size 1+ … + Triggered Buy Order Size N))) + Open Loss, 0)
* Order Cost for Sell Orders = max(-Initial Margin Rate \* New Order Price \* (New Order Size + max(0, 2 \* (Open Position Size + Triggered Sell Order Size 1+ … + Triggered Sell Order Size N))) + Open Loss, 0)
* In the equations above:
  * Open Loss = |min(New Order Size \* (Mark Price - New Order Price), 0)| + Fee
  * Position / Order Size is positive for Long and Negative for Short

**For spot markets:**

* Spot order costs differ for buy and sell orders. Buy orders require only a USDC balance check, while sell orders require both spot asset and USDC balance checks.
* [Available Balance for Spot](/extended-resources/trading/unified-margin-and-balances.md#account-balances) (in USDC) = max(Equity − Spot Equity − Margin Reserved for Perps − Margin Reserved for Spot Buy Orders, 0)
* [Available Balance for Spot Sell](/extended-resources/trading/unified-margin-and-balances.md#account-balances) (in Asset) = Asset Balance − Balance Reserved for Spot Sell Orders
* Order Cost for Buy Orders (in USDC) = New Order Size \* New Order Price \* (1 + Fee Rate)
* Order Cost for Sell Orders (in Asset) = |New Order Size|
* Order Cost for Sell Orders (in USDC) = Open Loss + Haircut
  * Open Loss = |min(New Order Size \* (Index Price - New Order Price), 0)| + Fee
  * Haircut = New Order Size \* Index Price \* (1 - [Spot Contribution Factor](/extended-resources/trading/unified-margin-and-balances.md#collateral-assets))
  * Order Size is positive for Buy Orders and Negative for Sell Orders

### Order Cancellation Rules

Given that Available Balance for Trading Perps ≥ Available Balance for Spot (in USDC), and Available Balance for Spot Sell (in Asset) cannot decrease without user action, the following rules apply:

* If Available Balance for Spot (in USDC) becomes negative, all open spot buy orders are cancelled.
* If Available Balance for Trading Perps becomes negative, all open perpetual orders are cancelled, except for reduce-only orders.

Notes:

* Spot sell orders and perpetual reduce-only orders remain active unless the account becomes liquidatable.
* While Available Balance for Spot (in USDC) is negative, the user may not place new spot buy orders.
* While Available Balance for Trading Perps is negative, the user may only place orders that reduce the size of existing perpetual positions or sell spot assets.
