Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Monadic style order resolution
trait MonadicStyleNewOrderResolver extends NewOrderResolutionErrors {
val symbolDao: SymbolDao
val marketTakerDao: MarketTakerDao
val marketMakerDao: MarketMakerDao
def resolve(order: NewOrder): Validation[String, ResolvedNewOrder] =
for {
symbol <- symbolDao.findByName(order.symbolName)
.toSuccess(UnknownSymbol)
marketTaker <- marketTakerDao.findByExternalId(order.marketTakerId)
.toSuccess(UnknownMarketTakerId)
account <- marketTaker.accounts.find(_.id == order.accountId)
.toSuccess(UnauthorizedAccount)
marketMaker <- marketMakerDao.findByExternalId(order.marketMakerId)
.toSuccess(UnknownMarketMakerId)
_ <- marketTaker.symbols.find(_ == symbol)
.toSuccess(UnauthorizedSymbol)
_ <- (order.quantity > 0).option(order.quantity)
.toSuccess(InvalidQuantity)
_ <- (order.price > 0).option(order.price)
.toSuccess(InvalidPrice)
} yield
ResolvedNewOrder(
account = account,
symbol = symbol,
marketMaker = marketMaker,
price = order.price,
quantity = order.quantity,
side = order.side
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment