Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to make intraday charts with quantmod and the free barchart getHistory API
# Here we get 5 min intervals from the API, docs:
gdxjson <- fromJSON("<< INSERT YOUR API KEY HERE >>&symbol=GDX&type=minutes&interval=5&startDate=20151019000000")
# Columns that need to be coerced to numeric types, and columns to remove
numeric_columns <- c('open','high','low','close','volume')
rm_columns <- c('timestamp','symbol','tradingDay')
results <- gdxjson$results
# Convert character vector to time objects using lubridate
results$timestamp <- ymd_hms(gdxjson$results$timestamp)
timestamp <- results$timestamp
# Now loop over columns coercing and removing as necessary, is there a better way?
for (col in names(results)) {
if (is.element(col, numeric_columns)) {
results[col] <- as.numeric(results[[col]])
if (is.element(col, rm_columns)) {
results[col] <- NULL
# Convert dataframe to XTS object
gdx_xts <- as.xts(results, timestamp)

This comment has been minimized.

Copy link
Owner Author

uberscientist commented Oct 26, 2015

output chart with EMA:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.