# Trading Perps

On EMDX we use USDC as the stablecoin to collateralize positions. Given that the current EMDX version does not run over any L2, investors don´t need to make any deposit or to send a transaction as a condition to start trading, because having some USDC positive balance in their wallet is sufficient to open the desired position.

Mark Price is the price of the last trade on the EMDX market, while the Index Price reflects the underlying asset spot price average from multiple sources provided by an oracle infrastructure. In our case, we use the Chainlink infrastructure available on the Avalanche network. For more information about oracle price service you can check this link.

The Funding Rate is a mechanism that aims to close the gap between the Mark and the Index price, by encouraging a price convergence. The formula is the following:

$fundingRate = (TWAPmark-TWAPindex)/24$

The logic behind the math consists of creating an incentive for investors to enter into a position which is not the general trend of the market. In other words, when the mark price of an asset on EMDX trades at a premium to the Index price, the funding rate will be positive, thus, longs will pay shorts; conversely, if that asset is trading at a discount to the Index price, the funding rate will be negative, and shorts will pay longs. Therefore, those investors who are charged with funding payments, will tend to reduce or close their position, consequently it will help to maintain both prices reasonably close. As the formula reflects, the wider the gap between both prices the more expensive the rate to be paid/received by the investors.

Every user's address has a unique Collateral Account, while every product on the platform has a unique Margin Account, meaning that we use an isolated margin system. This information is displayed on the Position Tab within the Margin Column. The collateral deposited by the users (the "Margin") is used to back up open positions and is subject to a liquidation process in case the price of the underlying asset reaches the Liquidation Price.

To open a position the required Initial Margin is 10% of the Notional Position value, that is to say, if an investor wants to open a 1000 USDC position (long or short) the required minimum Initial Margin will be 100 USDC. The Maintenance Margin is the Margin required to keep a position open. On EMDX this Margin is set at 6.25% of the notional value of the open position, which means that the collateral deposited by the user must be greater than 6.25% of the notional value in order to avoid liquidations.

E.G., if a user opens a long position of 1000 USDC in BTC/USDC with an entry price of 10,000 USDC the initial margin required will be of 100 USDC (10%) and the maintenance margin of 62.5 USDC (6.25%). If the BTC price drops, let's say 3,75%, from 10,000 to 9,625 USDC, the collateral loss in the Margin Account will trigger the liquidation process given that the maintenance level will be equal or below its minimum of 62.5 USDC. [100 USDT - ($375*0.1) = 62.5 USD]

Leverage gives a trader the opportunity to multiply profits on successful trades while, on the other hand, leverage multiplies the potential downside risk in case a trade does not pan out. On EMDX the maximum level of Leverage available is 10x. While it's a very efficient capital management tool, EMDX recommends users to understand the implications and risks of opening leverage positions.

Margin Ratio is an inverse approach to the Asset/Collateral ratio and is expressed in the following formula:

****

$marginRatio=(margin +unrealizedPnL*)/positionNotional$

****

This ratio allows monitoring the level of collateral of an open position(s), thus, the risks of liquidation of a trade. This ratio also reflects the performance of an open position, since it adds or subtracts to the margin, the open profits or losses of the trade. The Notional Position is calculated by multiplying the Size (amount of asset bought or sold) by the Mark Price of the asset.

Leveraged positions, by definition, use borrowed capital, which is warrantied by the collateral in the trader's margin account. When the Maintenance Margin reaches, or falls below, the level of 6.25% a liquidation process is activated.

Currently, and until operations become stabilized on, once a trader's Maintenance Margin reaches a level of 6.25% or less of the Notional Value of the position, the position will be fully liquidated. Partial liquidation mechanisms will apply in the upcoming version.

The liquidation process is executed by an internal engine which is carried on by distributed programmatic modules, named "liquidator bots" or "keepers", and they are freely controlled by autonomous third parties that receive fees for executing that task.

****

$EffectiveLeverage = PositionSize * MarkPrice / Margin$

` `

****

$Accumulation(PositionSize * MarkPrice) / ReservedCollateral$

$PositionSize.abs * MarkPrice$

**Initial Margin**

$OpenNotional*0.10$

**Maintainance Margin**

$OpenNotional*0.0625$

**Margin Ratio**

****

$marginRatio=(margin +unrealizedPnL)/positionNotional$

**MaintenanceMarginRatio(*)**

****

$EntryPrice * PositionSize(*)$

****

$OpenNotional / PositionSize.abs$

****

$Margin / OpenNotional$

**MarkPrice**

****

$Current Amm Price (*)$

**IndexPrice**

$Oracle provided price$

**ClosePosPriceSlippage**

$(RealCloseQuoteAmount - CloseQuoteAmount) / CloseQuoteAmount$

****

$EntryPrice * (1-ReverseLeverage-ClosePosPriceSlippage+MaintenanceMarginRatio)$

****

$EntryPrice * (1 + ReverseLeverage - ClosePosPriceSlippage - MaintenanceMarginRatio)$

****

$fundingRate = (TWAPmark-TWAPindex)/24$

$funding Payment=positionSize * fundingRate$

**getInputPrice(side, quoteAssetAmount)**

Get input amount returns how many base assets you will get with the input quote amount.

****

**Function name:**

`getInputPrice(side, quoteAssetAmount)`

**Parameters:**

**quoteAssetAmount**: quote asset amount

**side:**long/short

**k =**

`baseAssetReserve * quoteAssetReserve`

For long position:

**quoteAssetReserveAfter**

= `quoteAssetReserve + quoteAssetAmount`

For short position:

**quoteAssetReserveAfter**

**=**

`quoteAssetReserve - quoteAssetAmount`

**baseAssetReserveAfter**= k / quoteAssetReserveAfter

**RETURN:**

`abs(baseAssetReserveAfter - baseAssetReserve)`

****

****

**getInputPrice(side, quoteAssetAmount)**

Get output price returns how many quote assets you will get with the input base amount.

****

**Function name:**getOutputPrice(side, baseAssetAmount)

**Parameters:**

**baseAssetAmount:**base asset amount

**side**: long/short

**k**= baseAssetReserve * quoteAssetReserve

For long position:

**baseAssetReserveAfter =**baseAssetReserve + baseAssetAmount

For short position:

**baseAssetReserveAfter =**baseAssetReserve - baseAssetAmount

**quoteAssetReserveAfter =**k / baseAssetReserveAfter

**RETURN:**abs(quoteAssetReserveAfter - quoteAssetReserve)

**notional**= collateral * leverage

**size =**

`getImputPrice( side, notional)`

**RETURN:**notional / size

****

**RETURN:**

`((entryPrice - marketPrice) / marketPrice) * 100`

**EfectiveLeverage**=

`PositionSize * MarkPrice / Margin`

**GlobalLeverage =**

`accumulation(PositionSize * MarkPrice) / ReservedCollateral`

Notional value of the position calculated based on entry price

**Open Notional**=

`PositionSize * EntryPrice`

**Position Notional**

{

k = quoteAssetReserve * baseAssetReserve

if long

baseAssetAfter = baseAssetReserve + positionSize

if short

baseAssetAfter = baseAssetReserve - positionSize

quoteAssetAfter = k / baseAssetAfter

RETURN: abs(quoteAssetAfter - quoteAssetReserve)

}

****

if long

**RETURN:**

`positionNotional - openNotional`

if short

**RETURN:**

`openNotional - positionNotional`

``

import Big from "big.js"

export function getLiquidationPrice(

leverage: Big,

margin: Big,

openNotional: Big,

positionSize: Big,

mmr: Big, // mmr: maintenanceMarginRatio

k: Big,

): Big {

// NOTE: return zero for the case of no liquidation price

// set 0.0001 as the deviation value

if (leverage.lte(1.0001)) {

return new Big(0)

}

const pn = positionSize.gte(0)

? margin.minus(openNotional).div(mmr.minus(1))

: margin.add(openNotional).div(mmr.add(1))

const x = positionSize.gte(0)

? positionSize

.mul(-0.5)

.add(positionSize.mul(pn).pow(2).add(pn.mul(k).mul(positionSize).mul(4)).sqrt().div(pn.mul(2)))

: positionSize

.mul(-0.5)

.add(positionSize.mul(pn).pow(2).minus(pn.mul(k).mul(positionSize).mul(4)).sqrt().div(pn.mul(-2)))

return k.div(x.pow(2))

}

****

**twapInterval =**

`1h`

**fundingPeriod =**

`1h`

**premium =**

`twapMarketPrice - twapIndexPrice`

**timeFraction =**

` fundingPeriod / 1 day`

**premiumFraction =**

`premium * timeFraction`

**fundingRate =**

** **

`premiumFraction / twapIndexPrice `

****

**(***)

*information directly fetched from the vAMM contract.*

Last modified 2mo ago