Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to make intraday charts with quantmod and the free barchart getHistory API
load.packages('quantmod')
load.packages('jsonlite')
library(lubridate)
# Here we get 5 min intervals from the API, docs: http://www.barchartondemand.com/api/getHistory
gdxjson <- fromJSON("http://marketdata.websol.barchart.com/getHistory.json?key=<< 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)
chartSeries(gdx_xts)
@uberscientist

This comment has been minimized.

Show comment
Hide comment
@uberscientist

uberscientist Oct 26, 2015

output chart with EMA:
intraday_gdx

Owner

uberscientist commented Oct 26, 2015

output chart with EMA:
intraday_gdx

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