Coding
Quant Python

Cost of Crossing the Spread

Difficulty

Read the problem, hints and solution here. The editor needs a bigger screen: open this page on a laptop to write and run your code.

Implement crossing_cost(fills), which measures the cost of each fill against where the market was when it happened. Crossing the spread to get filled costs half the spread. Getting filled passively earns half the spread. Keep the sign right.

Each fill is a tuple (symbol, side, qty, price, mid2):

  • side is 'B' or 'S';
  • qty and price are positive integers, price in ticks;
  • mid2 is twice the mid price, as an integer.

The mid sits half way between bid and ask, so on a one-tick spread it lands on a half tick. Passing it doubled keeps it exact and avoids floats entirely, so work in half-ticks throughout.

For each fill the cost in half-ticks is

  • 2 * price - mid2 for a buy, since paying above the mid costs money;
  • mid2 - 2 * price for a sell, since selling below the mid costs money.

A fill on the favourable side of the mid produces a negative cost. That is not an error: it is price improvement, and a market maker's fills should mostly look like this.

Return a list of (symbol, total_cost_in_half_ticks) for every symbol that appears, sorted by symbol ascending.

Examples

crossing_cost([("ES", "B", 10, 5000, 9998), ("ES", "S", 10, 5000, 10002)])
# [('ES', 40)]

crossing_cost([("A", "B", 2, 10, 21), ("B", "S", 3, 10, 19)])
# [('A', -2), ('B', -3)]

In the first, both fills cross by one half-tick on ten lots, so each costs 20 half-ticks. In the second, both fills are on the good side of a half-tick mid, so both show as price improvement.

Constraints

  • 0 <= len(fills) <= 2 * 10^5.
  • Everything is an integer and the answer is exact. Do not divide by two anywhere: report the total in half-ticks as specified.
  • A fill exactly at the mid costs zero.
Terms used in this problem (10)
bid:
A resting order to buy, or its price. The best bid is the highest price anyone will pay right now.
ask:
A resting order to sell, or its price, also called the offer. The best ask is the lowest price anyone will sell at right now.
spread:
The gap between the best bid and the best ask, usually counted in ticks. A market maker earns it by buying at the bid and selling at the ask.
mid:
The average of the best bid and the best ask. It is the usual fair price when nothing is trading.
tick:
The smallest step a price can move. Prices in these problems are integers counted in ticks, so there is no rounding.
lot:
The unit of quantity. Orders and fills are counted in whole lots.
fill:
A trade that executes part or all of an order, at one price and one quantity. A partial fill leaves the rest of the order resting.
cross:
A bid and an ask cross when the bid price is at or above the ask price, so the two can trade. A crossed book is one where that has happened and not yet been resolved.
maker and taker:
A maker adds a resting order to the book. A taker trades against a resting order and removes it from the book.
symbol:
The short code that names an instrument, such as a share or a future.
Rate this problem
Language: Python 3.12crossing_cost
Rate this problem