6 kyu

Manage an Orderbook

Description:

Goal

Print the Top Of Book of an orderbook

Introduction

The state of each stock on a stock market is kept in a data structure called an orderbook. The orderbook consists of two sides, the "Buy Side", which represents offers to buy a stock, and the "Sell Side", which represents offers to sell a stock. The Buy Side is listed starting with the highest price and is ordered in decreasing price. The Sell Side is listed starting with the lowest price and is ordered in increasing price.

Buy Side : Sell Side
--------   ----------
4@10       18@16       <-------- Top Of Book
3@6         2@20
2@4

The notation used is Qty@Price, so for example 4@10 on the Buy Side indicates that someone is offering to buy 4 shares at a price of 10. Similarly, 18@16 on the Sell Side indicates that someone is willing to sell 18 shares at a price of 16.

The 'Top Of Book' denotes the top level of the orderbook, that is the offer on the Buy Side with the highest price and the offer on the Sell Side with the lowest price. In this example, the Top of Book is 4@10 : 18@16.

Data Feed

All updates to the orderbook (new order, modifications, cancellations, and trades) can be represented by two messages: Add and Cancel.

Add Messages

Add messages have the following format:

message_type: 'a'
order_side: 'b' for buy or 's' for sell
order_id: integer unique per order
quantity: integer value
price: integer value

The order id is unique per trading day and therefore won't repeat in this exercise. There are no fractional quantities and all prices are integer values. When various orders have the same price, their quantities are aggregated and all of those orders appear at the same level in the orderbook.

Cancel Messages

Cancel messages have the following format:

message_type: 'c'
order_id: integer corresponding to an existing order

You can assume that all order ids in the cancel message correspond to an existing order.

Input Format

In Python, the messages are stored sequentially as tuples in a list. For example, the input

[('a', 'b', 1, 2, 3), ('c', 1)]

describes an add order on the Buy Side with an order id of 1, a quantity of 2, and a price of 3 followed by a cancellation for the order with id 1.

Output Format

Your goal in this exercise is to print the Top Of Book for the orderbook after you receive a data feed containing add and cancel messages. The output should be a string with the following format

"4@20 : 2@40"

where the Buy Side is on the left and the Sell Side is on the right. If there are no orders on a particular side, it should be represented as 0@0. For example, a top of book with no sell orders would look like "4@20 : 0@0".

Example

At the start, the orderbook is empty.

Buy Side : Sell Side
--------   ----------
0@0        0@0

An add order on the buy side is received: message_type='a' side='b' order_id=1 qty=1 price=5

Buy Side : Sell Side
--------   ----------
1@5        0@0

An add order on the sell side is received: message_type='a' side='s' order_id=2 qty=8 price=10

Buy Side : Sell Side
--------   ----------
1@5        8@10

Another add order on the buy side is received: message_type='a' side='b' order_id=3 qty=2 price=4

Buy Side : Sell Side
--------   ----------
1@5        8@10
2@4

Another add order on the sell side is received: message_type='a' side='s' order_id=4 qty=2 price=10

Buy Side : Sell Side
--------   ----------
1@5        10@10
2@4

An order on the buy side is cancelled: message_type='c' order_id=3

Buy Side : Sell Side
--------   ----------
1@5        10@10

An order on the sell side is cancelled: message_type='c' order_id=2

Buy Side : Sell Side
--------   ----------
1@5        2@10
Algorithms

Stats:

CreatedAug 20, 2020
PublishedAug 20, 2020
Warriors Trained335
Total Skips71
Total Code Submissions367
Total Times Completed91
Python Completions91
Total Stars8
% of votes with a positive feedback rating90% of 34
Total "Very Satisfied" Votes28
Total "Somewhat Satisfied" Votes5
Total "Not Satisfied" Votes1
Total Rank Assessments13
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • Hamburgler Avatar
  • Blind4Basics Avatar
Ad