Quant from Scratch by QuantWright

Introduction to Quant Finance · Lesson 13

Orders: market, limit and stop

How the three basic orders turn into trades, and why the price you planned on isn't always the price you get.

Know the basics? Read the lesson notes

Who is waiting to trade

To trade, you send an order: an instruction to buy or sell, sent to the market where buyers and sellers meet. Orders that can't trade straight away wait in a list called the order book.

Here is the book for a made-up share (a small piece of a company) at the start of a day. Above the middle are the asks: people waiting to sell, each at the lowest price they'll accept. Below are the bids: people waiting to buy, each at the highest price they'll pay. The bars show how many shares wait at each price.

The gap between the lowest ask and the highest bid is the spread. The last trade is the price of the most recent deal; it's what a chart shows as "the price".

Tap the best ask, the lowest price anyone is selling at. Then tap the best bid, the highest price anyone is buying at.

The order book of a made-up share (simulated)

Middle column: price in dollars. Bars and numbers: shares waiting at that price.

The best ask is 100.10 and the best bid is 99.90, so the spread is 100.10 − 99.90 = 0.20. The last trade, 100.00, sits between them: right now nobody is offering to trade at it.

Buy now

The simplest order is a market order: buy (or sell) now, at whatever price is on offer. You send a market order to buy 10 shares. The last trade was 100.00. What price do you pay?

100.00 was the last deal, but nobody is selling at 100.00 now. To buy at once you take the cheapest seller waiting: 100.10, the best ask.
99.90 is what buyers are offering. You are the buyer here, so you have to meet a seller, and the cheapest one asks 100.10.
To buy at once, you take the cheapest seller waiting: 100.10, the best ask. There are 33 shares there, enough for your 10.

The market order fills

When an order turns into a trade, we say it fills. Send the market buy and watch it fill. Then sell the same 10 shares straight back with a market sell.

The same book: your two market orders (simulated)

The row your order trades with lights up, and its shares go down by your 10.

  • No trades yet

You bought at 100.10 and sold at 99.90. Nothing moved, yet you got back 0.20 a share less than you paid: the spread. On 10 shares, 0.20 × 10 = $2.00.

A market order buys at the best ask and sells at the best bid. It fills at once as long as someone is waiting on the other side. What it doesn't set is the price: you get whatever the other side is offering. Each of your trades also became the new last trade.

Name your price: the limit order

A limit order names a price, called the limit. A limit buy at 99.50 says: buy, but pay no more than 99.50. Nobody is selling that low right now, so the order waits in the book with the other bids. It fills only if the best ask comes down to 99.50 before the day ends.

Below are 20 made-up days that all start from this same book. Each runs from the open to the close (the day's first and last prices), and on each the price wanders at random, nothing planned. Play day 1, then try others. You can move your limit too.

One made-up day, from the open to the close (simulated)

Black line: the best ask, the price your limit buy waits for. Grey line: the best bid. Dashed: your limit. A dot marks a fill.

Day1 of 20
Lowest ask that day–
Your orderWaiting
Days it fills, at this limit–

On day 1 the price never fell below the open: the lowest ask all day was the opening 100.10, so a limit buy at 99.50 waited all day and never filled. At 99.50 it filled on 12 of the 20 days, each time at 99.50.

Set the limit at 100.10 or higher and it fills at once, at 100.10: a limit is the most you'll pay, and a seller is already asking less. A limit order sets the price. Whether you trade at all is up to the market.

Sell if it falls: the stop order

Now say you hold 10 shares, bought at 100.10. A stop order waits for a price you choose, the stop price, and then becomes a market order. A sell stop at 99.40 says: if the best bid falls to 99.40 or below, sell at once at whatever the best bid is. People use it to try to limit a loss, so it's often called a stop-loss.

Play the same 20 days with a sell stop at 99.40 and watch what it sells at.

The same 20 days, with a sell stop (simulated)

Black line: the best bid, the price that triggers your sell stop. Grey line: the best ask. Dashed: your stop. A dot marks where it sold.

Day1 of 20
Lowest bid that day–
Your stopWaiting
Days it sells, at this stop–

At 99.40 the stop sold on 14 of the 20 days. On 12 it sold at exactly 99.40. On 2 it sold at 99.30: the bid jumped from 99.50 straight to 99.30 (prices move in steps of 0.10), skipping 99.40 but still at or below the stop.

A stop decides when you sell. The price is whatever the best bid is at that moment.

Overnight

Go back to day 1 and picture you still hold the 10 shares at its end. The best bid at the close is 100.10, and you place a sell stop at 99.00.

The market shuts overnight. Bad news comes out while it's shut, and when it opens the next morning, the highest price any buyer offers is 95.00. The price has jumped straight past your stop, with no trades in between. A jump like that is called a gap.

Your stop is at 99.00 and the market opens at 95.00. What happens to it?

Keep your guess in mind. On the next screen the market opens.

The gap

The end of day 1, the night, and the start of the next morning. Open the market and watch your stop.

Day 1's close, the night, and the next morning (simulated)

Black line: the best bid, the price that triggers your sell stop. Grey line: the best ask. Dashed: your sell stop at 99.00. A dot marks your sale; the bid kept falling after it.

Your stop99.00
First bid next morning–
Sold at–
Less than planned–

The difference between the price you planned on and the price you got has a name: slippage. Here it's 95.00 − 99.00 = −4.00 a share, or $40 less on your 10 shares. A stop gives no promise about the price. After a gap it sells wherever the market opens.

Theory

The three fill rules

A market order, a limit order and a stop order are each a rule for when the order fills (turns into a trade) and at what price. Below, "the other side's best price" means the best ask when you buy and the best bid when you sell. As rules, checked at every moment until the order fills:

market order:
  fill now, at the other side's best price

limit order at L:
  if the other side's best price is L or better:
      fill, at L or better
  else: wait (it may never fill)

stop order at S:
  if the price has reached S:
      become a market order
  else: wait
the other side's best price
for a buy, the best ask; for a sell, the best bid
L
the limit price. "L or better" means L or lower for a buy, L or higher for a sell
S
the stop price. "Reached S" here means the best bid falls to S or below for a sell stop, or the best ask rises to S or above for a buy stop
Worked example: your orders

Market buy: filled now at the best ask, 100.10. Limit buy at 99.50 on day 1: the best ask never got to 99.50 or lower, so it waited all day: no fill. A sell limit at 100.50 (sell, but only at 100.50 or better) sent at the open: the best bid, 99.90, is below 100.50, so it waits, and fills only if the bid rises to 100.50 or higher. Sell stop at 99.00 overnight: the bid opened at 95.00, already below 99.00, so the stop was reached and it became a market sell and filled at the best bid, 95.00.

Holds when
  • your order is small enough to fill at one price (yours were 10 shares, and at least 20 waited at every price);
  • a waiting limit buy fills only when the best ask reaches L. In a real market it can also fill when a seller sells straight to your bid;
  • stops trigger on the best bid or ask. Many brokers trigger them on the last trade instead.
Breaks when

A big order uses up the shares at the best price and fills at worse ones (lesson D14, The order book).

Try the rules on new numbers. The best bid is 49.80 and the best ask is 50.00. You send a limit buy at 50.20. What happens?

The best ask, 50.00, is already 50.20 or better (lower, for a buy), so it fills now, at the price on offer: 50.00.
50.20 is the most you'll pay, not what you pay. A seller is already asking 50.00, which is better, so it fills now at 50.00.
A limit buy waits only while the best ask is above L. Here the ask, 50.00, is already below 50.20, so it fills now, at 50.00.
Theory

Slippage: planned price against filled price

The difference between the price you got and the price you planned on is called slippage. Write down the planned price before you send the order, then measure:

slippage = fill − intended
total slippage = slippage × shares
fill
the price your order actually traded at
intended
the price you planned on: for a stop, the stop price; for a limit, the limit; for a market order, the best price on offer (the ask for a buy, the bid for a sell) when you decided
shares
how many shares the order traded

For a buy, slippage above 0 means you paid more than planned; for a sell, below 0 means you got less. For a buy, a negative number is better than planned; for a sell, a positive one is. A limit can fill better than its limit, which is slippage in your favour: a buy limit at 25.00 that fills at 24.80 has 24.80 − 25.00 = −0.20.

For a market order we measure from the ask or bid, not from the chart's last trade, because that last trade was not a price you could buy at. The spread covers that gap: it is a cost too, but it is not slippage. Your market buy at 100.10 had no slippage, and still paid the spread.

Worked example: your gap

A sell stop at 99.00 filled at 95.00: 95.00 − 99.00 = −4.00 a share, so you got 4.00 a share less than planned. Total: −4.00 × 10 shares = −$40.

Holds when
  • the order fills at one price;
  • the intended price is fixed before the order is sent, not picked afterwards.
Breaks when

A limit that never fills has no fill, so no slippage, yet the missed trade can cost more than slippage would have.

Try it on new numbers. A buy stop at 50.00 buys once the best ask rises to 50.00 or above; traders use one to buy only after the price has risen past a level. Overnight news, and the next morning the best ask opens at 52.40. What is the slippage, per share?

The ask opened at 52.40, already above 50.00, so the stop became a market buy and filled at the best ask: slippage = 52.40 − 50.00 = +2.40 a share, 2.40 more than planned.
The ask opened at 52.40, already above 50.00, so the stop became a market buy and filled at the best ask, 52.40. Slippage = fill − intended = 52.40 − 50.00 = +2.40 a share.

What this means for a backtest

A backtest runs a trading rule over past prices to see what it would have made. It has to decide, for every order, whether it filled and at what price. Three things follow. Turn them over one at a time.

  1. Every backtest uses a fill rule, stated or not.

    Filling at the last trade, the price on the chart, leaves out the spread, a cost separate from slippage: a real market order buys at the ask and sells at the bid. Look for the fill rule in the backtest's settings or notes.

Practice 1 of 4

Your own stop, gapped

The situationYou hold 200 shares of a made-up share, with a sell stop at 40.00. Overnight there's bad news, and the next morning the best bid opens at 37.50.

How many dollars less than planned do the 200 shares bring in?

Hint: the stop becomes a market sell at the open. Slippage per share = fill − intended. Then multiply by the shares.

The stop sells at the first bid, 37.50. Slippage = 37.50 − 40.00 = −2.50 a share, and −2.50 × 200 = −$500: $500 less than planned.
The bid opened at 37.50, below the stop, so the stop became a market sell at 37.50. Slippage = 37.50 − 40.00 = −2.50 a share; × 200 shares = −$500, which is $500 less than planned.
Practice 2 of 4

A course's backtest

The situationA trading course shows a backtest. At each signal (the moment the rule decides to place an order) its rule places a limit buy 0.50 below the last trade. The backtest counts a trade at every signal, filled at the limit price.

What is wrong with how it counts?

A limit fills only if the price reaches it. On your day 1 a limit 0.60 below the open never filled; at 99.50 it missed on 8 of the 20 days.
A limit fills only if the price comes down to it. On the days it didn't, there was no trade, but this backtest counts one anyway, at a good price.
They can: check at each moment whether the best ask reached the limit, as on your 20 days. This backtest skips that check, so signals whose limit never filled still count as trades.
Practice 3 of 4

Stops in your own backtest

The situationYour backtest fills every sell stop at exactly its stop price, including on mornings the market opened below the stop.

What does that do to its results on those mornings?

A stop becomes a market order when the price reaches it. After a gap down, the first bid is below the stop, so the real sale is lower than the stop price.
On those mornings the open is below the stop by definition, so a real sale at the first bid is worse than the stop price, not better.
The stop triggers at the open and sells at the first bid, below the stop. Filling at the stop price hides that slippage, like your 4.00 a share on day 1's gap.
Practice 4 of 4

Selling with a limit

The situationYou hold 300 shares of a made-up share and want at least 62.00 each. You send a sell limit at 62.00. Right now the best bid is 62.30 and the best ask is 62.50.

What is the slippage, per share? (Use a minus sign if it's below 0.)

Hint: for a sell, the other side's best price is the best bid. Is it already 62.00 or better? Then slippage = fill − intended.

The best bid, 62.30, is already 62.00 or better (higher, for a sell), so the limit fills now at 62.30. Slippage = 62.30 − 62.00 = +0.30 a share: for a sell, above 0 is better than planned.
For a sell, the other side's best price is the best bid, 62.30. It's already 62.00 or better, so the limit fills now at 62.30, not at 62.00 and not at the ask. Slippage = 62.30 − 62.00 = +0.30 a share, in your favour.
✓

Lesson done

An order is a rule for when you trade and at what price. A market order trades now. A limit order sets the price but may never trade. A stop order waits, then trades at whatever price is on offer, which after a gap can be far from the stop. The difference between the price you planned on and the price you got is slippage.

Key ideas

Order book
the orders waiting to trade: bids (to buy) and asks (to sell), with prices and sizes
Best bid / best ask
the highest price a buyer is offering / the lowest price a seller is asking
Spread
best ask − best bid; a cost of trading, separate from slippage
Fill
an order turning into a trade, and the price it traded at
Market order
fills now, at the other side's best price
Limit order
fills only at its limit price or better; may never fill
Stop order (stop-loss)
waits until the price reaches the stop, then becomes a market order
Gap
a jump in price with no trades in between, often from one day's close to the next open
Slippage
fill − intended price

Formulas

slippage = fill − intended  ·  total = slippage × shares  ·  spread = best ask − best bid

Market: fill now at the other side's best price. Limit at L: if the other side's best price is L or better, fill at L or better, else wait. Stop at S: if the price has reached S, become a market order, else wait.

Lesson notes

An order is a rule for when you trade and at what price. A market order trades now, at the best price someone on the other side is offering. A limit order sets the price but may never trade. A stop order waits, then trades at whatever price is on offer, which after a gap can be far from the stop. The difference between the price you planned on and the price you got is slippage.

The order book

An order is an instruction to buy or sell. Orders that can't trade at once wait in the order book: the asks are people waiting to sell, each at the lowest price they'll accept, and the bids are people waiting to buy, each at the highest price they'll pay. In the lesson's made-up share, at the open, the best (lowest) ask is 100.10 and the best (highest) bid is 99.90, so the spread is 100.10 − 99.90 = 0.20. The last trade was 100.00, between them.

The market order

A market order buys or sells now. A buy takes the best ask, 100.10, and a sell takes the best bid, 99.90. When an order turns into a trade, it fills. Buy and sell 10 shares straight back and you lose 0.20 a share: 0.20 × 10 = $2.00.

The limit order

A limit order names a price, the limit. A limit buy at 99.50 means: pay no more than 99.50. Nobody sells that low, so it waits for the best ask to come down to 99.50. These counts come from the lesson's simulation of 20 made-up days, which the notes don't list. It filled on 12, each time at 99.50. On day 1 the lowest ask was the opening 100.10, so it never filled. A limit at 100.10 or higher fills at once.

The stop order

A stop order waits for a price you choose, the stop price, then becomes a market order. A sell stop at 99.40 says: if the best bid falls to 99.40 or below, sell at once. It sold on 14. On 12 it sold at exactly 99.40. On 2 it sold at 99.30: prices move in steps of 0.10, and the bid jumped from 99.50 straight to 99.30.

The gap

Say you hold 10 shares with a sell stop at 99.00. Day 1 closes with the best bid at 100.10, above the stop, so nothing happens. Bad news comes out overnight, and the next morning the highest bid is 95.00. A jump past the stop with no trades in between is a gap. The stop becomes a market sell and fills at 95.00.

Slippage

Slippage is the fill minus the price you planned on, written down before you send the order. Here: 95.00 − 99.00 = −4.00 a share, so −$40 on 10 shares. For a buy, above 0 means you paid more than planned, so negative is better. For a sell, below 0 means you got less, so positive is better. For a market order, plan on the best price on offer.

What this means for a backtest

A backtest runs a trading rule over past prices to see what it would have made, so it must decide for every order whether it filled and at what price. Filling at the last trade leaves out the spread. A limit that never filled is a trade that never happened. A stop fills at the best bid (for a sell) or best ask (for a buy) after it triggers, which after a gap is the opening price, not the stop.

Formula: the three fill rules

market order:
  fill now, at the other side's best price

limit order at L:
  if the other side's best price is L or better:
      fill, at L or better
  else: wait (it may never fill)

stop order at S:
  if the price has reached S:
      become a market order
  else: wait
the other side's best price
for a buy, the best ask; for a sell, the best bid
L
the limit price. "L or better" means L or lower for a buy, L or higher for a sell
S
the stop price. "Reached S" here means the best bid falls to S or below for a sell stop, or the best ask rises to S or above for a buy stop
Worked example: the lesson's orders

Market buy: filled now at the best ask, 100.10. Limit buy at 99.50 on day 1: the best ask never got to 99.50 or lower, so it waited all day: no fill. A sell limit at 100.50 (sell, but only at 100.50 or better) sent at the open: the best bid, 99.90, is below 100.50, so it waits, and fills only if the bid rises to 100.50 or higher. Sell stop at 99.00 overnight: the bid opened at 95.00, already below 99.00, so the stop was reached and it became a market sell and filled at the best bid, 95.00.

Holds whenBreaks when

A big order uses up the shares at the best price and fills at worse ones (lesson D14, The order book).

Formula: slippage

slippage = fill − intended
total slippage = slippage × shares
fill
the price your order actually traded at
intended
the price you planned on: for a stop, the stop price; for a limit, the limit; for a market order, the best price on offer (the ask for a buy, the bid for a sell) when you decided
shares
how many shares the order traded
Worked example: the lesson's gap

A sell stop at 99.00 filled at 95.00: 95.00 − 99.00 = −4.00 a share, so you got 4.00 a share less than planned. Total: −4.00 × 10 shares = −$40.

Holds whenBreaks when

A limit that never fills has no fill, so no slippage, yet the missed trade can cost more than slippage would have.

One more worked example

Made-up round numbers

Another made-up share: best bid 30.40, best ask 30.60.

A market buy of 50 shares fills now at the best ask, 30.60, and costs 50 × 30.60 = $1,530.

You hold 200 shares with a sell stop at 29.80, and the best bid opens at 29.10 after overnight news. That is below 29.80, so the stop becomes a market sell and fills at 29.10.

Slippage: 29.10 − 29.80 = −0.70 a share.

Total: −0.70 × 200 = −$140.

Common mistakes

Check yourself

  1. The best bid is 20.40 and the best ask is 20.60. You send a limit buy at 20.80. What happens, and at what price?
    Answer

    For a buy, the other side's best price is the ask, 20.60. It is already 20.80 or lower, so the order fills now, at 20.60.

  2. You hold 150 shares with a sell stop at 80.00. The next morning the best bid opens at 78.20. What is the slippage, per share and in total?
    Answer

    78.20 − 80.00 = −1.80 a share, and −1.80 × 150 = −$270.

  3. A backtest fills every limit buy at its limit price at every signal (the moment the rule decides to place an order). What is wrong?
    Answer

    A limit buy fills at its limit or lower, and only when the best ask reaches it. Counting every signal as filled counts trades that never took place.

Try it yourself

Run the simulation. Every price and number in this lesson comes from one Python script with a fixed random seed (below). Change the spread, the size of the steps or the gap and see what moves.

Play it by hand. Write the opening book on paper: best bid 99.90, best ask 100.10. Roll a die for each moment: 1 or 2, both prices go down 0.10; 3 or 4, they stay; 5 or 6, they go up 0.10. Place a limit buy and a sell stop on the paper, apply the three rules at every roll, and note when each one fills and at what price.

About QuantWright

QuantWright, which publishes this course, sells backtesting software. It backtests with costs: commission, slippage and next-bar fills. Every result states its data window, data source and costs. quantwright.com

The code behind every number (Python, seed 2026110902)
Loading…

Going further: a big market order walks the book

Your market buy was 10 shares, and 33 were waiting at the best ask, so it filled at one price. A bigger order takes everything at the best ask, then moves up to the next price, and the next. Pick a size.

A market buy against the opening book (simulated)

Shares to buy
Average price paid–
Worst price paid–

100 shares take all 33 at 100.10, all 59 at 100.20 and 8 at 100.30, for an average of about 100.18. Against the 100.10 on offer when you decided, that's slippage of about 0.08 a share, from size alone. The theory card's "small enough to fill at one price" is the line this crosses.

Going further: the stop-limit order

A stop-limit order is a stop that becomes a limit order instead of a market order when it triggers. Say a sell stop at 99.00 with a limit of 98.50. After day 1's gap, the bid opened at 95.00, below the 98.50 limit, so it would not sell at the open. On that simulated morning the bid never rose above 95.00, so it would not have sold at all. It sets a worst price, and in exchange it may not sell.

For the precisePrices are whole cents; a tick is 0.10 and the spread is always 0.20. Each later moment, the bid and ask move together by −2, −1, 0, +1 or +2 ticks with chances 5%, 20%, 50%, 20%, 5%: a random walk with no drift, 200 moments a day. Fill rules exactly as on the theory card, with the stop triggered by the best bid (sell) or best ask (buy) and a resting limit filled at its limit the first moment the best ask reaches it. Days a limit buy fills, out of 20, from 99.00 to 100.40: 5, 6, 7, 7, 10, 12, 14, 17, 18, 18, 18, then 20 from 100.10 up. Days a sell stop sells, from 99.00 to 99.80: 7, 7, 10, 12, 14, 17, 18, 18, 18. The gap was put in by hand: the stop is the whole dollar at least 1.00 below day 1's closing bid, and the next morning opens 4.00 below the stop. Seed 2026110902.

Honest notes

Where this comes from