Questions / Quant development, Trading system components
Self-trade prevention
Trader A sends a buy for 150 at 11. Resting orders from A are cancelled. How much trades?
def match(book, side, px, qty, trader):
trades = 0
for o in book: # resting sells, best price first
if qty == 0 or o['px'] > px:
break
if o['trader'] == trader: # self-trade prevention: cancel resting
o['qty'] = 0
continue
fill = min(qty, o['qty'])
o['qty'] -= fill
qty -= fill
trades += fill
return trades
book = [{'px': 10, 'qty': 50, 'trader': 'B'},
{'px': 10, 'qty': 40, 'trader': 'A'},
{'px': 11, 'qty': 60, 'trader': 'C'},
{'px': 12, 'qty': 80, 'trader': 'B'}]
print(match(book, 'buy', 11, 150, 'A'))
Answer it in the app It's free. You get hints, a second try if you miss, and a follow-up question.
Show the answer and worked solution
Answer: 110 count
- 50 from B at 10.
- A’s own 40 at 10 is cancelled.
- 60 from C at 11.
- 12 is above the limit.
Interview shortcut. Skip your own order: 50 from B + 60 from C.
Takeaway. Cancel-resting STP removes your own order without a print.
In the app this has a follow-up where one thing about the setup changes. Try the follow-up.