R/shinyplot-plotly_price_f.R

Defines functions plotly_price_f

Documented in plotly_price_f

#' Plotly plot price
#'
#' 
#' Dependencies: library1("TTR", "plotly")
#' Return plot object
#' @importFrom magrittr %>%
#' @param plot_df plot_df
#' @param ticker ticker/code
#' @keywords plotly price chart klse
#' @export
#' @examples
#' plotly_price_f(plot_df, "7087")
plotly_price_f <- function(plot_df, ticker){

      suppressWarnings({
            
      df <- as.data.frame(     plot_df %>% dplyr::filter(code == ticker)      )
      
      sname <- df[1,"stock"]
      fullsname <- paste0(ticker, " - ", sname)
      mindate <- min(df[,"date"])
      maxdate <- max(df[,"date"])
      
      # create Bollinger Bands
      if(nrow(df) > 21){
            
            bbands <- TTR::BBands(df[,c("high","low","close")])
            
            df <- data.frame(df, bbands)
      }
      
      direction <- vector()
      color1 <- '#17BECF'     #up
      color2 <- '#d82929'     #down
      
      u1 <- df$close >= df$open
      direction[u1] <- 'Increasing'
      direction[!u1] <- 'Decreasing'
      i <- list(line = list(color = color1))
      d <- list(line = list(color = color2))
      
      # plot candlestick chart
      p <- df %>%
        plotly::plot_ly(x = ~date, type="candlestick",
                open = ~open, close = ~close,
                high = ~high, low = ~low, name = fullsname,
                increasing = i, decreasing = d,
                height = 720, width = 960
                ) 
      if(nrow(df) > 21){
            p <- p %>% plotly::add_lines(y = ~up , name = "B Bands",
                        line = list(color = '#ccc', width = 0.5),
                        legendgroup = "Bollinger Bands",
                        hoverinfo = "none") %>%
              plotly::add_lines(y = ~dn, name = "B Bands",
                        line = list(color = '#ccc', width = 0.5),
                        legendgroup = "Bollinger Bands",
                        showlegend = FALSE, hoverinfo = "none") %>%
              plotly::add_lines(y = ~mavg, name = "Mv Avg",
                        line = list(color = '#E377C2', width = 0.5),
                        hoverinfo = "none") %>%
              plotly::layout(yaxis = list(title = "Price (Adjusted)"))
      }
      # plot volume bar chart
      pp <- df %>%
        plotly::plot_ly(x=~date, y=~vol, type='bar', name = paste(sname, "Volume"),
                color = ~direction, colors = c(color2, color1)) %>%
        plotly::layout(yaxis = list(title = "Volume"))
      
      # create rangeselector buttons
      rs <- list(visible = TRUE, x = 0.5, y = -0.055, #x =0.5
                 xanchor = 'center', yref = 'paper',
                 font = list(size = 9),
                 buttons = list(
                   list(count=1,
                        label='RESET',
                        step='all'),
                   list(count=1,
                        label='1 YR',
                        step='year',
                        stepmode='backward'),
                   list(count=3,
                        label='3 MO',
                        step='month',
                        stepmode='backward'),
                   list(count=1,
                        label='1 MO',
                        step='month',
                        stepmode='backward')
                 ))
      
      # subplot with shared x axis
      p <- plotly::subplot(p, pp, heights = c(0.8,0.2), nrows=2, #heights = c(0.7,0.2)
                   shareX = TRUE, titleY = TRUE) %>%
        plotly::layout(title = paste0( fullsname, " (", mindate, " to ", maxdate, ")" ),
               xaxis = list(rangeselector = rs),
               legend = list(orientation = 'h', x = 0.5, y = 1,
                             xanchor = 'center', yref = 'paper',
                             font = list(size = 10),
                             bgcolor = 'transparent'))
      
      })
      
      return(p)

}
junyitt/tfunction documentation built on May 4, 2019, 4:23 p.m.