Skip to content

Instantly share code, notes, and snippets.

@prystupa
Last active December 10, 2015 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prystupa/4369872 to your computer and use it in GitHub Desktop.
Save prystupa/4369872 to your computer and use it in GitHub Desktop.
OrderBook with helper methods to peek the top order and to decrease quantity of the top outstanding order
class OrderBook(side: Side, orderTypes: (Order => OrderType)) {
private var limitBook: List[(Double, List[Order])] = Nil
private val priceOrdering = if (side == Sell) Ordering[Double] else Ordering[Double].reverse
def top: Option[Order] = limitBook.headOption.map({
case (_, orders) => orders.head
})
def decreaseTopBy(qty: Double) {
limitBook match {
case ((level, orders) :: tail) => {
val (top :: rest) = orders
limitBook = (qty == top.qty, rest.isEmpty) match {
case (true, true) => tail
case (true, false) => (level, rest) :: tail
case _ => (level, orderTypes(top).decreasedBy(qty) :: rest) :: tail
}
}
case Nil => throw new IllegalStateException("No top order in the empty book")
}
}
// the rest of the implementation is omitted
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment