Lesson 123 min read

Build a Crypto Algo: A Delta-Neutral Funding Bot, End to End

November 05, 2026

// TL;DR

The capstone assembles the course into one delta-neutral loop: FETCH funding rate + spot + perp price; check the SIGNAL (funding above a threshold, basis attractive); run a RISK CHECK (size, leverage, liquidation distance, limits); ENTER NEUTRAL (short perp + buy equal spot so price moves cancel); then MONITOR (collect funding, exit if it flips) — re-checking each funding window, with a KILL-SWITCH that flattens BOTH legs on any error. The example earns funding without betting on direction. The realities that make or break it: keep leverage LOW so a spike can't liquidate the perp leg; watch liquidation price constantly; account for fees on both legs/exchanges (they can eat the whole edge); handle the two legs being on different venues; and exit cleanly when funding flips — a half-closed hedge is suddenly a naked leveraged bet. Boring, delta-neutral, small and paranoid beats clever and big. Start on a testnet.

This is the capstone. We assemble the whole course — market structure, perps, funding, the Greeks, risk, and the tooling — into one small, disciplined, delta-neutral crypto algo: a funding-capture bot. It does not predict BTC; it earns funding while staying market-neutral. Simple idea, demanding execution.

The architecture

A simple crypto algo, funding capture, end to end. Five steps in a loop. One, fetch: funding rate, spot and perp price. Two, signal: is funding above a threshold, is the basis attractive. Three, risk check: size, leverage, liquidation distance, limits. Four, enter neutral: short the perp plus long equal spot, hedged. Five, monitor: collect funding, exit if it flips. Re-check each funding window, with a kill-switch that flattens both legs on any error. The realities that make or break a crypto algo: the idea is simple — when funding is positive, short the perp and buy an equal amount of spot so price moves cancel and you collect funding delta-neutrally. The discipline is everything: keep leverage low so a spike can't liquidate the perp leg; watch your liquidation price constantly; account for fees on both legs and both exchanges which can eat the whole funding edge; handle the perp and spot being on different venues; and exit cleanly when funding flips negative. A kill-switch must flatten both legs together, because a half-closed hedge is suddenly a naked leveraged directional bet. Boring, delta-neutral, small and paranoid beats clever and big.

Every systematic crypto strategy runs the same loop you have seen throughout:

  1. Fetch — funding rate, spot price, perp price (Lesson 11).
  2. Signal — is funding above your threshold (and/or the basis attractive)?
  3. Risk check — size the position, check leverage, liquidation distance, and your limits. This gate can veto the trade.
  4. Enter neutral — short the perp and buy an equal amount of spot, so price moves cancel and you are delta-neutral (Lesson 8).
  5. Monitor — collect funding each window, and exit when funding flips negative or your rules say so.

…re-checking every funding window, wrapped in a kill-switch that flattens both legs the instant anything looks wrong.

A runnable skeleton

import ccxt

def run_once(ex_spot, ex_perp, cfg):
    funding = ex_perp.fetch_funding_rate(cfg.symbol)["fundingRate"]
    spot    = ex_spot.fetch_ticker(cfg.spot_symbol)["last"]

    # 2. SIGNAL — only trade when funding pays us enough (annualised)
    apr = funding * 3 * 365
    if apr < cfg.min_apr:
        return f"no trade — funding {apr:.0%} APR below threshold"

    # 3. RISK CHECK (veto gate)
    qty = size_position(cfg.capital, cfg.max_risk_pct, spot)
    if leverage_for(qty, spot, ex_perp) > cfg.max_leverage:
        return "skip — would exceed max leverage"
    if liquidation_distance(ex_perp) < cfg.min_liq_buffer:
        return "skip — liquidation too close"

    # 4. ENTER NEUTRAL — short perp + buy equal spot
    ex_perp.create_order(cfg.symbol, "market", "sell", qty, params={"reduceOnly": False})
    ex_spot.create_order(cfg.spot_symbol, "market", "buy", qty)
    return f"entered delta-neutral funding capture, {apr:.0%} APR"

# main loop — runs 24/7
while True:
    try:
        log(run_once(ex_spot, ex_perp, cfg))
        enforce_exits(ex_spot, ex_perp, cfg)       # exit if funding flips
        check_margin_and_liquidation(ex_perp, cfg)
    except Exception as e:
        kill_switch(ex_spot, ex_perp)              # flatten BOTH legs, stop
        alert(f"KILLED: {e}")
        break
    sleep(cfg.poll_seconds)

As with the options course, notice how little of this is “clever”. The signal is two lines. The overwhelming majority is risk checks, margin monitoring, exits and the kill-switch — and that is exactly right. In a live crypto algo, the discipline is the strategy.

The realities that make or break it

  • Keep leverage LOW. The short-perp leg uses margin; a sharp rally can liquidate it and leave you long spot only — now you are naked, directional, and losing. Low leverage and a big liquidation buffer are survival.
  • Watch the liquidation price constantly. This is a 24/7 trade; margin must be monitored around the clock, with auto top-up or auto-exit before liquidation.
  • Fees can eat the whole edge. Two legs, entry and exit, often on two exchanges — model every fee precisely. A 20% APR that becomes 5% after fees may not be worth the risk.
  • Two venues, carefully. Spot on one exchange, perp on another means transfer latency and venue risk on both. A delay while moving collateral can leave you unhedged.
  • Exit cleanly, together. When funding flips, close both legs. A kill-switch that flattens both at once is non-negotiable — a half-closed hedge is instantly a naked leveraged bet, which is exactly the thing you were avoiding.
  • Start on a testnet, then tiny. Paper-trade the full loop for weeks, then one small position, scaling glacially.

The course in one line

Crypto takes the options and derivatives you know and drops them into a 24/7, 4–5× more volatile, largely unregulated, API-driven market — so the maths is familiar, the opportunities (perps, funding, basis, fat premium) are real, and the risks (liquidation, exchange failure, depeg, custody) can ruin you regardless of your analysis. Automate the boring parts like the programmer you are, keep leverage low and size small, prefer delta-neutral and defined-risk, and make survival the strategy.

You now have the foundations — crypto market structure, perps and funding, options, volatility, strategies, basis trades, DeFi, risk, and the tooling to build. Go and paper-trade on a testnet, patiently, and stay paranoid.

For more, revisit the Options Trading for Programmers course for the options fundamentals, the Low-Latency Trading Systems course for execution, the algo trading vocabulary, and position sizing with Kelly.