0 people like it.

Yet Another Financial Contracts (Demo)

Yet another tutorial based on the DSL for modelling financial contracts. This is a walkthrough with examples and tasks that is loaded by the previous 'Setup' snippet. Designed to run in Try F#.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
85: 
86: 
87: 
88: 
89: 
90: 
91: 
92: 
93: 
94: 
95: 
96: 
97: 
98: 
99: 
open System

// --------------------------------------------------------
// Get price on a specified day 

let prices = Yahoo.GetPriceTable(DateTime(2011, 1, 1))
let p1 = prices.["MSFT", DateTime(2011, 1, 10)]
let p2 = prices.["YHOO", DateTime(2012, 1, 10)]

// Print prices using 'printfn' function:
printfn "%f %f" p1 p2

// --------------------------------------------------------
// Simple representation of financial contracts

type Amount = float
type Ticker = string

type Contract = 
  | Trade of Ticker * Amount
  | After of DateTime * Contract
  | Until of DateTime * Contract
  | Both of Contract * Contract

// --------------------------------------------------------
// Sample trades and pattern matching

// Create several values representing contracts
let msft = Trade("MSFT", 1000.0)
let aapl = Trade("AAPL", 200.0)
let both = Both(msft, aapl)

// Print name of a trade, if it is simple Trade
match msft with
| Trade(name, amount) -> printfn "Trade: %s" name
| _ -> printfn "Complex trade"

// --------------------------------------------------------
// TASK #1: Write a function that takes a trade and prints 
// its value (if the trade is Trade) using 'prices' above

// (...)
//
// We would like to be write:
//   evaluate msft (DateTime(2011, 1, 10))

// --------------------------------------------------------
// Simplify construction using functions and operator

let trade (what, amount) = Trade(what, amount)
let after dt contract = After(dt, contract)
let until dt contract = Until(dt, contract)
let ($) c1 c2 = Both(c1, c2)

// TASK #2a: Write function 'sell' that behaves like
// 'trade', but makes the amount negative
// TASK #2b: Write function 'onDate' that creates a 
// trade that can happen only on a specified day

// (...)
 
let itstocks = 
  after (DateTime(2012, 4, 1)) (trade ("MSFT", 1000.0)) $
  onDate (DateTime(2012, 5, 15)) (sell ("AAPL", 200.0))

// --------------------------------------------------------
// Recursive processing of contracts - print what 
// trades can happen on a specified day

let rec run contract day = 
  match contract with 
  | Trade(what, amount) -> 
      printfn "%s (%f)" what amount
  | After(dt, contract) ->
      if day >= dt then run contract day
  | Until(dt, contract) ->
      if day <= dt then run contract day
  | Both(contract1, contract2) ->
      run contract1 day
      run contract2 day

// Test the function using IT stocks
run itstocks (DateTime(2012, 3, 1))
run itstocks (DateTime(2012, 5, 10))

// --------------------------------------------------------
// TASK #3: Write a function 'evaluate' that recursively
// processes the contract and evaluates the total price
// on a specified day using the 'prices' table

// TASK #4 (BONUS): Extend the 'Contract' type and add a 
// new case 'Choice' that represents a choice between two
// contracts. Update the 'evaluate' function to return 
// both minimal and maximal price (depending on which 
// branch of 'Choice' is executed.)

// TASK #5 (BONUS): Write a function 'opposite' that takes
// a contract and builds a new contract where all amounts
// of 'Trade' elements are negative
namespace System
val prices : obj

Full name: Script.prices
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
DateTime()
   (+0 other overloads)
DateTime(ticks: int64) : unit
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
   (+0 other overloads)
val p1 : float

Full name: Script.p1
val p2 : float

Full name: Script.p2
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
type Amount = float

Full name: Script.Amount
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
type Ticker = string

Full name: Script.Ticker
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
type Contract =
  | Trade of Ticker * Amount
  | After of DateTime * Contract
  | Until of DateTime * Contract
  | Both of Contract * Contract

Full name: Script.Contract
union case Contract.Trade: Ticker * Amount -> Contract
union case Contract.After: DateTime * Contract -> Contract
union case Contract.Until: DateTime * Contract -> Contract
union case Contract.Both: Contract * Contract -> Contract
val msft : Contract

Full name: Script.msft
val aapl : Contract

Full name: Script.aapl
val both : Contract

Full name: Script.both
val name : Ticker
val amount : Amount
val trade : what:Ticker * amount:Amount -> Contract

Full name: Script.trade
val what : Ticker
val after : dt:DateTime -> contract:Contract -> Contract

Full name: Script.after
val dt : DateTime
val contract : Contract
val until : dt:DateTime -> contract:Contract -> Contract

Full name: Script.until
val c1 : Contract
val c2 : Contract
val itstocks : Contract

Full name: Script.itstocks
val run : contract:Contract -> day:DateTime -> unit

Full name: Script.run
val day : DateTime
val contract1 : Contract
val contract2 : Contract
Raw view Test code New version

More information

Link:http://fssnip.net/cq
Posted:12 years ago
Author:Tomas Petricek
Tags: try f#