Tick-Indexed Price Ladder
Prerequisites: STL Container Choice, map, list, and deque Trade-Offs, Cache Lines, Locality, and Pointer Chasing
A market data handler keeps, for each symbol such as AAPL, a summary of the orders resting at the exchange. Build this two-sided summary, a price ladder, for one symbol.
- Bids and asks: orders to buy and orders to sell.
- Price level: all the orders on one side at one price. Only its total quantity is stored, not the individual orders.
- Best bid: the highest price with a non-zero bid quantity.
- Best ask: the lowest price with a non-zero ask quantity.
- Spread: the gap between the best bid and the best ask.
- Tick size: the fixed step prices move in, so the valid prices form a grid.
What to implement
process_commands(base, tick_size, levels, commands)returns the printed lines in command order.- Each command is one line, and
sideisBIDorASK. - On stdin, the first three lines are
base,tick_size, andlevels. The fourth is the number of commands. - There are three parts, each adding behavior to the previous one. One solution must pass all eleven tests.
| Command | Meaning | Prints |
|---|---|---|
SET side price qty | Set the total quantity at price to qty; 0 empties the level | nothing (Part 2: OUT_OF_BAND) |
ADD side price delta | Add the signed delta to the quantity at price | nothing (Part 2: OUT_OF_BAND) |
BEST | Best level on each side | BID level ASK level |
DEPTH side k | Best k non-empty levels on side | side level level ... |
RECENTER new_base | Move the band (Part 3) | DROPPED b a |
A level prints as price:quantity, for example 19010:300. A side with no non-empty level prints NONE in its place.
The band
STL Container Choice noted that when keys are dense small integers, an array indexed by the key beats a map. Subtracting a base price and dividing by the tick size turns prices near the market into such keys.
- The ladder covers a band of
levelsconsecutive grid prices starting atbase. Leveliholds the pricebase + i * tick_size, so the level of a price is(price - base) / tick_size. - Each side is an array of
levelsquantities with a cached best index, so reading the best bid and best ask needs no search. - A
std::mapfrom price to quantity per side also produces the right output, and the tests check only the output. The Approach compares the two layouts.
Part 1: Levels and the cached best
SETreplaces the quantity at a level:SET BID 19010 300thenSET BID 19010 500leaves 500, not 800.ADDchanges the quantity bydelta. A level whose quantity reaches 0 is empty.- A
SETorADDthat leaves an empty level at 0 changes nothing. - The two sides are stored separately. A command on one side never changes the other.
- Each side's best index stays cached and is updated as levels change:
- a level that becomes non-empty closer to the spread than the cached best becomes the new best;
- a change to any other level leaves the best where it is;
- only when the best level itself becomes empty does the ladder scan outward, away from the spread, to the next non-empty level, or to the end of the array if there is none.
Output
BID level ASK level side level level ...
BEST: for exampleBID 19010:300 ASK 19011:200, orBID NONE ASK 19011:200when every bid level is empty.DEPTH side k:side, then the side's firstknon-empty levels from the best. Bids go from the highest price down, asks from the lowest price up.- Empty levels are skipped and do not count toward
k. A side with fewer thanknon-empty levels prints all of them; a side with none printsside NONE.
Limits and guarantees
levelsandtick_sizeare between 1 and 10,000. There are at most 10,000 commands.base, everynew_base, and every price are between 1 and 1012. That does not fit in a 32-bitint, so store prices instd::int64_t.- Every price and every
new_baselies on the tick grid: it differs from the initialbaseby a whole multiple oftick_size. qtyis between 0 and 109.deltais between -109 and 109 and may be 0.- An
ADDnever takes a level's quantity below 0. ManyADDs can still push a level past 231, so store quantities instd::int64_tas well. kis between 1 andlevels.- The book is never crossed: whenever both sides have a non-empty level, the best bid price is below the best ask price.
Tests 1–4 cover this part. Every price in them lies inside the band.
Part 2: Prices outside the band
Prices can now fall below base or above base + (levels - 1) * tick_size. Both of those end prices are inside the band.
- A
SETorADDwhose price is outside the band printsOUT_OF_BANDand changes nothing, whatever its quantity or delta. That includes aSETwith quantity 0. BESTandDEPTHwork as before.
Tests 5–7 cover this part. Test 6 has a band of a single level.
Part 3: Recentering the band
When the market drifts toward the edge of the band, the handler moves the band with it.
RECENTER new_basemakesnew_basethe price of level 0. The band keepslevelslevels and the sametick_size, so it coversnew_basethroughnew_base + (levels - 1) * tick_size.new_basemay be above, below, or equal to the current base, and may move the band bylevelslevels or more.- A non-empty level whose price is inside the new band keeps its quantity at the same price. Only its index changes, to
(price - new_base) / tick_size. - A level whose price is outside the new band is dropped, on both sides. Its quantity does not come back if a later
RECENTERcovers that price again. - Prices that the new band covers and the old band did not start empty.
- After the move, each side's best is its best remaining non-empty level, or
NONE. - Every later command uses the new band, including the Part 2 band check.
Output: DROPPED b a, where b and a are the numbers of non-empty bid and ask levels discarded. Empty levels that fall outside the band are not counted.
Tests 8–11 cover this part.
19000 5 8 9 SET BID 19010 300 SET BID 19000 100 SET ASK 19020 200 SET ASK 19030 400 BEST DEPTH ASK 3 ADD BID 19010 -300 BEST DEPTH BID 2
BID 19010:300 ASK 19020:200 ASK 19020:200 19030:400 BID 19000:100 ASK 19020:200 BID 19000:100
Level i holds the price 19000 + 5i, so the band runs from 19000 to 19035.
The bid at 19010 sits at level (19010 - 19000) / 5 = 2 and becomes the best bid. The bid at 19000 lands at level 0, further from the spread, so the best stays at level 2.
DEPTH ASK 3 finds only two non-empty ask levels and prints both.
ADD BID 19010 -300 empties the best bid level, so the ladder scans downward. Level 1 (19005) is empty and level 0 (19000) holds 100, which becomes the new best bid.