1
votes

I have a class that mixes in a number of different traits the encode behavior for matching orders given various available order types. The class definition looks as follows...

class DoubleAuctionMarket(val security: Security) extends EventBus {
  this: MatchingEngine =>

The relevant parts of the base MatchingEngine trait look as follows...

trait MatchingEngine {

  /** Implements crossing logic for various types of orders. */
  def crosses(incoming: Order, top: Order): Boolean

  /** Implements price formation rules for various types of orders. */
  def formPrice(incoming: Order, top: Order): Double

}

I have another trait called LimitOrderOnlyMatchingEngine that extends the base trait as follows...

trait LimitOrderOnlyMatchingEngine extends MatchingEngine {

  def crosses(incoming: LimitOrder, top: LimitOrder): Boolean = {

    (incoming, top) match {
      case (incoming: LimitOrderAsk, top: LimitOrderBid) =>
        incoming.limit <= top.limit
      case (incoming: LimitOrderBid, top: LimitOrderAsk) =>
        incoming.limit >= top.limit
    }

  }

  def formPrice(incoming: LimitOrder, top: LimitOrder): Double = top.limit

}

Now, when I try to mix in the LimitOrderOnlyMatchingEngine using

new DoubleAuctionMarket(security) with LimitOrderOnlyMatchingEngine

I am told that "object creation is impossible" because neither the crosses method nor the formPrice method have been implemented as required by the self type annotation I used.

Not sure what is going wrong. I suspect that either:

  1. I need to somehow override the relevant methods in the LimitOrderOnlyMatchingEngine or
  2. I need to define the input types for those methods in the base class differently.

Thoughts?

1

1 Answers

0
votes

The problem is that you're technically not implementing the two methods in the MatchingEngine trait. The trait specifies:

def crosses(incoming: Order, top: Order): Boolean
def formPrice(incoming: Order, top: Order): Double

But you implemented:

def crosses(incoming: LimitOrder, top: LimitOrder): Boolean
def formPrice(incoming: LimitOrder, top: LimitOrder): Double

You could solve it with generics (I never remember if it is +Order or -Order ...) but the other way is that because you're using pattern matching, it will work just by changing the argument types - though you'll probably want to cover the other order types one way or another.

I added stubs for classes like Order, LimitOrder etc and the following compiles nicely:

// NOTE: Order not LimitOrder
def crosses(incoming: Order, top: Order): Boolean = {
  (incoming, top) match {
    case (incoming: LimitOrderAsk, top: LimitOrderBid) =>
      incoming.limit <= top.limit
    case (incoming: LimitOrderBid, top: LimitOrderAsk) =>
      incoming.limit >= top.limit
  }
}

// NOTE: Order not LimitOrder
def formPrice(incoming: Order, top: Order): Double = 0.0 // Note pattern matching needed

As a side note, it's great Scala is making headway into the finance world.