Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.economicsl.auctions.{AskOrder, Price, Quantity, Tradable}


/** Base trait for a limit order to sell some `Tradable`. */
trait LimitAskOrder[+T <: Tradable] extends AskOrder[T] with SinglePricePoint[T]
trait LimitAskOrder[+T <: Tradable, Q <: Quantity[_]] extends AskOrder[T] with SinglePricePoint[T, Q]


/** Companion object for `LimitAskOrder`.
Expand All @@ -30,14 +30,14 @@ trait LimitAskOrder[+T <: Tradable] extends AskOrder[T] with SinglePricePoint[T]
*/
object LimitAskOrder {

implicit def ordering[O <: LimitAskOrder[_ <: Tradable]]: Ordering[O] = SinglePricePoint.ordering[O]
implicit def ordering[O <: LimitAskOrder[_ <: Tradable, _ <: Quantity[_]]]: Ordering[O] = SinglePricePoint.ordering[O]

def apply[T <: Tradable](issuer: UUID, limit: Price, quantity: Quantity, tradable: T): LimitAskOrder[T] = {
def apply[T <: Tradable, Q <: Quantity[_]](issuer: UUID, limit: Price, quantity: Q, tradable: T): LimitAskOrder[T, Q] = {
SinglePricePointImpl(issuer, limit, quantity, tradable)
}

private[this] case class SinglePricePointImpl[+T <: Tradable](issuer: UUID, limit: Price, quantity: Quantity, tradable: T)
extends LimitAskOrder[T]
private[this] case class SinglePricePointImpl[+T <: Tradable, Q <: Quantity[_]](issuer: UUID, limit: Price, quantity: Q, tradable: T)
extends LimitAskOrder[T, Q]

}

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import java.util.UUID
import org.economicsl.auctions.{BidOrder, Price, Quantity, Tradable}

/** Base trait for a limit order to buy some `Tradable`. */
trait LimitBidOrder[+T <: Tradable] extends BidOrder[T] with SinglePricePoint[T]
trait LimitBidOrder[+T <: Tradable, Q <: Quantity[_]] extends BidOrder[T] with SinglePricePoint[T, Q]


/** Companion object for `LimitBidOrder`.
Expand All @@ -29,14 +29,14 @@ trait LimitBidOrder[+T <: Tradable] extends BidOrder[T] with SinglePricePoint[T]
*/
object LimitBidOrder {

implicit def ordering[O <: LimitBidOrder[_ <: Tradable]]: Ordering[O] = SinglePricePoint.ordering[O].reverse
implicit def ordering[O <: LimitBidOrder[_ <: Tradable, _ <: Quantity[_]]]: Ordering[O] = SinglePricePoint.ordering[O].reverse

def apply[T <: Tradable](issuer: UUID, limit: Price, quantity: Quantity, tradable: T): LimitBidOrder[T] = {
def apply[T <: Tradable, Q <: Quantity[_]](issuer: UUID, limit: Price, quantity: Q, tradable: T): LimitBidOrder[T, Q] = {
SinglePricePointImpl(issuer, limit, quantity, tradable)
}

private[this] case class SinglePricePointImpl[+T <: Tradable](issuer: UUID, limit: Price, quantity: Quantity, tradable: T)
extends LimitBidOrder[T]
private[this] case class SinglePricePointImpl[+T <: Tradable, Q <: Quantity[_]](issuer: UUID, limit: Price, quantity: Q, tradable: T)
extends LimitBidOrder[T, Q]

}

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.economicsl.auctions.{Price, Quantity, Tradable}


/** Base trait for a market order to sell some `Tradable`. */
trait MarketAskOrder[+T <: Tradable] extends LimitAskOrder[T] {
trait MarketAskOrder[+T <: Tradable, Q <: Quantity[_]] extends LimitAskOrder[T, Q] {

/** An issuer of a `MarketAskOrder` is willing to sell at any strictly positive price. */
val limit: Price = Price.MinPositiveValue
Expand All @@ -34,12 +34,12 @@ trait MarketAskOrder[+T <: Tradable] extends LimitAskOrder[T] {
*/
object MarketAskOrder {

def apply[T <: Tradable](issuer: UUID, quantity: Quantity, tradable: T): MarketAskOrder[T] = {
def apply[T <: Tradable, Q <: Quantity[_]](issuer: UUID, quantity: Q, tradable: T): MarketAskOrder[T, Q] = {
SinglePricePointImpl(issuer, quantity, tradable)
}

private[this] case class SinglePricePointImpl[+T <: Tradable](issuer: UUID, quantity: Quantity, tradable: T)
extends MarketAskOrder[T]
private[this] case class SinglePricePointImpl[+T <: Tradable, Q <: Quantity[_]](issuer: UUID, quantity: Q, tradable: T)
extends MarketAskOrder[T, Q]

}

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.economicsl.auctions.{Price, Quantity, Tradable}


/** Base trait for a market order to buy a particular `Tradable`. */
trait MarketBidOrder[+T <: Tradable] extends LimitBidOrder[T] {
trait MarketBidOrder[+T <: Tradable, Q <: Quantity[_]] extends LimitBidOrder[T, Q] {

/** An issuer of a `MarketBidOrder` is willing to pay any finite price. */
val limit: Price = Price.MaxValue
Expand All @@ -34,11 +34,11 @@ trait MarketBidOrder[+T <: Tradable] extends LimitBidOrder[T] {
*/
object MarketBidOrder {

def apply[T <: Tradable](issuer: UUID, quantity: Quantity, tradable: T): MarketBidOrder[T] = {
def apply[T <: Tradable, Q <: Quantity[_]](issuer: UUID, quantity: Q, tradable: T): MarketBidOrder[T, Q] = {
SinglePricePointImpl(issuer, quantity, tradable)
}

private[this] case class SinglePricePointImpl[+T <: Tradable](issuer: UUID, quantity: Quantity, tradable: T)
extends MarketBidOrder[T]
private[this] case class SinglePricePointImpl[+T <: Tradable, Q <: Quantity[_]](issuer: UUID, quantity: Q, tradable: T)
extends MarketBidOrder[T, Q]

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import scala.collection.GenIterable


/** Mixin trait providing a schedule of price-quantity pairs for an order. */
trait PriceQuantitySchedule[+T <: Tradable] {
trait PriceQuantitySchedule[+T <: Tradable, Q <: Quantity[_]] {
this: Order[T] =>

type PricePoint = (Price, Quantity)
type PricePoint = (Price, Q)

/** A schedule is a step-wise specification of an `Order` to buy (or sell) various quantities
* of a `Tradable` at specific, discrete price-points.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scala.collection.immutable


/** Mixin trait defining an `Order` for multiple units of a `Tradable` at some limit price. */
trait SinglePricePoint[+T <: Tradable] extends PriceQuantitySchedule[T] {
trait SinglePricePoint[+T <: Tradable, Q <: Quantity[_]] extends PriceQuantitySchedule[T, Q] {
this: Order[T] =>

/** Limit price (per unit of the `Tradable`) for the Order.
Expand All @@ -31,9 +31,9 @@ trait SinglePricePoint[+T <: Tradable] extends PriceQuantitySchedule[T] {
def limit: Price

/** Desired number of units of the `Tradable`. */
def quantity: Quantity
def quantity: Q

val schedule: immutable.Map[Price, Quantity] = immutable.Map(limit -> quantity)
val schedule: immutable.Map[Price, Q] = immutable.Map(limit -> quantity)

/** The total value of the order */
val value: Currency = limit.value * quantity.value
Expand All @@ -52,7 +52,7 @@ object SinglePricePoint {
* @tparam O the sub-type of `Order with SinglePricePoint` that is being ordered.
* @return and `Ordering` defined over `Order with SinglePricePoint` instances.
*/
def ordering[O <: Order[_ <: Tradable] with SinglePricePoint[_ <: Tradable]]: Ordering[O] = {
def ordering[O <: Order[_ <: Tradable] with SinglePricePoint[_ <: Tradable, _ <: Quantity[_]]]: Ordering[O] = {
Ordering.by(o => (o.limit, o.issuer))
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/scala/org/economicsl/auctions/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ package object auctions {
/** Type used to representing currencies. */
type Currency = Double // todo should this be Long or Double?


/** Classes representing discrete and continuous quantities. */
sealed abstract class Quantity[@specialized (Long, Double) +T](implicit ev: T => Double) {

def value: T

}

final case class DiscreteQuantity(value: Long) extends Quantity[Long]

final case class ContinuousQuantity(value: Double) extends Quantity[Double]

}
12 changes: 7 additions & 5 deletions src/main/scala/org/economicsl/auctions/sandbox.sc
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,31 @@ class Apple extends Security
// Create a multi-unit limit ask order...
val issuer = UUID.randomUUID()
val google = new Google()
val order1: multiunit.LimitAskOrder[Google] = multiunit.LimitAskOrder(issuer, Price(10), Quantity(100), google)

val order1 = multiunit.LimitAskOrder(issuer, Price(10), DiscreteQuantity(100), google)
order1.value


// Create a multi-unit market ask order...
val order2: multiunit.MarketAskOrder[Google] = multiunit.MarketAskOrder(issuer, Quantity(100), google)
val order2 = multiunit.MarketAskOrder(issuer, DiscreteQuantity(100), google)

// Create some single-unit limit ask orders...
val order3: singleunit.LimitAskOrder[Google] = singleunit.LimitAskOrder(issuer, Price(5.0), google)
val order4: singleunit.LimitAskOrder[Google] = singleunit.LimitAskOrder(issuer, Price(6.0), google)

// Create a multi-unit limit bid order...
val order5: multiunit.LimitBidOrder[Google] = multiunit.LimitBidOrder(issuer, Price(10), Quantity(100), google)
val order5 = multiunit.LimitBidOrder(issuer, Price(10), ContinuousQuantity(150.5), google)

// Create a multi-unit market bid order...
val order7: multiunit.MarketBidOrder[Google] = multiunit.MarketBidOrder(issuer, Quantity(100), google)
val order7 = multiunit.MarketBidOrder(issuer, ContinuousQuantity(100), google)

// Create some single-unit limit bid orders...
val order8: singleunit.LimitBidOrder[Google] = singleunit.LimitBidOrder(issuer, Price(10.0), google)
val order9: singleunit.LimitBidOrder[Google] = singleunit.LimitBidOrder(issuer, Price(6.0), google)

// Create an order for some other tradable
val apple = new Apple()
val order10: singleunit.LimitBidOrder[Apple] = singleunit.LimitBidOrder(issuer, Price(55.9), apple)
val order10 = singleunit.LimitBidOrder(issuer, Price(55.9), apple)

// Create a four-heap order book and add some orders...
val orderBook = FourHeapOrderBook.empty[Google]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ limitations under the License.
*/
package org.economicsl.auctions.singleunit

import org.economicsl.auctions.{Order, Quantity, Tradable, multiunit}
import org.economicsl.auctions.{Order, DiscreteQuantity, Tradable, multiunit}


/** Mixin trait that restricts the quantity of an `Order with SinglePricePoint` to a single unit of a `Tradable`. */
trait SingleUnit[+T <: Tradable] extends multiunit.SinglePricePoint[T] {
trait SingleUnit[+T <: Tradable] extends multiunit.SinglePricePoint[T, DiscreteQuantity] {
this: Order[T] =>

val quantity = Quantity(1)
val quantity: DiscreteQuantity = DiscreteQuantity(1)

}

Expand Down