Skip to main content

Overview

Market orders allow you to trade immediately at the current market price. Unlike limit orders, market orders prioritize execution speed over price control.

How Market Orders Work

Market orders consume liquidity from the order book:
  • Market Buy: Purchases shares for a specified dollar amount
  • Market Sell: Sells a specified number of shares
The order matches against the best available prices until the specified amount is filled or the order book is exhausted.
Market orders can experience slippage in thin markets. Always review the order book depth before executing large market orders.

Prerequisites

  • Initialized ClobClient with valid credentials
  • USDC balance and approved allowances (for buys)
  • Token shares in your wallet (for sells)
  • Understanding of FOK and FAK order types

Order Execution Types

FOK (Fill-or-Kill)

The order must be completely filled immediately, or it’s rejected:
import { OrderType } from "@polymarket/clob-client";

const result = await clobClient.createAndPostMarketOrder(
  {
    tokenID: YES_TOKEN,
    amount: 100, // $100 USDC
    side: Side.BUY,
    orderType: OrderType.FOK,
  },
  { tickSize: "0.01" },
  OrderType.FOK
);
Use FOK when you need guaranteed full execution. If the entire order can’t be filled immediately, it will be rejected with no partial fills.

FAK (Fill-and-Kill)

The order fills as much as possible immediately, then cancels the remainder:
const result = await clobClient.createAndPostMarketOrder(
  {
    tokenID: YES_TOKEN,
    amount: 100, // $100 USDC
    side: Side.BUY,
    orderType: OrderType.FAK,
  },
  { tickSize: "0.01" },
  OrderType.FAK
);
Use FAK when partial fills are acceptable. Any unfilled portion is immediately canceled.

Market Buy Orders

Market buy orders specify the dollar amount to spend (not the number of shares):
import { ClobClient, Side, OrderType } from "@polymarket/clob-client";
import { ethers } from "ethers";

const YES_TOKEN = "71321045679252212594626385532706912750332728571942532289631379312455583992563";

// Buy $100 worth of YES shares at market price
const result = await clobClient.createAndPostMarketOrder(
  {
    tokenID: YES_TOKEN,
    amount: 100, // Spend $100 USDC
    side: Side.BUY,
    orderType: OrderType.FOK,
  },
  { tickSize: "0.01" },
  OrderType.FOK
);

console.log("Market buy executed:", result);
{
  "success": true,
  "orderID": "0x789abc...",
  "status": "FILLED",
  "matchedAmount": "100",
  "remainingAmount": "0",
  "averagePrice": "0.52",
  "sharesReceived": "192.30"
}
The response shows:
  • matchedAmount: Total USDC spent ($100)
  • averagePrice: Weighted average execution price ($0.52)
  • sharesReceived: Number of shares purchased (≈192.3 shares)

Market Sell Orders

Market sell orders specify the number of shares to sell:
const YES_TOKEN = "71321045679252212594626385532706912750332728571942532289631379312455583992563";

// Sell 110 YES shares at market price
const result = await clobClient.createAndPostMarketOrder(
  {
    tokenID: YES_TOKEN,
    amount: 110, // Sell 110 shares
    side: Side.SELL,
    orderType: OrderType.FOK,
  },
  { tickSize: "0.01" },
  OrderType.FOK
);

console.log("Market sell executed:", result);
{
  "success": true,
  "orderID": "0xdef456...",
  "status": "FILLED",
  "matchedAmount": "110",
  "remainingAmount": "0",
  "averagePrice": "0.48",
  "usdcReceived": "52.80"
}
The response shows:
  • matchedAmount: Total shares sold (110)
  • averagePrice: Weighted average execution price ($0.48)
  • usdcReceived: Total USDC received ($52.80)

Complete Example

Here’s a full working example with both buy and sell market orders:
import { ClobClient, Side, OrderType, ApiKeyCreds, Chain } from "@polymarket/clob-client";
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";

dotenvConfig();

async function main() {
  const wallet = new ethers.Wallet(process.env.PRIVATE_KEY!);
  const chainId = parseInt(process.env.CHAIN_ID || Chain.POLYGON.toString()) as Chain;
  
  console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`);

  const host = process.env.CLOB_API_URL || "https://clob.polymarket.com";
  const creds: ApiKeyCreds = {
    key: process.env.CLOB_API_KEY!,
    secret: process.env.CLOB_SECRET!,
    passphrase: process.env.CLOB_PASS_PHRASE!,
  };

  const clobClient = new ClobClient(host, chainId, wallet, creds);

  const YES_TOKEN = "71321045679252212594626385532706912750332728571942532289631379312455583992563";

  try {
    // Execute a market buy for $100
    console.log("\n=== Market Buy ===");
    const buyResult = await clobClient.createAndPostMarketOrder(
      {
        tokenID: YES_TOKEN,
        amount: 100,
        side: Side.BUY,
        orderType: OrderType.FOK,
      },
      { tickSize: "0.01" },
      OrderType.FOK
    );
    console.log("Buy result:", buyResult);

    // Wait a moment
    await new Promise(resolve => setTimeout(resolve, 2000));

    // Execute a market sell for 110 shares
    console.log("\n=== Market Sell ===");
    const sellResult = await clobClient.createAndPostMarketOrder(
      {
        tokenID: YES_TOKEN,
        amount: 110,
        side: Side.SELL,
        orderType: OrderType.FOK,
      },
      { tickSize: "0.01" },
      OrderType.FOK
    );
    console.log("Sell result:", sellResult);

  } catch (error: any) {
    console.error("Error executing market order:", error.message);
    if (error.response) {
      console.error("Response data:", error.response.data);
    }
  }
}

main();

Error Handling

Market orders can fail for several reasons:
{
  "error": "Insufficient liquidity",
  "message": "Order book does not have enough depth to fill the order"
}
Solution: Reduce the order size or use FAK instead of FOK to allow partial fills.
{
  "error": "Insufficient balance",
  "message": "Not enough USDC balance or approved allowance"
}
Solution: Check your USDC balance and ensure you’ve approved the CLOB contract.
{
  "error": "Slippage tolerance exceeded",
  "message": "Execution price differs too much from expected price"
}
Solution: Review the order book and consider placing a limit order instead.

Best Practices

Check Order Book

Review available liquidity before placing large market orders to estimate slippage.

Use FOK Wisely

Use FOK for critical trades where partial fills are unacceptable.

Monitor Spreads

Wide bid-ask spreads indicate low liquidity and potential high slippage.

Consider Limits

For large orders, consider limit orders or RFQ to get better prices.

When to Use Market Orders

Use market orders when:
  • Speed is more important than price
  • The spread is tight and slippage is minimal
  • The market is moving quickly and you need immediate execution
  • Order size is small relative to market depth
Use limit orders when:
  • Price control is important
  • The order book is thin
  • You’re willing to wait for a better price
  • Order size is large relative to available liquidity

Next Steps

Basic Orders

Learn about limit orders and time-in-force options

RFQ Trading

Get better prices for large orders using RFQ

Order Book Data

Learn to fetch and analyze order book depth

Trading Guide

Comprehensive guide to order management

Build docs developers (and LLMs) love