Benutzer:MovGP0/F♯/Volatility
< Benutzer:MovGP0 | F♯
MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
module Volatility
open System
open FSharp.Data
[<Measure>] type Money
[<Measure>] type Day
[<Measure>] type Year
type Stocks = CsvProvider<"http://ichart.finance.yahoo.com/table.csv?s=GOOG&c=2000&a=01&b=01&f=2000&d=12&e=31&g=d&ignore=.csv">
let stdDev (values:seq<float>) =
values
|> Seq.fold (fun acc x -> acc + (1.0 / float (Seq.length values)) * (x - (Seq.average values)) ** 2.0) 0.0
|> sqrt
let calcDailyReturns (prices:seq<float<Money>>) =
prices
|> Seq.pairwise
|> Seq.map (fun (x, y) -> log(x / y) * 1.0<Money>)
let annualVolatility (returns : seq<float<Money>>) =
let dailyReturns = calcDailyReturns(returns)
|> Seq.map (fun (value : float<Money>) -> value * 1.0<1/Money>)
let standardDerivation = stdDev dailyReturns
let days = Seq.length returns
standardDerivation * sqrt(float days) * 1.0<1/Year>
open System
open Volatility
let constructUrl (symbol:string) (fromDate:DateTime) (toDate:DateTime) =
String.Format(@"http://ichart.finance.yahoo.com/table.csv?s={0}&c={1:00}&a={2:00}&b={3:00}&f={4:00}&d={5:00}&e={6:00}&g=d&ignore=.csv",
symbol, fromDate.Year, fromDate.Month - 1, fromDate.Day, toDate.Year, toDate.Month - 1, toDate.Day)
let getPrices stock fromDate toDate =
let url = constructUrl stock fromDate toDate
let data = Stocks.Load(url)
data.Rows |> Seq.map (fun stock -> (Convert.ToDouble(stock.Close)) * 1.0<Money>)
let getAnnualizedVol stock fromStr toStr =
let fromDate = DateTime.Parse fromStr
let toDate = DateTime.Parse toStr
let prices = getPrices stock fromDate toDate
let vol = Math.Round(annualVolatility(prices) * 100.0 * 1.0<Year>, 2)
sprintf "Volatility for %s is %.0f %%" stock vol
[<EntryPoint>]
let main _ =
[| "MSFT"; "ORCL"; "GOOG"; "EBAY"; "AAPL"; "AMZN" |]
|> Array.map (fun symbol -> Console.WriteLine(getAnnualizedVol symbol "2014-01-01" "2014-07-15"))
|> ignore
0
|