Skip to content

Instantly share code, notes, and snippets.

@prystupa
prystupa / OrderBook.scala
Last active February 25, 2018 15:29
OrderBook implementation with support for Market Orders price and time priority
class OrderBook(side: Side, orderTypes: (Order => OrderType)) {
private var marketBook: List[Order] = Nil
private var limitBook: List[(Double, List[Order])] = Nil
private val priceOrdering = if (side == Sell) Ordering[Double] else Ordering[Double].reverse
def add(order: Order) {
orderTypes(order).price match {
@prystupa
prystupa / OrderBook.Limit.feature
Last active November 26, 2017 03:50
OrderBook tests for decreasing top outstanding order
Scenario Outline: Decrease top outstanding order partially and then fill it completely
When the following orders are added to the "<Side>" book:
| Broker | Qty | Price |
| A | 100 | 10.5 |
| B | 100 | 10.5 |
Then the "<Side>" order book looks like:
| Broker | Qty | Price |
| A | 100 | 10.5 |
| B | 100 | 10.5 |
When the top order of the "<Side>" book is filled by "20"
@prystupa
prystupa / LimitOrdersMatching.feature
Created December 25, 2012 18:10
Partial matches for limit orders
Feature: Core matching logic for limit orders
Background: Submit initial non-crossing orders to work with
Given the following orders are submitted in this order:
| Broker | Side | Qty | Price |
| A | Buy | 100 | 10.4 |
| B | Buy | 200 | 10.3 |
| C | Sell | 100 | 10.7 |
| D | Sell | 200 | 10.8 |
Then no trades are generated
@prystupa
prystupa / OrderBook.Market.feature
Created December 25, 2012 21:43
Price and time priority for market orders
Feature: Core Market Order Functionality for Price and Time Priority
Scenario Outline: Price and time priority of market orders over limit orders
When the following orders are added to the "<Side>" book:
| Broker | Qty | Price |
| A | 100 | 10.5 |
Then the "<Side>" order book looks like:
| Broker | Qty | Price |
| A | 100 | 10.5 |
When the following orders are added to the "<Side>" book:
@prystupa
prystupa / MarketOrdersMatching.feature
Last active November 26, 2017 03:49
Matching incoming market order against outstanding limit orders
Feature: Core matching logic for market orders
Scenario: Matching a large Buy market order against multiple limit orders
The order is large enough to fill the entire opposite book
The remainder of the market order is expected to rest in its book
When the following orders are submitted in this order:
| Broker | Side | Qty | Price |
| A | Buy | 100 | 10.7 |
| B | Buy | 200 | 10.6 |
| C | Buy | 300 | 10.5 |
@prystupa
prystupa / OrderBook.BestLimit.feature
Last active November 26, 2017 03:49
Best limit logic for limit and market orders
Feature: Maintaining best limit in the order book
Scenario Outline: Various life cycles of the order book best limit
# When are no orders in the book, the best limit is not defined
Then the best limit for "<Side>" order book is "None"
# If a market order enters the book, the best limit is still undefined
When the following orders are added to the "<Side>" book:
| Broker | Qty | Price |
| A | 100 | MO |
Then the "<Side>" order book looks like:
@prystupa
prystupa / MarketOrdersMatching.feature
Created December 27, 2012 20:10
Matching incoming limit orders against outstanding market orders
Scenario: Matching incoming Buy limit order against a single outstanding Sell market order
When the following orders are submitted in this order:
| Broker | Side | Qty | Price |
| A | Sell | 100 | MO |
| B | Buy | 120 | 10.5 |
Then the following trades are generated:
| Buying broker | Selling broker | Qty | Price |
| B | A | 100 | 10.5 |
And market order book looks like:
| Broker | Qty | Price | Price | Qty | Broker |
class StronglyConnectedComponentsAlgo {
type GraphAdjList = Vector[Set[Int]]
type SubGraphAdjList = Map[Int, Set[Int]]
def compute(adjacencyList: GraphAdjList): Vector[SubGraphAdjList] = {
def strongConnect(v: Int, state: State): State = {
if (!state.visited(v).isDefined) {
Feature: Computing strongly connected components of a graph using Tarjan's algorithm
Scenario: Simple chain with no loops
Given the following edges of a graph with "3" vertices:
| Start | End |
| 1 | 2 |
| 2 | 3 |
When I compute strongly connected components of this graph using Tarjan's algorithm
Then there is a connected component of the graph:
| Node | Successors |
@prystupa
prystupa / MatchingEngine.MarketOrders.feature
Last active November 26, 2017 03:49
Market orders matching with other market orders when "best limit" is defined
Scenario: Matching incoming Buy market order against Sell market order when another - limit - Sell order present
Given the following orders are submitted in this order:
| Broker | Side | Qty | Price |
| A | Sell | 100 | MO |
| B | Sell | 100 | 10.5 |
Then market order book looks like:
| Broker | Qty | Price | Price | Qty | Broker |
| | | | MO | 100 | A |
| | | | 10.5 | 100 | B |
When the following orders are submitted in this order: