Skip to content

Instantly share code, notes, and snippets.

@joshuaulrich
Created November 5, 2020 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuaulrich/c9d018f21feca087228ee4afcda630bb to your computer and use it in GitHub Desktop.
Save joshuaulrich/c9d018f21feca087228ee4afcda630bb to your computer and use it in GitHub Desktop.
update.realtime <-
function(ticker, history = NULL)
{
if (is.null(history)) {
history <- getSymbols(ticker, from = "2018-01-01", auto.assign = FALSE)
}
today <- getQuote(ticker, src = "yahoo")
now <- as.Date(today[,"Trade Time"])
cn <- c("Open", "High", "Low", "Last", "Volume", "Last")
live <- xts(today[, cn], now)
if (end(history) == now) {
output <- history
output[now, ] <- live
} else {
output <- rbind(history, live)
}
output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment