Skip to content

Commit

Permalink
Merge 5680261 into 39b9db0
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrpugh committed Apr 30, 2015
2 parents 39b9db0 + 5680261 commit a4d6b6d
Show file tree
Hide file tree
Showing 25 changed files with 899 additions and 217 deletions.
31 changes: 31 additions & 0 deletions app/models/AuctionMechanismProvider.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2015 David R. Pugh
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package models

import akka.actor.{Props, ActorRef, Actor, ActorLogging}
import com.typesafe.config.ConfigFactory


trait AuctionMechanismProvider {
this: MarketLike =>

private val conf = ConfigFactory.load("markets.conf")
private val auctionMechanismClass = Class.forName(conf.getString("securities-market.auction-mechanism"))

val auctionMechanism: ActorRef = context.actorOf(Props(auctionMechanismClass, instrument), "auction-mechanism")

}
1 change: 1 addition & 0 deletions app/models/BilateralClearingMechanism.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class BilateralClearingMechanism extends ClearingMechanismLike {

val clearingMechanismBehavior: Receive = {
case fill: FillLike =>
log.info(s",${System.nanoTime()}" + fill.toString)
val transactionHandler = context.actorOf(Props[TransactionHandler])
transactionHandler ! fill
}
Expand Down
11 changes: 4 additions & 7 deletions app/models/CCPClearingMechanism.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ package models
import akka.actor.{ActorRef, Props}


/** Central counter party (CCP) clearing mechanism.
/** Central counterparty (CCP) clearing mechanism.
*
* @note The key difference between CCP clearing and bilateral clearing is that
* CCP inserts itself as the counter-party to both the ask and the bid
* CCP inserts itself as the counterparty to both the ask and the bid
* trading parties before processing the final transaction. By acting as
* a counter-party to every transaction, the CCP assumes all counter-party
* risk.
* a counterparty on every transaction the CCP effectively assumes all
* counterparty risk.
*/
class CCPClearingMechanism extends ClearingMechanismLike
with AssetsHolderLike {

/** Central counterparty For now assume that central counter party has "deep pockets". */
var cash: Double = Double.PositiveInfinity

/* BilateralClearingMechanism can be used to process novated fills. */
val bilateralClearingMechanism: ActorRef = context.actorOf(Props[BilateralClearingMechanism])

Expand Down
4 changes: 2 additions & 2 deletions app/models/ClearingMechanismLike.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package models
import akka.actor.{ActorLogging, Actor}


trait ClearingMechanismLike extends Actor with
ActorLogging {
trait ClearingMechanismLike extends Actor
with ActorLogging {

def clearingMechanismBehavior: Receive

Expand Down
31 changes: 31 additions & 0 deletions app/models/ClearingMechanismProvider.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2015 David R. Pugh
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package models

import akka.actor.{Props, ActorRef, Actor, ActorLogging}
import com.typesafe.config.ConfigFactory


trait ClearingMechanismProvider {
this: MarketLike =>

private val conf = ConfigFactory.load("markets.conf")
private val clearingMechanismClass = Class.forName(conf.getString("securities-market.clearing-mechanism"))

val clearingMechanism: ActorRef = context.actorOf(Props(clearingMechanismClass), "clearing-mechanism")

}
28 changes: 17 additions & 11 deletions app/models/DoubleAuctionMechanism.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@ limitations under the License.

package models

import akka.actor.ActorRef
import akka.actor.{Actor, ActorLogging, Props}


case class DoubleAuctionMechanism(clearingMechanism: ActorRef, instrument: AssetLike) extends AuctionMechanismLike with
MatchingEngineLike {
object DoubleAuctionMechanism {

def props(instrument: SecurityLike): Props = {
Props(DoubleAuctionMechanism(instrument))
}

}


case class DoubleAuctionMechanism(instrument: SecurityLike) extends Actor
with ActorLogging
with MatchingEngineLike {

val askOrderBook: AskOrderBook = AskOrderBook(instrument)

Expand All @@ -31,9 +41,9 @@ case class DoubleAuctionMechanism(clearingMechanism: ActorRef, instrument: Asset
/** Receive a bid (i.e. buy) or ask (i.e., sell) order for an instrument. */
def receive = {
case askOrder: AskOrderLike =>
sender() ! OrderReceived; tryFindMatchingBid(askOrder)
tryFindMatchingBid(askOrder)
case bidOrder: BidOrderLike =>
sender() ! OrderReceived; tryFindMatchingAsk(bidOrder)
tryFindMatchingAsk(bidOrder)
}

/** Attempt to match incoming Bid orders.
Expand Down Expand Up @@ -137,9 +147,8 @@ case class DoubleAuctionMechanism(clearingMechanism: ActorRef, instrument: Asset
*/
def generatePartialFill(ask: AskOrderLike, bid: BidOrderLike, price: Double, quantity: Double): Unit = {
val partialFill = PartialFill(ask.tradingPartyRef, bid.tradingPartyRef, instrument, price, quantity)
log.info(s",${System.nanoTime()}" + partialFill.toString)
updateReferencePrice(price)
clearingMechanism ! partialFill
context.parent ! partialFill
}

/** Generate a totally filled order.
Expand All @@ -151,9 +160,8 @@ case class DoubleAuctionMechanism(clearingMechanism: ActorRef, instrument: Asset
*/
def generateTotalFill(ask: AskOrderLike, bid: BidOrderLike, price: Double, quantity: Double): Unit = {
val totalFill = TotalFill(ask.tradingPartyRef, bid.tradingPartyRef, instrument, price, quantity)
log.info(s",${System.nanoTime()}" + totalFill.toString)
updateReferencePrice(price)
clearingMechanism ! totalFill
context.parent ! totalFill
}

/** Update the reference price for the security. */
Expand All @@ -163,8 +171,6 @@ case class DoubleAuctionMechanism(clearingMechanism: ActorRef, instrument: Asset

}

case object OrderReceived




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,15 @@ limitations under the License.

package models

import akka.actor.{ActorRef, ActorLogging, Actor}
import akka.actor.{ActorLogging, Actor}


/** Base trait for all AuctionMechanisms */
trait AuctionMechanismLike extends Actor with
/** Base trait for all Exchanges.
*
* @note An Exchange should be thought of as a collection of MarketLike of a
* certain type.
*/
trait ExchangeLike extends Actor with
ActorLogging {

/** Security being traded via the auction mechanism. */
def instrument: AssetLike

/** ActorRef for a ClearingMechanismLike actor.
*
* @note The ClearMechanismLike actor processes the transaction(s) for
* each FillLike instance generated via the AuctionMechanism.
*/
def clearingMechanism: ActorRef

}
6 changes: 3 additions & 3 deletions app/models/Fill.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sealed trait FillLike {

val bidTradingPartyRef: ActorRef

val instrument: AssetLike
val instrument: SecurityLike

val price: Double

Expand All @@ -43,7 +43,7 @@ sealed trait FillLike {
*/
case class PartialFill(askTradingPartyRef: ActorRef,
bidTradingPartyRef: ActorRef,
instrument: AssetLike,
instrument: SecurityLike,
price: Double,
quantity: Double) extends FillLike {

Expand All @@ -66,7 +66,7 @@ case class PartialFill(askTradingPartyRef: ActorRef,
*/
case class TotalFill(askTradingPartyRef: ActorRef,
bidTradingPartyRef: ActorRef,
instrument: AssetLike,
instrument: SecurityLike,
price: Double,
quantity: Double) extends FillLike {

Expand Down
2 changes: 1 addition & 1 deletion app/models/LimitAskOrder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import akka.actor.ActorRef
* @param quantity Desired quantity of the security.
*/
case class LimitAskOrder(tradingPartyRef: ActorRef,
instrument: AssetLike,
instrument: SecurityLike,
limitPrice: Double,
quantity: Double) extends
AskOrderLike with
Expand Down
2 changes: 1 addition & 1 deletion app/models/LimitBidOrder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import akka.actor.ActorRef
* @param quantity Desired quantity of the security.
*/
case class LimitBidOrder(tradingPartyRef: ActorRef,
instrument: AssetLike,
instrument: SecurityLike,
limitPrice: Double,
quantity: Double) extends
BidOrderLike with
Expand Down
40 changes: 40 additions & 0 deletions app/models/MarketLike.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2015 David R. Pugh
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package models

import akka.actor.{ActorRef, ActorLogging, Actor}


/** Base trait for all Market classes.
*
* @note Market has two functions: price and quantity formation (i.e., generating
* transactions) via some AuctionMechanismLike; processing of transactions
* via some ClearingMechanismLike.
*/
trait MarketLike extends Actor
with ActorLogging {

/** Security traded on the market. */
def instrument: SecurityLike

/** Auction mechanism used to generate transaction prices and quantities. */
def auctionMechanism: ActorRef

/** Clearing mechanism used to process transactions. */
def clearingMechanism: ActorRef

}
11 changes: 7 additions & 4 deletions app/models/NoiseTrader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package models
import akka.actor.{ActorLogging, ActorRef, Actor}
import com.typesafe.config.ConfigFactory

import scala.collection.mutable
import scala.util.Random


Expand Down Expand Up @@ -53,9 +52,13 @@ case class NoiseTrader(market: ActorRef,
prng.nextDouble() * maxQuantity
}

def decideInstrument(): AssetLike = {
val idx = prng.nextInt(assets.size)
assets.keys.toList(idx)
def decideInstrument(): SecurityLike = {
// find all securities
val securities = assets.filterKeys(_.isInstanceOf[SecurityLike])

// pick a security at random
val idx = prng.nextInt(securities.size)
securities.keys.toList(idx).asInstanceOf[SecurityLike]
}

def generateNewAskOrder(): AskOrderLike = {
Expand Down
4 changes: 2 additions & 2 deletions app/models/OrderBookLike.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sealed trait OrderBookLike {
}


case class AskOrderBook(instrument: AssetLike) extends
case class AskOrderBook(instrument: SecurityLike) extends
mutable.PriorityQueue[AskOrderLike] with
OrderBookLike {

Expand All @@ -48,7 +48,7 @@ case class AskOrderBook(instrument: AssetLike) extends
}


case class BidOrderBook(instrument: AssetLike) extends
case class BidOrderBook(instrument: SecurityLike) extends
mutable.PriorityQueue[BidOrderLike] with
OrderBookLike {

Expand Down
2 changes: 1 addition & 1 deletion app/models/OrderLike.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait OrderLike {
def buy: Boolean

/** The unique identifier of the security. */
def instrument: AssetLike
def instrument: SecurityLike

/** The quantity being bought or sold. */
def quantity: Double
Expand Down

0 comments on commit a4d6b6d

Please sign in to comment.