Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggested fix for Stockapp #317

Closed
helgasoft opened this issue Sep 7, 2018 · 2 comments
Closed

suggested fix for Stockapp #317

helgasoft opened this issue Sep 7, 2018 · 2 comments

Comments

@helgasoft
Copy link

The first sample app in https://www.opencpu.org/apps.html is very nice, but not operational due to cancellation of Google Finance API. Here is a suggested fix using data from the good guys at IEX.
The function is still called googledata for easy integration, but deserves the name changed to iexdata.

#' IEX data
#' 
#' Download historical prices for a given stock from IEX
#' 
#' @param ticker stock ticker symbol. E.g. "IBM".
#' @param from start date. Either string or date object.
#' @param to end date. Either string or date object.
#' @return dataframe with historical prices
#' @export
googledata <- function(ticker, from = NULL, to = NULL) {
	library(jsonlite)
	myurl <- paste0('https://api.iextrading.com/1.0/stock/',ticker,'/chart/5y')
	mydata <- fromJSON(myurl)		# data.frame
	if (length(mydata)==0) 
		return(NULL)	# ticker not found
	if (!all( c("date","open","high","low","close","volume") %in% names(mydata))) {
		return(NULL)	# ticker has incomplete column list
	}
	mydata <- subset(mydata, select=c(date,open,high,low,close,volume))
	mydata$date <- as.Date(mydata$date, tz='')
	mydata <- mydata[mydata$date>=as.Date(from) & mydata$date<=as.Date(to),]
	names(mydata) <- c('Date','Open','High','Low','Close','Volume')
	
	return(mydata);
}

# test
test.data <- googledata(ticker='IBM', from='2017-07-07', to='2018-05-07');
library(ggplot2)
qplot(Date, Close, data = test.data, geom = c("line", "smooth"));

@jeroen
Copy link
Member

jeroen commented Sep 14, 2018

Thanks! Could you do a PR at https://github.com/rwebapps/stockapp ?

jeroen added a commit to rwebapps/stockapp that referenced this issue Sep 14, 2018
@jeroen
Copy link
Member

jeroen commented Sep 14, 2018

Actually I fixed it already: #317

Thanks for the suggestion!!

@jeroen jeroen closed this as completed Sep 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants