Skip to content

Instantly share code, notes, and snippets.

@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:
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 |
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) {
algorithm tarjan is
input: graph G = (V, E)
output: set of strongly connected components (sets of vertices)
index := 0
S := empty
for each v in V do
if (v.index is undefined) then
strongconnect(v)
end if
@prystupa
prystupa / MatchingEngine.scala
Last active December 10, 2015 05:58
Implementation of matching for incoming limit orders crossing with market orders
class MatchingEngine(buy: OrderBook, sell: OrderBook, orderTypes: (Order => OrderType))
extends mutable.Publisher[OrderBookEvent] {
// Only the relevant modified code is shown
// ...
private def tryMatchWithTop(order: Order, top: Order): Option[Trade] = {
def trade(price: Double) = {
val (buy, sell) = if (order.side == Buy) (order, top) else (top, order)
Some(Trade(buy.broker, sell.broker, price, math.min(buy.qty, sell.qty)))
@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 |
@prystupa
prystupa / OrderBook.scala
Last active December 10, 2015 05:48
Implementation of "bestLimit" in OrderBook
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 bestLimit: Option[Double] = limitBook.headOption.map({
case (limit, _) => limit
})
@prystupa
prystupa / OrderBookSteps.scala
Last active December 10, 2015 04:48
Additional steps for best limit scenarios
class OrderBookSteps extends ShouldMatchers {
// only the new code is shown
// ...
@Then("^the best limit for \"([^\"]*)\" order book is \"([^\"]*)\"$")
def the_best_limit_for_order_book_is(sideString: String, expectedBestLimit: String) {
val (_, book) = getBook(sideString)
val actual = book.bestLimit
@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 / MatchingEngine.scala
Last active December 10, 2015 04:28
tryMatchWithTop for limit orders that happens to support matching ANY order type against outstanding limit orders
private def tryMatchWithTop(order: Order, top: Order): Option[Trade] = top match {
case topLimit: LimitOrder => {
if (orderTypes(order).crossesAt(topLimit.limit)) {
val (buy, sell) = if (order.side == Buy) (order, topLimit) else (topLimit, order)
Some(Trade(buy.broker, sell.broker, topLimit.limit, math.min(buy.qty, sell.qty)))
}
else None
}
}