Asked at IMC Trading
Theory: Quant Python That Survives ReviewRead 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.
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.
0 <= len(fills) <= 2 * 10^5.