StarkEx Perpetual Trading overview

StarkEx Perpetual Trading enables running a decentralized, perpetuals exchange that provides its users with self-custody, and settles transactions in a trustless manner.

A perpetuals exchange is not intended to be a marketplace for NFTs. To create an NFT marketplace, use StarkEx Spot Trading.

A position includes a collateral asset and one or more synthetic assets. Unlike spot trading, a trader can hold a leveraged position, which enables trading in asset amounts that are greater than the amount of funds actually invested.


When you create a StarkEx-powered perpetuals exchange, you define the requirements for the initial margin and the maintenance margin.

The maintenance margin determines the amount of leverage that StarkEx enforces whenever executing a trade or liquidation transaction. When a position’s value falls below the maintenance margin, it is not well-leveraged. The maintenance margin is similar to total risk.

The application is responsible for enforcing the initial margin before sending a transaction to the StarkEx gateway. StarkEx enforces the maintenance margin.

Because StarkEx does not enforce the initial margin it is possible to circumvent the inital margin requirement by executing a Forced Trade.

Total value and total risk

The total value of a position is the sum of the value of the position’s collateral and synthetic assets, expressed in the collateral currency.

The total risk is a measurement that includes the total value of all synthetic assets in a position, and also takes into account a predetermined risk factor for each synthetic asset. As the risk factor increases, so does the total risk.

You, the operator, determine the risk factor according to your business logic, and include it in the general configuration for your StarkEx instance as risk_factor, which is a tiered list that varies depending on the amount of a given synthetic asset. It is equivalent to the term maintenance margin rate (MMR), where MMR is expressed as a percentage. For example, if the MMR is 0.7%, then risk_factor = 0.007. For more information on risk_factor, its syntax and its tiers, see risk_factor.

Total risk is related to the maintenance margin as follows:

  • When a position’s total value is equal to its total risk, it is exactly at the maintenance margin.

  • When a position’s total value is less than its total risk, it is below the maintenance margin.

  • When a position’s total value is greater than its total risk, it is above the maintenance margin.

This section describes total value and total risk in terms of values that are visible to the end-user. When providing these values to the StarkEx gateway, you need to convert them to internal StarkEx values. For more information, see Calculating internal StarkEx values for total value and Calculating internal StarkEx values for total risk.

In the following examples, consider that Alice holds the following position:


500 USDC


4 BTC. 1 BTC = 20,000 USDC. risk_factor = 0.5
6 ETH. 1 ETH = 1,000 USDC. risk_factor = 0.1

Example: Total value

The total value of Alice’s position is 86,500 USDC, calculated as follows:

500 + (4 * 20,000) + (6 * 1,000) = 86,500

To convert this value to a StarkEx internal value,

Example: Total risk

The total risk of Alice’s position is 40,600 USDC, calculated as follows:

(4 * 20,000 * 0.5) + (6 * 1,000 * 0.1) = 40,600

TV/TR checks

When Total_value ≥ Total_risk, StarkEx checks that the following is true:

Total_value * 232 ≥ Total_risk


  • Total_value is an unsigned 96-bit number

  • Total_risk is an unsigned 128-bit number

When Total_valuenew / Total_risknew ≥ Total_valueold / Total_riskold, we actually check that Total_valuenew * Total_riskold ≥ Total_valueold * Total_risknew.

The products (Total_valuenew * Total_riskold) and (Total_valueold * Total_risknew) are both 224 bit integers.

Requirements for maintaining a position

A position must be well-leveraged. That is, \(\text{total_value} \ge \text{total_risk}\), which is another way of saying that the total value of a position must be above the maintenance margin.

If \(0 \lt \text{total_value} \lt \text{total_risk}\), you can liquidate the position without the position owner’s signature by matching the position to another trader’s signed limit order that would result in the position becoming well-leveraged.

If \(\text{total_value} \lt 0\), you can deleverage the position by matching it to another position that has the opposite balance with respect to some assets, without signatures from either position owner.

Factors that can affect a position’s balance

The following factors can affect a position’s balance, potentially causing a position to be liquidated or deleveraged:

  • transactions that your user initiates, such as trades and transfers

  • funding ticks

  • oracle price ticks