# Trading Suspension

Trading suspension is a protective mechanism that temporarily halts certain trading activities while maintaining
critical system functions. This documentation explains when suspension occurs, what operations are restricted, and how
the system resumes normal operations.

## Explanation

### When Suspension Occurs

Suspension can be triggered by:

- **Manual intervention**: Administrators can manually suspend trading for maintenance, system upgrades, or emergency
  situations. 
  - Call the `PUT ExchangeWideControls/SuspendTrading` endpoint to manually suspend
  - Call the `PUT ExchangeWideControls/ResumeTrading` endpoint to manually unsuspend
- **Engine incorrectly configured**: The engine doesn't have one of the following critical accounts
  - Liquidator account
  - Fees account
  - Penny Jar account
- **Message Cache Full**: If messages are not purged fast enough. If the age of the oldest message exceeds a pre-configured maximum age, the exchange goes into suspended mode until the situation is remediated 
- **On Engine Restart**: If configured to do so, the engine can restart in either suspended or suspended post-only mode for a pre-configured duration, after which it will automatically unsuspend.

### Operations During Suspension

When the exchange enters a suspended state:

#### Allowed Operations

- **Order cancellations**: Traders can cancel their existing open orders
- **Mark price updates**: The system continues to process and update mark prices from price feeds
- **Position tracking**: All positions continue to be tracked and valued at current mark prices

#### Restricted Operations

- **New orders**: No new orders can be placed (market, limit, or any other order type)
- **Order modifications**: Existing orders cannot be modified
- **Stop orders**: Stop orders will not be triggered while the exchange is suspended, even if the mark crosses the stop price.
- **Liquidations**: The liquidation process is paused and will not execute even if accounts fall below their maintenance
  margin requirements

#### Post-only Suspension

In this special case, post-only orders are still permitted to go through. This allows market makers to continue posting liquidity in preparation for the resumption of trading. Everything else follows the same principles as a regular suspension.
- Call the `PUT ExchangeWideControls/SuspendTradingPostOnly` endpoint to manually suspend in post only mode

### Mark Price Processing

During suspension, the matching engine continues to:

- Receive mark price updates from external price feeds
- Update position valuations based on current mark prices
- Calculate unrealized profits and losses
- Monitor margin levels (without triggering any liquidations)

This ensures that when trading resumes, all positions are accurately valued and the system has the most recent market
data. 

### Liquidation Resumption

Upon unsuspension, the system processes liquidations as follows:

1. **Price Lock**: Uses the last mark price received while the exchange was suspended
2. **Queue Processing**: Processes all accounts in the pending liquidation queue in the order they became
   undercollateralized
3. **Standard Process**: Each liquidation follows the normal liquidation workflow using the locked mark price
4. **Insurance Fund**: If necessary, the insurance fund absorbs losses as in normal operations

This approach ensures that:

- Liquidations occur at fair prices (the last known market price)
- No accounts gain an unfair advantage from the suspension period
- The system remains solvent and properly collateralized
