Advanced Trading

Market Making API
for AI Agents

Market makers earn money by providing liquidity โ€” posting limit orders on both sides of the book and earning the spread. It is one of the most consistently profitable strategies when done algorithmically. Purple Flea's trading API gives your agent everything it needs to quote markets professionally.

Start Market Making โ†’ Read the Guide
How It Works

Market Making Basics

The core mechanic is simple. The complexity is in the edge cases โ€” inventory accumulation, adverse selection, and spread management under volatility.

A market maker simultaneously posts a bid (buy order) below the mid-price and an ask (sell order) above the mid-price. When both sides fill, the maker earns the difference โ€” the spread.

With post-only orders, the market maker always pays the maker fee (or earns a maker rebate on exchanges that pay for liquidity provision). Purple Flea's API fully supports post_only=True, ensuring your orders never accidentally become market orders and incur taker fees.

The quoting loop runs continuously: check the current mid-price, cancel stale orders, post fresh bid and ask, repeat. The frequency of this loop determines how tightly you track the market. Most agents requote every 1โ€“5 seconds on liquid markets.

Spread width is the key parameter. Wider spreads mean each fill is more profitable but you fill less frequently. Narrower spreads give you more fills but thinner margin. The optimal spread depends on market volatility โ€” Purple Flea's API returns real-time volatility metrics to help your agent decide dynamically.

ETH-PERP Order Book
$3,524.500.45
$3,523.201.20
$3,521.00 โ† Your Ask0.01
Mid: $3,520.00
$3,519.00 โ† Your Bid0.01
$3,517.800.88
$3,516.402.10
Spread
$2.00 (5.7 bps)
Maker Rebate
0.02%
Integration

Production-ready quoting loop

A complete market-making loop in under 30 lines of Python using the Purple Flea SDK.

Market Making Bot โ€” Core Loop Python
import purpleflea
import asyncio

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

async def market_make(market: str, spread_bps: float = 10, size: float = 0.01):
    """Post bid and ask around mid-price."""
    snapshot = await book.get_snapshot(market)
    mid = (snapshot['best_bid'] + snapshot['best_ask']) / 2
    
    half_spread = mid * (spread_bps / 10000) / 2
    bid_price = round(mid - half_spread, 2)
    ask_price = round(mid + half_spread, 2)
    
    # Cancel existing orders
    await trader.cancel_all_orders(market=market)
    
    # Post new quotes
    bid = await trader.place_order(market=market, side="buy", size=size, price=bid_price, post_only=True)
    ask = await trader.place_order(market=market, side="sell", size=size, price=ask_price, post_only=True)
    
    print(f"Quoting {market}: bid ${bid_price} / ask ${ask_price} | spread {spread_bps}bps")

asyncio.run(market_make("ETH-PERP"))
Risk Management

Inventory Management

The primary risk of market making is inventory accumulation โ€” when one side of the book fills consistently and you build a directional position you didn't intend.

The Inventory Problem

When you quote a market and a large directional move occurs, only one side of your quotes fills. If BTC drops 2%, all your bids get hit but your asks never fill โ€” you now hold more BTC than intended, at a loss versus current price.

This is called adverse selection โ€” the trades you get are the ones market participants most wanted to do against you. Managing this is the primary skill of professional market makers.

Mitigation Strategies

  • โ†’Skew quotes: When long inventory accumulates, tighten the ask and widen the bid to encourage selling.
  • โ†’Inventory limits: Halt quoting when position exceeds a threshold. Resume when neutral.
  • โ†’Directional hedge: Open a small futures position opposite to accumulated inventory.
  • โ†’Widen during volatility: When volatility spikes, widen spreads to compensate for higher adverse selection risk.
0.02%
Maker Rebate
Earn on every filled limit order
Real-time
Order Management
Cancel, replace, and update in milliseconds
Live
Inventory Monitoring
Track net exposure in real time via API
Get Started

Start Market Making with Purple Flea

Register for a free API key and deploy your first market-making agent today. Full SDK, documentation, and strategy guides included.

Related APIs

Everything you need to run a complete market-making operation.