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
object OrderType { | |
def all(buy: => OrderBook, sell: => OrderBook): PartialFunction[Order, OrderType] = { | |
// omitted [unmodified] order type definitions for limit and market orders | |
// ... | |
case self@PegOrder(_, side, _) => new OrderType { | |
private lazy val book = side match { | |
case Buy => buy |
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
sealed trait OrderBookEvent | |
case class Trade(buyingBroker: String, sellingBroker: String, | |
price: Double, qty: Double) extends OrderBookEvent |
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
sealed trait PriceLevel | |
case class LimitPrice(limit: Double) extends PriceLevel | |
case object MarketPrice extends PriceLevel |
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
sealed trait PriceLevel | |
case class LimitPrice(limit: Double) extends PriceLevel |
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
trait Order { | |
val broker: String | |
val qty: Double | |
val side: Side | |
} | |
case class LimitOrder(broker: String, side: Side, qty: Double, limit: Double) extends Order | |
case class MarketOrder(broker: String, side: Side, qty: Double) extends 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
object OrderType { | |
def all(): PartialFunction[Order, OrderType] = { | |
// Unmodified code omitted | |
// ... | |
case self@MarketOrder(_, _, _) => new OrderType { | |
def bookDisplay: String = "MO" |
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
trait OrderType { | |
def bookDisplay: String | |
def price: PriceLevel | |
} | |
object OrderType { | |
def all(): PartialFunction[Order, OrderType] = { |
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
Feature: Maintaining reference price as trades occur | |
Scenario: Updating reference price as it is set at the opening and trades occur | |
Given the reference price is set to "10" | |
When the following orders are submitted in this order: | |
| Broker | Side | Qty | Price | | |
| A | Buy | 100 | 11 | | |
| B | Sell | 100 | 11 | | |
Then the following trades are generated: |
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
# Matching using a reference price | |
Scenario: Matching incoming Buy market order against Sell market order when no best limit price is available | |
Given the reference price is set to "10" | |
Given the following orders are submitted in this order: | |
| Broker | Side | Qty | Price | | |
| A | Sell | 100 | MO | | |
| B | Buy | 100 | MO | | |
Then the following trades are generated: | |
| Buying broker | Selling broker | Qty | Price | |
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 MatchingEngine(buy: OrderBook, sell: OrderBook, orderTypes: (Order => OrderType)) | |
extends mutable.Publisher[OrderBookEvent] { | |
private var _referencePrice: Option[Double] = None | |
def referencePrice = _referencePrice.get | |
def referencePrice_=(price: Double) { | |
_referencePrice = Some(price) | |
} |