# Risk Management

Risk Management is a crucial aspect of derivatives to ensure that the protocol is always safe and resistant to economic manipulation. One major risk management step Hxro Network takes is to ensure that too much unrealized PnL cannot be used to post collateral and too much collateral cannot be withdrawn. The discussion below details how Hxro Network achieves this.

## Mark Pricing

The Last-Trade-Price, i.e., the LTP of the orderbook, does not influence the calculation of the mark price. The mark price for a market is calculated by taking the index price (Spot price of the underlying asset calculated via Oracles) and applying an EMA of a spread to it. This spread is calculated as the difference between the derivative instrument's index price and the mid price. This allows the mark price to move around the index price but prevents the variable spread from moving too quickly in the event traders try to manipulate the price on the instrument. See spread calculation on how the exponentially weighted spread is calculated.

## Midpoint Index Spread Calculation

One of the major problems in calculating the mark price is low liquidity. One can easily manipulate low liquidity markets and still influence the mark price calculation, although not as much as when the Last-Trade-Price influenced the mark price. To address this, the Hxro Network risk engine does not simply consider the top-of-the-book bids and asks for the calculation of the orderbook mid price. For context, the orderbook mid price is calculated as best\_bid / 2 + best\_ask / 2. There needs to be a qualifying market to calculate the mid price, which in turn is used in the calculation of the spread and the mark price. For there to be a qualifying market, the cumulative bids and the cumulative asks must be within a band of each other and should add up to equal to or more than a minimum size threshold.

In reality, the midpoint price is used to calculate the spread, and the mark price is the midpoint of the best bid and ask prices when both the above conditions are met. This also prevents bad actors from putting small orders on the book to extract information from order flow while taking on essentially no risk and influencing the mark price.

What happens if the band between the cumulative bids and asks is too wide or the size doesn't meet the required threshold? The exponentially weighted spread of the index price and the mark price stays constant from the last time there was a qualifying market.

For example, assume that the price of the BTCUSD asset fetched from Pyth oracles is $25,000. And the orderbook mid price reflects a price of $25,250. If the price moves dramatically such that either orders on the top of the book get filled, or market makers remove their quotes such that the price rises to $30,000. In such case, there would be no qualifying market, and the new mark price would be $30,250, reflecting the spread while the market would still be liquid (at $25,000 levels).

## Update frequency of the Spread

The spread's update frequency depends on the MPG activity and not actual clock time. There is also a threshold of 1 update to the spread per block. This ensures that the spread does not update too fast, allowing anyone to keep posting higher bids or lower asks progressively before allowing other traders to trade against those orders and influence the mark price that way. In theory, the spread between the index price and mid price is likely more stable than the mark price itself because of the index price component fetched from a liquid market. However, the spread can update much slower in practice than the 1 block standard if the order actions update very infrequently.

## Capping Unrealized PnL while adding margin

As understood from [margining](margining), unrealized PnL is counted as collateral for new positions just as USDC is (USDC is the only supported form of collateral as of now; see [Collateral](collateral) for more information). The Hxro Network risk engine ensures that too much unrealized PnL cannot be withdrawn. The margin requirement ensures that when a trader wants to add to a profitable position, the margin requirement also increases with size. This would decrease the unrealized PnL that can be withdrawn after increasing the position size. Why? Because the unrealized PnL before was counted as margin while adding to the position, the amount of new unrealized PnL after adding to the position.

## Orderbook Marking for Unrealized PnL

This specific logic governs withdrawals that are aimed at protecting the system. While cash balance and available margin are calculated using the mark price, users cannot simply withdraw excess unrealized PnL from an open position unless they can trade out of the position given the current bids or asks. Supposing that a user has a long position on a derivative, there must be sufficient buyers for the position for the user to withdraw his unrealized PnL made from the position. In case the user has a short position, there must be sufficient sellers for the position for the user to withdraw his unrealized PnL made from the position.

This mechanism can be thought of as a system accounting for price impact. Suppose the user's position cannot be fully exited due to a lack of liquidity on the other side. In that case, i.e., if the market impact exceeds the required margin on the position, they cannot withdraw the unrealized PnL but get to keep the deposited margin and abandon the position.

Another way to think about this mechanism is protection against bad mark prices because cash balance and margin are calculated using the current mark price. Hence, to prevent traders from exploiting the system when the mark prices stay wacky for a period, this system proves effective. Why? When a group of highly capitalized traders tries to withdraw excess PnL from a position that has been marked ineffectively to an erratic price, there is bound to be market impact preventing traders from withdrawing PnL and enabling withdrawals of only the required margin on the position.

Let's take a practical example to understand this. A trader longed BTCUSD-PERP of quantity 1 and a mark price of $25,000. The mark price increases to $50,000. He put up margin of $3k(leverage = \~8x). The cumulative best bid for 1 BTCUSD-PERP on the orderbook was $40,000. In normal scenarios, the exchange would allow the trader to withdraw PnL of $22,000($50,000 - $25,000 - $3,000). Hxro Network Dexterity would only allow PnL withdrawals up to $15,000($40,000 - $25,000) and not the typical full PnL of $22,000 at that instant.

## OI Caps

These open interest thresholds/caps are imposed at the TRG (see [TRG](trader-risk-groups-trgs)) level. Orders from a TRG would be blocked if they ever breach a certain open interest threshold. These thresholds don't kick in until open interest on the overall contract reaches a specific level. This is done in order to prevent the risk engine from classifying orders that normally do not prevent whale risk but are relatively large with respect to the contract's OI because the contract has just opened.

## Price Bands

Price bands can be specified as a spread around the current mark price of the asset. It prevents anyone from placing orders that are considered too wide from the current mark price. The width is dictated by the spread. The wider the spread, the wider the price bands, and the less conservative the system is for orders that may be wide enough from the mark price. As for the inverse, the thinner the spread, the thinner the price bands, and the more conservative the system is for orders that may be wide enough from the mark price.
