Basis trading spot-futures algorithm development

We design and develop full-cycle blockchain solutions: from smart contract architecture to launching DeFi protocols, NFT marketplaces and crypto exchanges. Security audits, tokenomics, integration with existing infrastructure.
Showing 1 of 1 servicesAll 1306 services
Basis trading spot-futures algorithm development
Complex
~1-2 weeks
FAQ
Blockchain Development Services
Blockchain Development Stages
Latest works
  • image_website-b2b-advance_0.png
    B2B ADVANCE company website development
    1214
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    852
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_logo-advance_0.png
    B2B Advance company logo design
    561
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    823

Development of Basis Trading Algorithm (Spot-Futures)

Basis trading — one of few DeFi strategies with predictable income source. Buy ETH spot, simultaneously short ETH perpetual futures on same volume. Delta-neutral position. Funding rate on perpetual — your income. When market bullish, longs pay shorts. In 2021-2023 this yielded 20-60% APY with no directional risk.

Sounds simple. But implementation that works without losses on sharp market moves, negative funding, and margin calls — this is engineering task.

Risk sources breaking simple implementations

Funding rate reverses

Funding rate on Binance Perp, dYdX, GMX, Hyperliquid — variable. On bullish market longs pay shorts (positive funding, you receive). On bearish — opposite (negative funding, you pay). If your algorithm can't exit position on regime change, negative funding will eat your capital.

Implementation: threshold on negative funding rate. If 8-hour funding goes below -0.01% three periods straight — algorithm begins position closure. Not instant (to not move market), but TWAP exit over several hours.

Liquidation of short position on pump

Delta-neutrality — mantra breaking on extreme moves. ETH up 30% per hour. Your short on perpetual loses margin faster than you can top up. If margin ratio falls below liquidation threshold — position forcibly closes. Spot remains. You go from delta-neutral to delta-long with liquidation loss (usually 0.5-1% of position as liquidation fee).

Solution — margin buffer. Keep perpetual margin ratio not at liquidation threshold, but with 2-3x buffer. On pump — auto-top-up margin from spot wallet or close part of spot position. Algorithm tracks unrealizedPnl and marginRatio in real-time and acts before exchange force-liquidates.

Roll management on dated futures

If using dated futures (not perpetuals), position expires. 3-7 days before expiry need "roll": close current contract and open next. Price difference between them — roll yield (can be positive or negative).

Automatic roller requires: monitoring time to expiry, calculating roll cost, executing two orders atomically (or as close as possible). On CEX this is relative order: sell current and buy next via calendar spread order if exchange supports (Binance, CME crypto). If not — risk of slippage between two separate orders.

Building algorithm

Choosing venues

Venue Type Funding Features
dYdX v4 Perpetual DEX 8h Decentralized, Cosmos-based
GMX v2 Perpetual DEX Hourly rate No orderbook, PnL vs LP pool
Hyperliquid Perpetual DEX 8h High liquidity, own L1
Binance Perp CEX 8h Largest volume, mature API
Bybit CEX 8h Good liquidity alt-perps

For pure DeFi approach: spot on Uniswap V3, short on dYdX v4 or Hyperliquid. For maximum liquidity and stability — Binance spot + Binance Perp (CEX risk, but minimal slippage).

Calculating funding PnL

# Funding per period = notional * funding_rate
# funding_rate at most exchanges — 8-hourly
funding_8h = position_size_usd * funding_rate_current
annualized_apy = funding_8h * 3 * 365  # 3 periods per day, 365 days

# Net APY accounting for roll and fees
net_apy = annualized_apy - entry_exit_fees - borrow_cost - roll_cost

Algorithm calculates net_apy for each potential position and enters only above threshold (e.g., 15% APY after all costs).

Execution engine

Opening position — not one order. Coordinated action:

  1. Check available liquidity on perpetual (bid/ask spread < threshold)
  2. Place limit order on spot (or market with minimal slippage)
  3. Immediately after spot executes — place short on perpetual on same volume
  4. If perpetual doesn't execute in N seconds — close spot, start over

Discrepancy between spot and perpetual execution creates temporary directional risk. On liquid pairs (BTC, ETH) — milliseconds. On less liquid altcoins — seconds, and price difference can be significant.

Use WebSocket connections to exchanges for real-time fills. REST API for order submission too slow on active market.

Position monitoring

Critical metrics to track in real-time:

  • delta = spot position + perpetual position (should be ~0)
  • margin_ratio perpetual (alert near 150% of liquidation threshold)
  • funding_rate current and 7-day moving average
  • funding_received_cumulative vs fees_paid_cumulative — actual PnL

PostgreSQL for history. TimescaleDB for time-series (funding rates, prices). Grafana for dashboard. PagerDuty or Telegram for alerts on margin call approach.

Timeline estimates

Algorithm for one trading pair on two CEX (spot + perp) with basic monitoring — 1-1.5 weeks. With multi-pair, multi-exchange logic, automatic roll management and full risk management — 2-3 weeks. Integration with on-chain DEX perpetuals (dYdX, GMX) adds complexity — separate estimate. Cost calculated after clarifying exchange integrations and risk requirements.