Ivan
Ivan

Reputation: 7746

Modeling domain type

A limit price has a decimal value. A Market price does not.

How do I model this in F#?

module TradingDomain

let NoPrice = None

type PriceActionType = 
    | Limit of decimal
    | Market of NoPrice

Upvotes: 1

Views: 71

Answers (2)

s952163
s952163

Reputation: 6324

There are a couple of ways to go about it, but if you do domain modelling it's a good idea to have an understanding of all the components, in this case, 1) how traders think about the order, 2) how FIX (if that's what's being used) thinks about the order, and 3) how the market you trade thinks about the order. Btw, there's this book that you might find useful. Also, Chapter 7 in F# Deep Dives.

That said Tarmil's answer should work for you, and here but I have two comments. Sometimes it's better to be explicit about the type and use .NET types. float and decimal are F# aliases and they might refer to the function that casts to float. There is also the possibility to use Some and None for expressing the price. So here's a version that includes the orderside as well:

type Price =
    | Limit of Decimal
    | Market

type OrderSide =
    | Buy of Price
    | Sell of Price 
    | ShortSell of Price

You can use it like this: Buy (Limit 10.0M) or Sell Market. You could also define Price like this:

type Price2 =
    | Limit of Decimal option
    | None

Whichever version will help you later do validation.

Upvotes: 0

Tarmil
Tarmil

Reputation: 11362

You can just not give Market any arguments:

type PriceActionType =
    | Limit of decimal
    | Market

Upvotes: 5

Related Questions