Implementing Margin Trading in a Mobile Exchange App
Margin trading uses borrowed funds. Users deposit collateral (margin) and the exchange provides leverage 3x, 5x, 10x. Profit and loss are calculated on the full position size, not collateral. If loss approaches the margin amount, the exchange force-closes the position (liquidation). Implementing this on mobile means building a complex real-time risk display system.
Margin Accounts: Isolated and Cross
Two modes with different logic:
Cross Margin — entire margin account balance serves as collateral for all positions. One position "eats" another's margin. Liquidation price is calculated for the whole account.
Isolated Margin — each trading pair has a separate isolated margin wallet. Maximum loss is capped at what's deposited in that wallet. For mobile UX, isolated margin is simpler: users understand they only lose what they deposited for that pair.
The UI must clearly distinguish these modes. A toggle with brief explanation (tooltip or bottom sheet with example) is mandatory.
Calculating Liquidation Price
Liquidation Price is the key indicator for a margin position. Users must see it before opening a position.
Simplified formula for ISOLATED LONG (Binance):
LiquidationPrice = EntryPrice × (1 - 1/Leverage + MaintenanceMarginRate)
Maintenance Margin Rate for most Binance pairs: 0.5–1.5% depending on tier.
// iOS — liquidation price calculation for isolated long position
struct MarginPositionCalculator {
static func liquidationPriceLong(
entryPrice: Decimal,
leverage: Int,
maintenanceMarginRate: Decimal = 0.005
) -> Decimal {
let leverageDecimal = Decimal(leverage)
return entryPrice * (1 - 1 / leverageDecimal + maintenanceMarginRate)
}
static func liquidationPriceShort(
entryPrice: Decimal,
leverage: Int,
maintenanceMarginRate: Decimal = 0.005
) -> Decimal {
let leverageDecimal = Decimal(leverage)
return entryPrice * (1 + 1 / leverageDecimal - maintenanceMarginRate)
}
}
Update liquidation price on each leverage change or margin adjustment. Display in red with distance percentage from current price — "Liquidation at 14.3% below current."
Margin Ratio and Margin Call
Margin Ratio = Maintenance Margin / Account Capital × 100%.
- Below 100% — account is safe
- 80–100% — yellow zone, push "Deposit additional margin"
- Above 100% — forced position closure (liquidation)
On the open position screen, display Margin Ratio as a progress bar with color gradient (green → yellow → red). WebSocket updates balance and P&L in real-time.
Borrowing and Repayment
Margin borrowing flow:
-
Borrow: user specifies amount and currency. Exchange returns
tranId. UI shows available limit, current interest rate (hourly/annual), max available. -
Repay: interest is paid first, then principal. Button "Repay All" calculates full repayment amount including accrued interest.
Binance API: POST /sapi/v1/margin/loan and POST /sapi/v1/margin/repay. Rates via GET /sapi/v1/margin/interestRateHistory.
Risk Notifications
Margin trading requires proactive alerts:
- Margin Ratio > 75% → push "Approaching liquidation on BTC/USDT"
- Margin interest accrual > X USDT → periodic reminder
- Position force-closed → immediate push with loss amount
Firebase Cloud Messaging with high priority for liquidation alerts — APNs apns-priority: 10, FCM priority: high. Regular priority can delay notifications minutes, critical for margin.
Open Position Screen
Open position card displays:
| Field | Update |
|---|---|
| Unrealized PnL (USDT and %) | Real-time WebSocket |
| Liquidation Price | On margin change |
| Margin Ratio | Real-time |
| Leverage | Static at opening |
| Entry Price / Mark Price | Real-time |
Mark Price (fair price via index) is different from Last Price. P&L and liquidation calculate from Mark Price, not Last Price.
Timeline and Scope
| Component | Timeline |
|---|---|
| Cross/Isolated toggle with explanations | 3 days |
| Order form with leverage and liquidation calc | 1 week |
| Borrowing and repayment + rate UI | 1 week |
| Real-time P&L and Margin Ratio in WebSocket | 1 week |
| Risk push alerts (Margin Call, Liquidation) | 3 days |
| Position history and trades | 3 days |
Minimum margin module: 4–5 weeks. Full system with Cross/Isolated, borrowing history, detailed analytics — 2–3 months.







