@orpha
To compute On-Balance Volume (OBV) using F#, you can follow these steps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let calculateOBV (prices : float list) (volumes : float list) =
let rec calculateOBVHelper (prices : float list) (volumes : float list) (obv : float list) =
match prices, volumes with
| [], _ -> obv |> List.rev
| price :: restPrices, volume :: restVolumes ->
let change =
match restPrices with
| [] -> 0.0
| hd :: _ -> if price > hd then volume else if price < hd then -volume else 0.0
let newObv =
match obv with
| [] -> change
| hd :: _ -> hd + change
calculateOBVHelper restPrices restVolumes (newObv :: obv)
calculateOBVHelper prices volumes []
|
1 2 3 4 |
let prices = [100.0; 105.0; 110.0; 105.0; 115.0] let volumes = [500.0; 600.0; 700.0; 300.0; 800.0] let obv = calculateOBV prices volumes printfn "On-Balance Volume (OBV): %A" obv |
This code snippet will calculate the OBV values for the given price changes and volumes and print the result.