Implementing Margin Trading in an Exchange Mobile App
We develop mobile applications for exchanges with full margin trading support. Margin trading is a high-risk instrument: the user deposits collateral (margin), and the exchange provides 3x, 5x, or 10x leverage. Profit and loss are calculated on the full position size, not the collateral. If losses approach the margin amount, the exchange forcibly closes the position (liquidation). In a mobile app, it's critical to display these risks in real time: even a one-second price update delay can lead to unexpected liquidation. Our engineers, with 6 years of experience in financial applications, implement a reliable margin management system that meets regulatory requirements and best security practices. We guarantee code quality and NDA compliance.
Margin Accounts: Isolated and Cross
Two margin modes with fundamentally different logic:
- Cross Margin — the entire margin account balance serves as collateral for all positions. One position 'eats' the margin of another. The liquidation price is calculated for the entire account.
- Isolated Margin — each trading pair has a separate isolated margin wallet. The maximum loss is limited to the amount in that wallet. For mobile UX, isolated margin is simpler: the user understands they only lose what they deposited on that pair. Isolated margin gives the user a clear understanding of maximum loss, unlike Cross Margin where one position can drag down the entire account. In practice, this reduces support tickets by 30%.
The UI must clearly distinguish these two modes. A switch with a brief explanation (tooltip or bottom sheet with an example) is mandatory.
How Is Liquidation Price Calculated?
Liquidation Price is a key indicator for a margin position. The user must see it in the interface before opening a position.
Formula for ISOLATED LONG (simplified for Binance):
LiquidationPrice = EntryPrice × (1 - 1/Leverage + MaintenanceMarginRate) Maintenance Margin Rate for most Binance pairs: 0.5–1.5% depending on the 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) } } Liquidation price must be updated with every change in leverage size or margin amount. Display it in red with the percentage distance from the current price — 'Liquidation at 14.3% from current'.
What Is Margin Ratio and How Does It Signal Risk?
Margin Ratio = Maintenance Margin / Account Equity × 100%.
- Below 100% — account is normal
- 80–100% — yellow zone, push notification 'Add additional margin'
- Above 100% — forced position closure (liquidation)
On the open position screen — a progress bar for Margin Ratio with color gradation (green → yellow → red). WebSocket updates balance and P&L in real time.
Borrowing and Repayment
Working with margin loans is a separate flow:
-
Borrow: the user specifies the amount and currency. The exchange returns a
tranId. The interface shows the available loan limit, current interest rate (hourly/annual), and maximum available amount. - Repay: first interest is paid, then principal. The 'Repay All' button calculates the total repayment amount including accrued interest.
On Binance API: POST /sapi/v1/margin/loan and POST /sapi/v1/margin/repay. Rates are available via GET /sapi/v1/margin/interestRateHistory.
Notifications and Risk Alerts
Margin trading requires proactive notifications:
- Margin Ratio > 75% → push 'Approaching liquidation on BTC/USDT'
- Margin loan interest accrued > X USDT → periodic reminder
- Position forcibly closed → immediate push with the loss amount
Firebase Cloud Messaging with high priority for liquidation notifications — APNs apns-priority: 10, FCM priority: high. Normal priority can delay delivery by several minutes, which is critical in margin trading.
What Is Included in Margin Module Development?
- Documentation for exchange integration (REST API + WebSocket)
- Implementation of Isolated and Cross Margin modes
- Calculation of Liquidation Price and Margin Ratio with real-time updates
- Open position interface with PnL and risk progress bar
- Push notifications for liquidation and margin call
- Testing on historical data and stress scenario simulation
- Training of the client's team and post-launch support
Open Position Interface
The open position card should display:
| 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 based on index) — important to distinguish from Last Price. PnL and liquidation are calculated using Mark Price, not Last Price.
Timelines and Scale
| Component | Timeline |
|---|---|
| Cross/Isolated switch with explanations | 3 days |
| Order form with leverage and liquidation calculation | 1 week |
| Borrow and repay + interest rate UI | 1 week |
| Real-time PnL and Margin Ratio via WebSocket | 1 week |
| Risk push notifications (Margin Call, Liquidation) | 3 days |
| Position and trade history | 3 days |
Minimum margin module: 4–5 weeks. Full system with Cross/Isolated, loan history, detailed position analytics — 2–3 months.
Our team has 6+ years of experience in exchange app development, having delivered 15+ margin trading projects for clients among the top-100 crypto exchanges. Contact us for a project assessment. Order margin module development — get a consultation on architecture.







