我写了这个脚本来绘制历史财务数据:
open FSharp.Data
#load "C:\Users\Nick\Documents\Visual Studio 2013\Projects\TryFsharp\packages\FSharp.Charting.0.90.9\FSharp.Charting.fsx"
open FSharp.Charting
open System
let plotprice nasdaqcode =
let url = "http://ichart.finance.yahoo.com/table.csv?s="+nasdaqcode
let company = CsvFile.Load(url)
let companyPrices = [ for r in company.Rows -> r.GetColumn "Date", r.GetColumn "Close" ]
(companyPrices
|> List.sort
|> Chart.Line).WithTitle(nasdaqcode, InsideArea=false)
plotprice "MSFT"
plotprice "ORCL"
plotprice "GOOG"
plotprice "NTES"
这很有效。
问题:
部分数据从1986年开始,部分数据从2000年开始。我想绘制2000年至2015年的数据。如何选择这段时间?
是否可以显示鼠标悬停在图表上的时间?
答案 0 :(得分:3)
如果您要访问Yahoo数据,那么最好使用CsvProvider
而不是使用F#Data中的CsvFile
。您可以找到有关the type provider here的更多信息。遗憾的是,标准F#数据库和TryFSharp.org上的命名是不同的,所以这有点令人困惑。
CSV类型提供程序将自动推断类型:
open FSharp.Data
open FSharp.Charting
open System
// Generate type based on a sample
type Stocks = CsvProvider<"http://ichart.finance.yahoo.com/table.csv?s=FB">
let plotprice nasdaqcode =
let url = "http://ichart.finance.yahoo.com/table.csv?s=" + nasdaqcode
let company = Stocks.Load(url)
// Now you can access the columns in a statically-typed way
// and the types of the columns are inferred from the sample
let companyPrices = [ for r in company.Rows -> r.Date, r.Close ]
// If you want to do filtering, you can now use the `r.Date` property
let companyPrices =
[ for r in company.Rows do
if r.Date > DateTime(2010, 1, 1) && r.Date < DateTime(2011, 1, 1) then
yield r.Date, r.Close ]
// Charting as before
companyPrices |> (...)
我不确定F#Charting库是否有基于鼠标指针位置显示价格的方法 - 它基于标准的.NET Windows窗体图表控件,因此您可以查看{{3} }。
答案 1 :(得分:0)
1)GetColumn获取一个字符串。您需要先将其转换为DateTime并进行比较。即。
let plotprice nasdaqcode =
let url = "http://ichart.finance.yahoo.com/table.csv?s="+nasdaqcode
let company = CsvFile.Load(url)
let companyPrices = [ for r in company.Rows -> DateTime.Parse(r.GetColumn "Date"), r.GetColumn "Close" ]
(companyPrices
|> List.filter (fun (date, _) -> date > DateTime(2000, 1, 1))
|> List.sort
|> Chart.Line).WithTitle(nasdaqcode, InsideArea=false)
2)您可以尝试添加标签(不知道如何在悬停时做...)
let plotprice nasdaqcode =
let url = "http://ichart.finance.yahoo.com/table.csv?s="+nasdaqcode
let company = CsvFile.Load(url)
let companyPrices = [ for r in company.Rows -> DateTime.Parse(r.GetColumn "Date"), r.GetColumn "Close" ]
(companyPrices
|> List.filter (fun (date, _) -> date > DateTime(2000, 1, 1))
|> List.sort
|> fun data -> Chart.Line(data, Labels=(Seq.map (fst >> string) data))).WithTitle(nasdaqcode, InsideArea=false)