Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kowalgta
Last active January 2, 2016 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kowalgta/8370875 to your computer and use it in GitHub Desktop.
Save kowalgta/8370875 to your computer and use it in GitHub Desktop.
#load @"..\packages\Deedle.0.9.12\Deedle.fsx"
#load @"..\packages\FSharp.Charting.0.90.5\FSharp.Charting.fsx"
// Please note that I had to use FSharp.Data.2.0.0-alpha as current stable version 1.1.10
// has a bug in CSV provider that disallows to load from more than 2 sources in one execution
#r @"..\packages\FSharp.Data.2.0.0-alpha2\lib\net40\FSharp.Data.dll"
#r @"..\packages\MathNet.Numerics.FSharp.2.6.0\lib\net40\MathNet.Numerics.FSharp.dll"
#r @"..\packages\MathNet.Numerics.2.6.2\lib\net40\mathnet.numerics.dll"
open Deedle
open System
open FSharp.Data
open FSharp.Charting
open MathNet.Numerics.Statistics
type StockData = CsvProvider<"http://ichart.finance.yahoo.com/table.csv?s=SPX", InferRows=5>
let urlFor symbol (startDate:System.DateTime) (endDate:System.DateTime) =
sprintf "http://ichart.finance.yahoo.com/table.csv?s=%s&a=%i&b=%i&c=%i&d=%i&e=%i&f=%i"
symbol
(startDate.Month - 1) startDate.Day startDate.Year
(endDate.Month - 1) endDate.Day endDate.Year
let loadStocks symbols startDate endDate =
[ for symbol in symbols ->
symbol =>
let data = StockData.Load(urlFor symbol startDate endDate).Data
Series.ofObservations [ for d in data -> (d.Date, d.Close)]]
|> Frame.ofColumns
|> Frame.orderRows
let startDate = new DateTime(2011, 01, 01)
let endDate = DateTime.Now
let symbols = ["AAPL"; "MSFT"; "GOOG"; "XOM"; "SPX"; "GLD"]
let stocks = loadStocks symbols startDate endDate
// Normalize returned values so they can be easily compared
let normalized =
stocks
|> Frame.mapColValues (fun os ->
let osAsFloat = os.As<float>()
let firstItem = osAsFloat.GetAt(0)
osAsFloat / firstItem)
// Display normalized values of all the indexes, looks like bullyish equities market is back :-)
Chart.Combine
[ for s in normalized.GetAllSeries()
-> Chart.Line(s.Value |> Series.observations, Name = s.Key) ]
|> Chart.WithLegend (Docking = ChartTypes.Docking.Top)
// Calculate daily returns
let dailyReturns =
stocks
|> Frame.mapColValues (fun os ->
os.As<float>()
|> Series.pairwiseWith (fun k (v1, v2) -> v2 / v1 - 1.0))
// Visualise and calculate correlation between two indexes
let correlationChart index1 index2 =
let values1 = dailyReturns.GetSeries<float>(index1) |> Series.values
let values2 = dailyReturns.GetSeries<float>(index2) |> Series.values
let correlationValue = Correlation.Pearson(values1, values2)
Chart.Point (Seq.zip values1 values2,
Name = sprintf "Correlation coefficient between %s and %s is %f"
index1 index2 correlationValue)
|> Chart.WithLegend (Docking = ChartTypes.Docking.Top)
|> Chart.WithXAxis (Enabled = false)
|> Chart.WithYAxis (Enabled = false)
// That's pretty correlated
correlationChart "SPX" "XOM"
// Not really correlatde
correlationChart "SPX" "GLD"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment