7 min read ยท March 4, 2026 ยท Purple Flea Team

Market Making with AI Agents: A Beginner's Guide

Market making is one of the oldest and most consistently profitable strategies in financial markets. With AI agents and programmatic APIs, it has become accessible to anyone with a bit of Python knowledge. This guide covers everything you need to build your first market-making agent using Purple Flea's trading API โ€” from understanding the core mechanic to managing inventory risk in production.

In this guide
  1. What is market making?
  2. How bid/ask spreads generate income
  3. Setting up a basic quoting loop
  4. Inventory management
  5. Adverse selection risk
  6. When to widen or narrow spreads
  7. Conclusion

What is market making?

A market maker is any participant who simultaneously posts a bid (buy order) and an ask (sell order) in an order book, creating liquidity that other traders can consume. Exchanges reward this behavior because without market makers, order books would be thin, spreads would be enormous, and the trading experience for everyone would be worse.

In traditional finance, market making was reserved for large institutions with seats on exchanges. In crypto, it is open to anyone via API. AI agents are particularly well-suited because market making requires speed, consistency, and the ability to respond to changing conditions around the clock โ€” exactly what agents do best.

The core loop is simple: check the current mid-price, cancel stale quotes, post a fresh bid slightly below mid and a fresh ask slightly above mid, repeat every few seconds. The profit comes from the spread captured each time both sides fill.

How bid/ask spreads generate income

Imagine ETH is trading at a mid-price of $3,500. You post a bid at $3,497 and an ask at $3,503. The spread is $6, or roughly 17 basis points (bps). When a market buyer hits your ask and a market seller hits your bid, you have bought at $3,497 and sold at $3,503 โ€” earning $6 per ETH regardless of which direction ETH moves.

On a liquid market, both sides might fill dozens of times per day. If your size is 0.1 ETH and you capture the $6 spread ten times per day, that is $6 per day from a $350 position โ€” roughly 0.6% daily or over 200% annualized before fees. Real-world numbers are lower due to adverse selection (explained below), but the principle holds.

On exchanges that offer maker rebates, the income is even higher. Purple Flea's trading API offers a 0.02% maker rebate on all filled limit orders, which compounds on top of the spread income. A market maker effectively earns: spread captured + maker rebate on both sides.

Key concept: Market making profit comes from spread capture, not directional prediction. A successful market maker profits in both bull and bear markets โ€” as long as both sides of their quotes keep filling.

Setting up a basic quoting loop

Here is a complete quoting loop using Purple Flea's Python SDK. The function fetches the current order book snapshot, computes bid and ask prices at a configurable spread, cancels any existing orders, and posts fresh quotes using post_only=True to guarantee maker execution.

Basic Quoting Loop Python
import purpleflea
import asyncio

trader = purpleflea.TradingClient(api_key="YOUR_KEY")
book = purpleflea.MarketDataClient(api_key="YOUR_KEY")

async def requote(market: str, spread_bps: float, size: float):
    snap = await book.get_snapshot(market)
    mid = (snap['best_bid'] + snap['best_ask']) / 2
    half = mid * (spread_bps / 10_000) / 2
    await trader.cancel_all_orders(market=market)
    await trader.place_order(market=market, side="buy", size=size,
        price=round(mid-half, 2), post_only=True)
    await trader.place_order(market=market, side="sell", size=size,
        price=round(mid+half, 2), post_only=True)

async def run_mm(market="ETH-PERP", spread_bps=10, size=0.01):
    while True:
        await requote(market, spread_bps, size)
        await asyncio.sleep(3) # requote every 3 seconds

asyncio.run(run_mm())

The post_only=True flag is critical. Without it, if the market has already moved and your "bid" is above the current best ask, your order would execute as a market order โ€” paying the taker fee and potentially buying at a worse price than intended. Post-only orders cancel automatically if they would cross the book, protecting you from accidental taker fills.

Inventory management

The most common failure mode for new market makers is ignoring inventory. When a strong directional move occurs, only one side of your book fills. If ETH drops 3%, all your bids get hit โ€” you accumulate long ETH positions at prices above the current market. Your open P&L goes negative even though your spread income is positive.

Professional market makers solve this with inventory skewing: when net inventory is too long, shift both bid and ask prices downward to encourage selling. When net inventory is too short, shift prices upward to encourage buying back. The quotes stay balanced around a skewed mid rather than the true market mid.

A simple version of inventory skew in Python:

Inventory-Aware Quoting Python
async def requote_with_skew(market, spread_bps, size, max_inventory=0.5):
    snap = await book.get_snapshot(market)
    position = await trader.get_position(market)
    net = position['net_size'] # positive = long, negative = short

    mid = (snap['best_bid'] + snap['best_ask']) / 2
    # Skew: shift mid down if long, up if short
    inventory_ratio = net / max_inventory
    skew_bps = -inventory_ratio * spread_bps * 0.5 # up to half spread
    skewed_mid = mid * (1 + skew_bps / 10_000)

    half = mid * (spread_bps / 10_000) / 2
    await trader.cancel_all_orders(market=market)
    await trader.place_order(market=market, side="buy", size=size,
        price=round(skewed_mid-half,2), post_only=True)
    await trader.place_order(market=market, side="sell", size=size,
        price=round(skewed_mid+half,2), post_only=True)
    print(f"Net: {net:+.4f} | Skew: {skew_bps:+.1f}bps | Mid: {skewed_mid:.2f}")

Adverse selection risk

Adverse selection is when the trades you receive are the ones most unfavorable to you. When an informed trader knows that ETH is about to drop โ€” because they have read a news event you haven't โ€” they will aggressively sell into your bid. You buy at a price that is immediately stale. Every fill you take is against someone who is more informed than you in that moment.

The mitigation is not to eliminate adverse selection โ€” it is unavoidable โ€” but to make your spread wide enough that the spread income outweighs the average adverse selection loss. Empirically, this requires monitoring your "realized spread" โ€” the difference between what you bought at and what you could sell at 30 seconds later. If your realized spread is consistently negative, your quoted spread is too narrow for that market's level of adverse selection.

When to widen or narrow spreads

Fixed-spread market making is a starting point, not a finished strategy. The optimal spread varies with market conditions, and your agent should adapt in real time.

Rule of thumb: Start with a spread of 10โ€“20 bps on liquid perpetual markets. Track your realized spread over 24 hours. If negative, widen by 5 bps. Repeat until realized spread is consistently positive. Then begin narrowing to optimize fill rate.

Conclusion

Market making is one of the most mechanically consistent strategies available to AI agents. Unlike directional trading, you do not need to be right about where price goes โ€” you only need both sides of your quotes to fill with reasonable frequency. The challenges are operational: managing inventory, tuning spreads to market conditions, and avoiding adverse selection during information events.

Start small, measure your realized spread, and iterate. Purple Flea's trading API provides everything you need โ€” post-only orders, real-time order book snapshots, position queries, and maker rebates โ€” to run a production market-making agent from day one.