Search code examples
rr-plotly

How to plot with R


I would like to do candlestick plot with R and also add the donchian channel as indicator line. In the example below, the dataframe is not being extracted. not sure where i am making a error.

library(reprex)
library(quantmod)
#> Loading required package: xts
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: TTR
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
library(PerformanceAnalytics)
#> 
#> Attaching package: 'PerformanceAnalytics'
#> The following object is masked from 'package:graphics':
#> 
#>     legend
library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
s <- get(getSymbols('CWK'))["2019::"]
#> 'getSymbols' currently uses auto.assign=TRUE by default, but will
#> use auto.assign=FALSE in 0.5-0. You will still be able to use
#> 'loadSymbols' to automatically load data. getOption("getSymbols.env")
#> and getOption("getSymbols.auto.assign") will still be checked for
#> alternate defaults.
#> 
#> This message is shown once per session and may be disabled by setting 
#> options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
s$sma20 <- SMA(Cl(s) , 20)
head(s,3)
#>            CWK.Open CWK.High CWK.Low CWK.Close CWK.Volume CWK.Adjusted sma20
#> 2019-01-02    14.29    14.75   13.82     14.53     911000        14.53    NA
#> 2019-01-03    14.51    14.70   13.97     14.19     627100        14.19    NA
#> 2019-01-04    14.25    14.35   13.60     14.25     697100        14.25    NA
s$dc <- DonchianChannel(s[,c("CWK.High","CWK.Low")],400,TRUE)
head(s,3)
#>            CWK.Open CWK.High CWK.Low CWK.Close CWK.Volume CWK.Adjusted sma20
#> 2019-01-02    14.29    14.75   13.82     14.53     911000        14.53    NA
#> 2019-01-03    14.51    14.70   13.97     14.19     627100        14.19    NA
#> 2019-01-04    14.25    14.35   13.60     14.25     697100        14.25    NA
#>            high mid dc
#> 2019-01-02   NA  NA NA
#> 2019-01-03   NA  NA NA
#> 2019-01-04   NA  NA NA

fig <- s %>% plot_ly(x = ~Date, type="candlestick",
          open = ~CWK.Open, close = ~CWK.Close,
          high = ~CWK.High, low = ~CWK.Low) 
#> Error: First argument, `data`, must be a data frame or shared data.
fig <- fig %>% layout(title = "Basic Candlestick Chart")
#> Error in layout(., title = "Basic Candlestick Chart"): object 'fig' not found

fig
#> Error in eval(expr, envir, enclos): object 'fig' not found

Solution

  • plot_ly want a data frame, but

    > class(s)
    [1] "xts" "zoo"
    

    so it says Error: First argument, data, must be a data frame or shared data.
    You need to convert class of s, below is an example;

    fig <- tibble::as_tibble(s, rownames = "Date") %>% 
      plot_ly(x = ~Date, type="candlestick",
             open = ~CWK.Open, close = ~CWK.Close,
             high = ~CWK.High, low = ~CWK.Low)