Order Types
16 min
A market order says "execute immediately, at whatever the current best price is" — guaranteed to execute (assuming any liquidity exists), but the exact price isn't guaranteed. A limit order says "only execute at this price or better" — a limit BUY only fills at the limit price or lower; a limit SELL only fills at the limit price or higher. Guaranteed price, but NOT guaranteed to execute at all.
def would_limit_buy_execute(limit_price, market_price):
return market_price <= limit_price
print(would_limit_buy_execute(100, 95)) # True -- market is cheaper than your limit
print(would_limit_buy_execute(100, 105)) # False -- market is more expensive, order waits
Buy and sell limits point in opposite directions -- a buy limit is a ceiling ('don't pay more than this'), a sell limit is a floor ('don't accept less than this').
When a buy order and a sell order "cross" (the buyer's limit is at or above the seller's limit — they'd both be happy at some price in between), an exchange matches them and executes a trade:
def match_orders(buy_price, sell_price):
if buy_price >= sell_price:
return (buy_price + sell_price) / 2 # simplified matching price
return None # no overlap -- no trade happens
print(match_orders(101, 100)) # 100.5 -- buyer will pay up to 101, seller will take 100 or more
print(match_orders(99, 100)) # None -- buyer won't pay enough, no trade
Real exchanges use more nuanced matching rules (usually executing at the RESTING order's price, not a midpoint, and with strict time-priority among orders at the same price) — the midpoint here is a simplification to keep the core "do these orders overlap at all" logic clear.
Write `would_limit_buy_execute(limit_price, market_price)`: a limit BUY order executes only if the market price is at or below the limit. Return True/False.
Write `match_orders(buy_price, sell_price)`: if the buy limit is at or above the sell limit (they 'cross'), the orders match — return the execution price as their midpoint, rounded to 2 decimals. Otherwise return None.
What's the key difference between a market order and a limit order?
You understand the market-vs-limit order tradeoff (guaranteed execution vs. guaranteed price), and can determine when a buy and sell order would match.