Last active
December 10, 2015 02:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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