Cash-and-carry arbitrage 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
Cash-and-carry arbitrage 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 cash-and-carry arbitrage algorithm

In February 2024 annualized basis on BTC perpetual futures on Binance consistently held at 15-18% annually. At the same time spot BTC could be bought, short opened on perp, and fixed 15%+ risk-free yield — regardless of price direction. This is cash-and-carry arbitrage: capturing the spread between spot and derivative with no directional risk. Implementation looks simple. Details are hidden in basis management, funding rate dynamics and expiration pricing.

Basis mechanics and yield sources

Where the spread comes from

Cash-and-carry works on divergence between spot price of asset and futures or perpetual price. For dated futures basis determined by cost of carry formula:

F = S × e^(r-q)×T

where S — spot price, r — risk-free rate, q — asset yield, T — time to expiration. On crypto market risk-free rate historically higher than traditional assets, so futures trade in contango (F > S) most of the time.

For perpetual futures basis maintained through funding rate: when perpetual trades above spot, longs pay shorts every 8 hours (on Binance). Funding rate size — direct yield source for cash-and-carry trader holding long spot + short perp.

Risks that aren't obvious

Funding rate reversal. Funding rate can become negative. In bear market or sharp rise in short open interest shorts start paying longs. Strategy turns from profitable to losing. Algorithm must track funding rate dynamics and close position on drop below threshold (accounting for fees and slippage on close).

Liquidation asymmetry. Short on futures has unlimited loss theoretically, long spot capped at zero. On sharp price rise (30% in hour, like some altcoin events) margin on short futures may not suffice even if long spot in profit. Strategy technically delta-neutral but requires sufficient margin buffer. Rule: margin on short ≥ 2x from maximum historical daily move of asset.

Exchange counterparty risk. Holding both spot and futures on one exchange — freeze risk (FTX case, 2022). Neutralized through cross-exchange strategy: spot on one exchange (or DeFi), futures on another. But adds complexity in collateral management.

Basis divergence on expiration. For dated futures theory says at expiration F → S. In practice 1-2 days before expiration basis can widen from demand imbalance — especially in altcoins with low liquidity. Algorithm must account for optimal exit time.

Algorithm architecture

System components

Data aggregator. Collects in real time:

  • Spot prices from multiple sources (Binance spot, Coinbase, Kraken) to avoid manipulation
  • Futures/perp prices and open interest
  • Current and predicted funding rate (Binance publishes predicted funding rate 8 hours ahead)
  • Historical funding rate data for calculating rolling annualized yield

Basis calculator. Normalizes data and calculates:

  • Current basis in % and annualized form
  • Estimated yield accounting for entry/exit fees, funding payments
  • Break-even holding period

Position manager. Opens and closes positions based on basis calculator signals. Ensures delta-neutrality: long spot size = notional value of short futures.

Risk monitor. Continuously tracks margin ratio on futures position, funding rate trend, basis anomalies. On condition violation — automated exit.

Net yield calculation

Real strategy yield after all costs:

Net APY = Funding Rate (annualized)
        - Trading fees (entry + exit, both legs)
        - Slippage (bid-ask spread × 2)
        - Borrowing cost (if using margin)
        - Opportunity cost on margin collateral

Example calculation for BTC cash-and-carry on Binance (April 2024):

  • Funding rate: 0.01% every 8h = 10.95% APY
  • Taker fees: 0.04% × 4 operations = 0.16% round trip
  • Slippage: ~0.03% × 2 = 0.06%
  • Net yield ≈ 10.73% APY before tax

With funding rate 0.03% (happened in bull market 2021) — net yield rises to 30%+ APY.

Rolling dated futures

Working with quarterly/monthly futures needs rollover algorithm: close expiring contract and open next. Optimal rollover time — 3-5 days before expiration when next contract basis normalized but current liquidity still adequate.

Automatic rollover accounts for:

  • Basis difference between current and next contract (if next in larger contango — rollover beneficial, if smaller — cost)
  • Current liquidity of both contracts
  • Spread on both contracts simultaneously (risk: basis can shift while closing first contract)

To minimize last risk use TWAP execution: close current and open new in parallel with small orders.

On-chain implementation: DeFi version of strategy

On DeFi markets cash-and-carry realized through:

Pendle Finance. Protocol splits yield-bearing tokens into Principal Token (PT) and Yield Token (YT). Buying PT at discount to par + hedging through Pendle AMM — cash-and-carry analog with fixed yield to maturity.

Perps on dYdX / GMX v2 / Synthetix Perps. Long spot through Aave/Compound (staked ETH or cbETH) + short ETH perp on dYdX. Spot position earns staking yield (~4% APY), short collects funding rate.

On-chain version adds gas costs and oracle risk, but removes exchange counterparty risk.

Development process

Analytics (3-5 days). Backtesting on historical data: Binance historical funding rates available through API for 2+ years. Model strategy on different market conditions, determine entry/exit thresholds.

Core development (1-2 weeks). Data aggregator, basis calculator, position manager. Integration with exchange APIs (CCXT as universal library or direct WebSocket connections for latency).

Risk management and monitoring (1 week). Automatic exit on funding rate < threshold, margin call protection, Telegram alerts.

Paper trading (3-5 days). Run without real money on production data. Check calculations, latency, edge cases.

Deployment. Testing with small amounts, gradual position increase.

Timeline estimates

Algorithm for one pair on one exchange with basic risk management — 2-3 weeks. Multi-asset, multi-exchange strategy with automatic rollover — 4-8 weeks. DeFi version through on-chain protocols — additional 2-3 weeks. Cost calculated individually.