R/gg_candles.R

Defines functions gg_candles

Documented in gg_candles

#' Produce a candelstick chart of a given price history.
#' Uses \code{ggplot2} graphics.
#' @param symbol.xts price history with \code{Open} and \code{Close} columns.
#' @param title_param chart title string
#' @param alpha_param graphics color alpha
#' @return the plot for further manipulation
#' @examples
#' \dontrun{
#' library(quantmod)
#' getSymbols("XLP",auto.assign=TRUE)
#' p <- gg_candles(XLP,"Price History")
#' }
gg_candles <- function(symbol.xts, title_param = NA, alpha_param = 1) {
  # dodges command check complaints
  Date <- Low <- High <- change <- width_s <- Open <- Close <- NULL

  df <- as.data.frame(symbol.xts)
  df$Date <- zoo::index(symbol.xts)
  df$change <- ifelse(df$Close > df$Open,
                      "up",
                      ifelse(df$Close < df$Open, "down", "flat"))

  width_candidates <- c(as.numeric(difftime(df$Date[2],
                                            df$Date[1]),
                                   units = "secs"),
                        as.numeric(difftime(df$Date[3],
                                            df$Date[2]),
                                   units = "secs"),
                        as.numeric(difftime(df$Date[4],
                                            df$Date[3]),
                                   units = "secs"))

  df$width_s <- min(width_candidates)

  # define the vector of candle colours either by name or by rgb()
  candle_colors <- c("down" = grDevices::rgb(192, 0, 0,
                                            alpha = 255,
                                            maxColorValue = 255),
                    "up" = grDevices::rgb(0, 192, 0,
                                          alpha = 255,
                                          maxColorValue = 255),
                    "flat" = grDevices::rgb(0, 0, 192,
                                            alpha = 255,
                                            maxColorValue = 255))

  g <- ggplot2::ggplot(df, ggplot2::aes(x = Date)) +
    ggplot2::geom_linerange(ggplot2::aes(ymin = Low,
                                         ymax = High,
                                         colour = change),
                            alpha = alpha_param) +
    ggplot2::theme_bw() +
    ggplot2::ggtitle(title_param) +
    ggplot2::ylab("Price") +
    ggplot2::xlab(NULL) +
    ggplot2::geom_rect(ggplot2::aes(xmin = Date - width_s / 2 * 0.9,
                                    xmax = Date + width_s / 2 * 0.9,
                                    ymin = pmin(Open, Close),
                                    ymax = pmax(Open, Close),
                                    fill = change),
                       alpha = alpha_param) +
    ggplot2::guides(fill = FALSE, colour = FALSE) +
    ggplot2::scale_color_manual(values = candle_colors) +
    ggplot2::scale_fill_manual(values = candle_colors)

  # Handle special cases: flat bar and Open == close:
  if (any(df$change == "flat"))
    g <- g + ggplot2::geom_segment(data = df[df$change == "flat", ],
                                   ggplot2::aes(x = Date - width_s / 2 * 0.9,
                                                y = Close,
                                                yend = Close,
                                                xend = Date + width_s / 2 * 0.9,
                                                colour = change),
                                   alpha = alpha_param)
  g
}
greatgray/scorecard documentation built on May 17, 2019, 8:34 a.m.