Last active
December 10, 2015 02:58
-
-
Save prystupa/4371498 to your computer and use it in GitHub Desktop.
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 MatchingEngineSteps extends ShouldMatchers { | |
val orderTypes = OrderType.all() | |
val buyBook: OrderBook = new OrderBook(Buy, orderTypes) | |
val sellBook: OrderBook = new OrderBook(Sell, orderTypes) | |
val matchingEngine = new MatchingEngine(buyBook, sellBook, orderTypes) | |
var actualTrades = List[Trade]() | |
events { | |
case trade: Trade => actualTrades = trade :: actualTrades | |
} | |
@When("^the following orders are submitted in this order:$") | |
def the_following_orders_are_submitted_in_this_order(orders: java.util.List[OrderRow]) { | |
orders.toList.foreach(o => matchingEngine.acceptOrder( | |
LimitOrder(o.broker, parseSide(o.side), o.qty, o.price.toDouble))) | |
} | |
@Then("^market order book looks like:$") | |
def market_order_book_looks_like(book: DataTable) { | |
val (buyOrders, sellOrders) = parseExpectedBooks(book) | |
buyBook.orders().map(o => BookRow(Buy, o.broker, o.qty, orderTypes(o).bookDisplay)) should equal(buyOrders) | |
sellBook.orders().map(o => BookRow(Sell, o.broker, o.qty, orderTypes(o).bookDisplay)) should equal(sellOrders) | |
} | |
@Then("^the following trades are generated:$") | |
def the_following_trades_are_generated(trades: java.util.List[Trade]) { | |
actualTrades.reverse should equal(trades.toList) | |
actualTrades = Nil | |
} | |
@Then("^no trades are generated$") | |
def no_trades_are_generated() { | |
actualTrades should equal(Nil) | |
} | |
private def events(handler: PartialFunction[OrderBookEvent, Unit]) { | |
matchingEngine.subscribe(new matchingEngine.Sub { | |
def notify(pub: matchingEngine.Pub, event: OrderBookEvent) { | |
handler(event) | |
} | |
}) | |
} | |
private def parseExpectedBooks(book: DataTable): (List[BookRow], List[BookRow]) = { | |
def buildOrders(orders: List[List[String]], side: Side) = { | |
orders.filterNot(_.forall(_.isEmpty)).map(order => { | |
val (broker :: qty :: price :: Nil) = order | |
BookRow(side, broker, qty.toDouble, price) | |
}) | |
} | |
val orders = book.raw().toList.drop(1).map(_.toList) | |
val buy = orders.map(_.take(3)) | |
val sell = orders.map(_.drop(3).reverse) | |
(buildOrders(buy, Buy), buildOrders(sell, Sell)) | |
} | |
private def parseSide(s: String): Side = s.toLowerCase match { | |
case "buy" => Buy | |
case "sell" => Sell | |
} | |
private case class OrderRow(broker: String, side: String, qty: Double, price: String) | |
private case class BookRow(side: Side, broker: String, qty: Double, price: String) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment