library(plotly)
library(dplyr)
library(VNDS)

df <- VNDS::tq_get('VND','2018-01-01','2018-05-01')
## #VND from 2018-01-01 to 2018-05-01 already cloned
(p <- df %>%
  plot_ly(x = ~date, type="candlestick",
          open = ~open, close = ~close,
          high = ~high, low = ~low) %>%
  layout(title = "Basic Candlestick Chart",
         xaxis = list(rangeslider = list(visible = F))))
#Add anotation and list
# annotation
a <- list(text = "Stock Split",
          x = '2018-01-01',
          y = 1.02,
          xref = 'x',
          yref = 'paper',
          xanchor = 'left',
          showarrow = FALSE
)

# use shapes to create a line
l <- list(type = line,
          x0 = '2018-01-01',
          x1 = '2018-06-06',
          y0 = 0,
          y1 = 1,
          xref = 'x',
          yref = 'paper',
          line = list(color = 'black',
                      width = 0.5)
)

(p <- df %>%
  plot_ly(x = ~date, type="candlestick",
          open = ~open, close = ~close,
          high = ~high, low = ~low) %>% 
  layout(title = "VND Stock",
         annotations = a,
         shapes = l))
## Note: no visible binding for global variable '.I' 
## Note: no visible binding for global variable '.I' 
## Note: no visible binding for global variable '.I' 
## Note: no visible binding for global variable '.I' 
## Note: no visible binding for global variable '.N' 
## Note: no visible binding for global variable '.N'
#Custom color
i <- list(line = list(color = '#FFD700'))
d <- list(line = list(color = '#0000ff'))

(p <- df %>%
  plot_ly(x = ~date, type="candlestick",
          open = ~open, close = ~close,
          high = ~high, low = ~low) %>% 
  layout(title = "VND Stock",
         annotations = a,
         shapes = l))
# Add bollinger bands
df <- VNDS::tq_get('VND','2018-01-01','2018-05-01')
## Note: no visible binding for global variable '.' 
## #VND from 2018-01-01 to 2018-05-01 already cloned
# create Bollinger Bands
bbands <- TTR::BBands(df[,c("high","low","close")])

# join data
df <- cbind(df, data.frame(bbands[,1:3]))

# colors column for increasing and decreasing
for (i in 1:length(df[,1])) {
  if (df$close[i] >= df$open[i]) {
      df$direction[i] = 'Increasing'
  } else {
      df$direction[i] = 'Decreasing'
  }
}

i <- list(line = list(color = '#17BECF'))
d <- list(line = list(color = '#7F7F7F'))

# plot candlestick chart
(p <- df %>%
  plot_ly(x = ~date, type="candlestick",
          open = ~open, close = ~close,
          high = ~high, low = ~low, name = "VND",
          increasing = i, decreasing = d) %>%
  add_lines(x = ~date, y = ~up , name = "B Bands",
            line = list(color = '#ccc', width = 0.5),
            legendgroup = "Bollinger Bands",
            hoverinfo = "none", inherit = F) %>%
  add_lines(x = ~date, y = ~dn, name = "B Bands",
            line = list(color = '#ccc', width = 0.5),
            legendgroup = "Bollinger Bands", inherit = F,
            showlegend = FALSE, hoverinfo = "none") %>%
  add_lines(x = ~date, y = ~mavg, name = "Mv Avg",
            line = list(color = '#E377C2', width = 0.5),
            hoverinfo = "none", inherit = F) %>%
  layout(yaxis = list(title = "Price")))
# plot volume bar chart
(pp <- df %>%
  plot_ly(x=~date, y=~volume, type='bar', name = "VND Volume",
          color = ~direction, colors = c('#17BECF','#7F7F7F')) %>%
  layout(yaxis = list(title = "Volume")))
# create rangeselector buttons
rs <- list(visible = TRUE, x = 0.5, y = -0.055,
           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 <- subplot(p, pp, heights = c(0.7,0.2), nrows=2,
             shareX = TRUE, titleY = TRUE) %>%
  layout(title = paste("VND: ",min(df$date)," - ",max(df$date)),
         xaxis = list(rangeselector = rs),
         legend = list(orientation = 'h', x = 0.5, y = 1,
                       xanchor = 'center', yref = 'paper',
                       font = list(size = 10),
                       bgcolor = 'transparent')))
#Viet ham tong quat
tq_candlechart <- function(symbol, from, to, colour, show.volume = TRUE,...){
 # Add bollinger bands
df <- VNDS::tq_get(symbol,from,to)

# create Bollinger Bands
bbands <- TTR::BBands(df[,c("high","low","close")])

# join data
df <- cbind(df, data.frame(bbands[,1:3]))

# colors column for increasing and decreasing
for (i in 1:length(df[,1])) {
  if (df$close[i] >= df$open[i]) {
      df$direction[i] = 'Increasing'
  } else {
      df$direction[i] = 'Decreasing'
  }
}

i <- list(line = list(color = colour[1]))
d <- list(line = list(color = colour[2]))

# plot candlestick chart
p <- df %>%
  plot_ly(x = ~date, type="candlestick",
          open = ~open, close = ~close,
          high = ~high, low = ~low, name = "VND",
          increasing = i, decreasing = d) %>%
  add_lines(x = ~date, y = ~up , name = "B Bands",
            line = list(color = '#ccc', width = 0.5),
            legendgroup = "Bollinger Bands",
            hoverinfo = "none", inherit = F) %>%
  add_lines(x = ~date, y = ~dn, name = "B Bands",
            line = list(color = '#ccc', width = 0.5),
            legendgroup = "Bollinger Bands", inherit = F,
            showlegend = FALSE, hoverinfo = "none") %>%
  add_lines(x = ~date, y = ~mavg, name = "Mv Avg",
            line = list(color = '#E377C2', width = 0.5),
            hoverinfo = "none", inherit = F) %>%
  layout(yaxis = list(title = "Price"))

# create rangeselector buttons
rs <- list(visible = TRUE, x = 0.5, y = -0.055,
           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')
           ))

if(show.volume){
# plot volume bar chart
pp <- df %>%
  plot_ly(x=~date, y=~volume, type='bar', name = paste0(symbol," Volume"),
          color = ~direction, colors = colour) %>%
  layout(yaxis = list(title = "Volume"))

# subplot with shared x axis
(p <- subplot(p, pp, heights = c(0.7,0.2), nrows=2,
             shareX = TRUE, titleY = TRUE) %>%
  layout(title = paste(symbol,": ",min(df$date)," - ",max(df$date)),
         xaxis = list(rangeselector = rs),
         legend = list(orientation = 'h', x = 0.5, y = 1,
                       xanchor = 'center', yref = 'paper',
                       font = list(size = 10),
                       bgcolor = 'transparent'))) } 
else {
  (p <- p %>%
  layout(title = paste(symbol,": ",min(df$date)," - ",max(df$date)),
         xaxis = list(rangeselector = rs),
         legend = list(orientation = 'h', x = 0.5, y = 1,
                       xanchor = 'center', yref = 'paper',
                       font = list(size = 10),
                       bgcolor = 'transparent')))}
}

tq_candlechart('VND','2018-01-01','2018-05-01',colour = c('red','darkred'))
## #VND from 2018-01-01 to 2018-05-01 already cloned