Send your feedback or ask for help here!
Consider test case “Multiple trades”, given the following rule:
-A trade cannot be concluded between orders that aren’t on top of the book: the best price is always traded first.
-For a buy order the best price is the highest; for a sell order the best price is the lowest.
And the input:
11
ABC BUY 10 23.50
BBC BUY 10 13.50
ABC SELL 8 23.50
ABC SELL 5 23.50
BBC BUY 8 13.50
BBC BUY 12 13.50
ABC BUY 10 23.50
ABC SELL 10 23.45
ABC BUY 3 23.45
BBC BUY 10 13.50
BBC SELL 100 13.45
for ABC, we have
The best ABC BUY is 10 23.50 (highest buy)
The best ABC SELL is 10 23.45 (lowest sell)
hence the buyer will pay at least the amount the seller asks, and we have a trade.
But the testcase says:
Found: ABC 10 23.50…
Expected: ABC 8 23.50
Btw, if the buyers price is higher than the sellers price, and there is no previous price in the system (no prior sale) it is unclear what the price will be.
There could be a new rule ala: If the buyers price are higher than the seller price, and there are no prior trades, the price will be the asking (sellers) price.
There is already such a rule. You process the orders one by one as they arrive. Orders arrive one at a time, and when a trade takes place, it is always between the current order and a previous order. The “previous price” is the price of the previous order.
For instance, consider these two orders:
ABC SELL 10 25.00
ABC BUY 10 30.00
The first order, SELL, is added to the books. Then the second order, BUY, is not added to the books: instead, it matches the “previous” SELL order to create a trade “ABC 10 25.00”, where 25.00 is “the previous price”.
If instead we had:
ABC BUY 10 30.00
ABC SELL 10 25.00
then the trade would be “ABC 10 30.00”, because the previous price is 30.00, not 25.00.