Skip to content

Instantly share code, notes, and snippets.

@ptsaraiva
Forked from anonymous/morningstar.R
Created January 19, 2017 21:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ptsaraiva/91d9304a29c3927a0ce524abe8c0b11b to your computer and use it in GitHub Desktop.
Save ptsaraiva/91d9304a29c3927a0ce524abe8c0b11b to your computer and use it in GitHub Desktop.
How to retrieve data from Morningstar
require(RCurl)
require(jsonlite)
myticker<-"FB"
url.histprice<-function(x){ return(paste0("http://globalquote.morningstar.com/globalcomponent/RealtimeHistoricalStockData.ashx?ticker=",x,"&showVol=true&dtype=his&f=d&curry=USD&range=1900-1-1|2014-10-10&isD=true&isS=true&hasF=true&ProdCode=DIRECT"))}
url.keyratios<-function(x){return(paste0("http://financials.morningstar.com/ajax/exportKR2CSV.html?t=",x))}
#Retrieve historical prices
json.histprice<-getURL(url.histprice(myticker))
json.histprice<-sub("NaN","\"NA\"",json.histprice)
histprice <- fromJSON(json.histprice)
df.prices<-as.data.frame(histprice$PriceDataList$Datapoints)
df.prices$Volume<-histprice$VolumeList$Datapoints
df.prices$Date<-as.Date(histprice$PriceDataList$DateIndexs[[1]],origin="1899-12-30")
colnames(df.prices) <- c("Open","High","Low","Close", "Volume", "Date")
#Retrieve keyratios. Need to preprocess before reading as csv
str.keyratios<-getURL(url.keyratios(myticker))
kr.fin <- sub(".*Financials\n(.*)Key Ratios -> Profitability.*","\\1",str.keyratios)
kr.margins <- sub(".*Key Ratios -> Profitability\n(.*)Profitability.*","\\1",str.keyratios)
kr.profit <- sub(".*Key Ratios -> Profitability.*(Profitability.*)Key Ratios -> Growth.*","\\1",str.keyratios)
kr.growth<-sub(".*Key Ratios -> Growth\n(.*)Key Ratios -> Cash Flow.*","\\1",str.keyratios)
kr.cashflow<-sub(".*Key Ratios -> Cash Flow\n(.*)Key Ratios -> Financial Health.*","\\1",str.keyratios)
kr.balance<-sub(".*Key Ratios -> Financial Health\n(Balance Sheet Items.*)Liquidity/Financial Health.*","\\1",str.keyratios)
kr.liquid<-sub(".*Key Ratios -> Financial Health.*(Liquidity/Financial Health.*)Key Ratios -> Efficiency Ratios.*","\\1",str.keyratios)
kr.eff<-sub(".*Key Ratios -> Efficiency Ratios\n(.*)","\\1",str.keyratios)
df.fin <-read.csv(textConnection(kr.fin))
df.margins <-read.csv(textConnection(kr.margins))
df.profit <-read.csv(textConnection(kr.profit))
df.growth <-read.csv(textConnection(kr.growth))
df.cashflow <-read.csv(textConnection(kr.cashflow))
df.balance <-read.csv(textConnection(kr.balance))
df.liquid<-read.csv(textConnection(kr.liquid))
df.eff<-read.csv(textConnection(kr.eff))
@anycorp
Copy link

anycorp commented Sep 6, 2017

This is great, many thanks for sharing. Wonder if you can expand it so that the user can use their Morningstar subscription and get say 10yr Income Sheet data from
url_IS <- "http://financials.morningstar.com/ajax/ReportProcess4CSV.html?reportType=is&period=12&dataType=A&order=asc&columnYear=10&rounding=3&denominatorView=raw&t=" %s+% myticker;

Thanks again!

@chmorik
Copy link

chmorik commented May 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment