Skip to content

3. Margin Trading

Sasha Nechaiev edited this page Sep 19, 2021 · 1 revision

Overview

The collateralized margin trading protocol allows users to trade leveraged long or short Forex pairs such as EURUSD, commodities such as XAUUSD, stocks such as APPL, and crypto-assets such as BTC-USD.

There is no order book, each trade initiated by the trader is instantaneously executed by the chosen liquidity pool. Future versions will allow trades split across multiple pools for the best price/risk, and currently, you can only trade with one pool at a time. Liquidity pools will enjoy earning spreads of each trade, but the catch is that they need to hedge the positions somehow and comply with a set of risk parameters set out by the protocol to safeguard the interests of all traders. Read more here.

Margin Trading on Turbulence Test Network

The guide below will illustrate the following functionalities

  • Check available margin liquidity pools
  • Check Check Pool Balance & Risks
  • Check pricing
  • Deposit balance into the chosen liquidity pool
  • Open Position, Close Position
  • Check trader profits/losses and risks

Via Console

Margin Liquidity Pool

Check Available Pools & Pairs

Check Query chain state baseLiquidityPoolsForMargin.nextPoolId(): LiquidityPoolId to check available pools.

Query chain state marginLiquidityPools.tradingPairOptions(TradingPair) to check available pairs

Query chain state marginLiquidityPools.poolTradingPairOptions(LiquidityPoolId, TradingPair) to check available leverage

Check Pool Balance & Risk

Query chain state marginProtocol.balances(LiquidityPoolId) to check balance.

To see the current Equity to Net Position Ratio (ENP) & Equity to Longest Leg Ratio (ELL), you can use RPC call:

curl -H 'Content-Type: application/json' --data \
'{ \
    "jsonrpc":"2.0", \
    "method":"margin_poolState", \
    "params":[<POOL_ID>], "id":1 \
 }' \
<ENDPOINT_URL>

Replace <POOL_ID> and <ENDPOINT_URL> with valid data.

Read more on Liquidity Pool risk profiles here.

Check pricing

Check Use RPC oracle.getAllValues() for up-to-date oracle pricing. Each liquidity pool would employ varied bid and ask spread for given margin trading pairs. Query chain state marginLiquidityPools.liquidityPoolOptions(LiquidityPoolId, TradingPair) to check spreads.

bidPrice = price - bidSpread
askPrice = price + askSpread

Deposit balance into chosen liquidity pool

Check Before trading, you will need to deposit a certain amount of USD into a pool. You can also withdraw the free balance (the amount that has yet to be used as margins) at any time.

Open Position, Close Position

Check Use marginProtocol.openPosition(pool_id, pair, leverage, leveraged_amount, price) to open a leveraged position. Note the price for a long position is the maximum price you can accept, while for a short position is the minimum you can accept.

Check Use marginProtocol.closePosition(position_id, price) to close a leveraged position. Note that the price for a long position is the minimum price you can accept, while for a short position is the maximum you can accept.

Check trader profits/losses and risks

To obtain the trader's risk profile you call:

curl -H 'Content-Type: application/json' \
--data '{ \
    "jsonrpc":"2.0", \
    "method":"margin_traderState", \
    "params":["<TRADER_ADDRESS>", 0], "id":1 \
  }' \
<ENDPOINT_URL>

Replace <TRADER_ADDRESS> and <ENDPOINT_URL> with valid data.

More details here on trader's risk profile.

DApp UI User Guide

Deposit before trade

Navigate to the Margin Trading page. You need to deposit some balance before trading, as it will be served as margins.

The main page displays all liquidity pools available, future versions will abstract away the complexity and only show an aggregated view. But this version allows users to manually choose and trade with a particular liquidity provider based on their preference e.g. spread, risk profile etc.

  • Click on Fast Buy/Sell button
  • Click on Deposit button to deposit USD (ticker: aUSD powered by Acala Dollar)

deposit

Fast Buy/Sell

From the main Margin Trading page

  • Click on Fast Buy/Sell button
  • Choose leverage e.g. x10
  • Enter leveraged amount e.g. 100 aUSD. Note that this is the leveraged amount, the margin required for opening this position (say for x10 leverage) would be 10 aUSD
  • Then go Buy/Long or Sell/Short

buy

Advanced Trading

Click on a trading pair of interest e.g. fEURUSD to go into the advanced trading page.

Be aware of your risk profile e.g. your margin level will need to be above Margin Call Threshold, otherwise no new positions are allowed. If your margin level falls below the Stop Out Threshold, then force closure will happen.

buy

Risk Profiles

Pool Risk Profile pool

To understand a liquidity pool's risk profile, read more here.

Trader Risk Profile trader

To understand a trader's risk profile, read more here.