Created
December 5, 2016 16:46
-
-
Save anonymous/c7d9c19cc67e03641966064d1518ed41 to your computer and use it in GitHub Desktop.
How to retrieve data from Morningstar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This may have changed since you wrote the code: the date origin should be "1900-01-01" rather than "1899-12-30".
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Love your code! Quick suggestion to pull in prices through present:
today <- as.character(Sys.Date())
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|",today,"&isD=true&isS=true&hasF=true&ProdCode=DIRECT"))}