Search code examples
rplotlyquantmod

Plot ly Candlestick charts for stocks not from USA


i've been trying R because it seems easy enough to create charts and do quick math analysis but i've run into a problem, the thing is i want to use plotly to graph my data but i cant make it work with symbols from stocks outside of US, here is the problem, im trying the first example from this page https://plot.ly/r/candlestick-charts/ and when i try to add the symbol BMV:BIMBOA instead of AAPL it will send me an error because of the ":"

Here is what i've tried

library(plotly)
library(quantmod)

Symbolname <- "BMV:BIMBOA"

getSymbols(Symbolname, src = "google")
df <- data.frame(Date=index(Symbolname),coredata(Symbolname))
df <- tail(df, 30)

p <- df %>%
  plot_ly(x = ~Date, type="candlestick",
          open = ~Symbolname.Open, close = ~Symbolname.Close,
          high = ~Symbolname.High, low = ~Symbolname.Low) %>%
  layout(title = "Basic Candlestick Chart")

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="finance/candlestick-basic")
chart_link

and its not working,i get the error symbolname.open not found, as a side note if i use BMV:BIMBOA instead of the symbolname var i get the error "BMV" is not found. i have tried using only BIMBOA but since the row in the xts object is called "BMV:BIMBOA.Open" its not working.

Is there a workaround for this or any other way to read this data and work with plotly to graph this?

Also tried this:

BMVBIMBOA <- `BMV:BIMBOA`

getSymbols("BMV:BIMBOA", src = "google")
df <- data.frame(Date=index(BMVBIMBOA),coredata(BMVBIMBOA))
df <- tail(df, 30)

p <- df %>%
  plot_ly(x = ~Date, type="candlestick",
          open = ~BMVBIMBOA.Open, close = ~BMVBIMBOA.Close,
          high = ~BMVBIMBOA.High, low = ~BMVBIMBOA.Low) %>%
  layout(title = "Basic Candlestick Chart")

The error is on the following 2 lines

          open = ~BMVBIMBOA.Open, close = ~BMVBIMBOA.Close,
          high = ~BMVBIMBOA.High, low = ~BMVBIMBOA.Low) %>%
  layout(title = "Basic Candlestick Chart")

Solution

  • Several problems here:

    1. The default mode of getSymbols is through side-effect, often (generally?) frowned upon. This is complicated by the fact that R does not generally like colons in variable names (though it can work just fine).

    2. Your use of index and coredata (both within the zoo package, I'm assuming) is incorrect. Those two functions want an object, but you are providing a length-1 character vector ("BMV:BIMBOA").

    3. (I think) The column names in the resulting data are specific to the symbol, so Symbolname.Open is not found; replace them with the actual names.

    Let's bypass the side-effect thing and hopefully get closer to what you want.

    library(zoo)
    library(plotly)
    library(quantmod)
    Symbolname <- "BMV:BIMBOA"
    mydata <- getSymbols(Symbolname, src = "google", auto.assign = FALSE)
    df <- data.frame(Date=index(mydata),coredata(mydata))
    df <- tail(df, 30)
    p <- df %>%
      plot_ly(x = ~Date, type="candlestick",
              open = ~BMV.BIMBOA.Open, close = ~BMV.BIMBOA.Close,
              high = ~BMV.BIMBOA.High, low = ~BMV.BIMBOA.Low) %>%
      layout(title = "Basic Candlestick Chart")
    

    BMV:BIMBOA plot