Can chartSeries, candleChart, or barChart be used to create an intraday chart in R?
chartSeries, candleChart, and barChart are part of the quantmod package for R.
2 Answers
Answers 1
First we need some example intraday trading data, which you can get for free from a variety of sites including Google's Undocumented Finance API.
Get some example data (hourly intervals)
query_addr <- 'https://www.google.com/finance/getprices' stock_symb <- 'GOOG' stock_exch <- 'NASD' intvl_size <- 60*60 # 1 hr interval (in seconds) -- use 24*this for daily period_len <- '90d' output_fmt <- 'd,o,h,l,c,v' # date, open, high, low, close, volume  library(httr) resp <-    POST(url = query_addr,         query = list(q = stock_symb,                      x = stock_exch,                      i = intvl_size,                      p = period_len,                     f = output_fmt) )  df <-    read.csv(text = content(resp),            skip = 7,            header = FALSE,            stringsAsFactors = FALSE)  # we need a function to munge the date convention used by google finance API g_fin_date <- function(dstr, intvl_size){   unix_dates <- numeric(length(dstr))   date_is_unix <- grepl('^a',dstr)   unix_dates[date_is_unix] <- as.numeric(sub('^a','',dstr[date_is_unix]))   for(i in 2L:length(dstr)){     if(!date_is_unix[i]){       unix_dates[i] <- unix_dates[i-1] + intvl_size     }   }   return(as.POSIXct(unix_dates,origin="1970-01-01",tz="GMT" )) }  # see header of resp text for column order names(df) <- c('close_date','Close','High','Low','Open','Volume')  df[,'close_date'] <- g_fin_date(df[,'close_date'], intvl_size=intvl_size)   Here I have just chosen hourly open (i.e. beginning price), high, low, close (i.e. ending price)-- but you can specify a finer level of detail if you desire -- it will still roll up to a larger period with quantmod::to.period().
Make an xts
  Once we have a data frame (such as you might obtain from an API or flat file) then you need to convert the data to xts.  Note that for xts the timestamp must be the row name (and can be dropped from the columns).
library(xts) rownames(df) <- df$close_date df$close_date <- NULL   Convert to OHLC (Open, High, Low, Close) using xts
  This is straightforward aggregation -- see ?to.period
GOOG <- to.hourly(as.xts(df)) # for daily use to.daily(as.xts(df))   More chart examples available at quantmod.com.
Make some charts using quantmod
  There are great charts already built into quantmod, including the ones you mentioned.
library(quantmod) chartSeries(GOOG) barChart(GOOG, theme='white.mono',bar.type='hlc') candleChart(GOOG,multi.col=TRUE,theme='white')    Enjoy your charts
Answers 2
Me: "I'll take intra-day time series charts in R for 100 Alex!" :D 
Alex: "THIS popular format for financial time series can be used by quantmod functions chartSeries, candleChart, and barChart to create intraday charts in R"
Me: "What is an xts object, indexed by data/time stamp, containing prices for the Open, High, Low, and Close?
Alex: "Right you are!"



0 comments:
Post a Comment