R: Plotly 'candlestick' objects don't have these attributes

Viewed 8

I have the following OHLC plot using plotly:

 pp = dff %>% 
  plot_ly(x = ~time_terminal, type="candlestick",
          open = ~open.ha, 
          close = ~close.ha,
          high = ~high, low = ~low)

enter image description here

When I try to add some points series to it, I get:

>  pp %>% add_trace(y = ~close, name = 'close', mode = 'points') 
Warning message:
'candlestick' objects don't have these attributes: 'y', 'mode'
Valid attributes include:
'close', 'closesrc', 'customdata', 'customdatasrc', 'decreasing', 'high', 'highsrc', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'increasing', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'low', 'lowsrc', 'meta', 'metasrc', 'name', 'opacity', 'open', 'opensrc', 'selectedpoints', 'showlegend', 'stream', 'text', 'textsrc', 'transforms', 'type', 'uid', 'uirevision', 'visible', 'whiskerwidth', 'x', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'yaxis', 'yhoverformat', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Any help welcome

dff <- structure(list(time_terminal = structure(c(1663973640, 1663973700, 
1663973760, 1663973820, 1663973880, 1663973940), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), open.ha = c(12310.0220066959, 12308.473503348, 
12306.386751674, 12303.655875837, 12306.1654379185, 12308.9202189592
), high = c(12311.3, 12310.3, 12303.8, 12316.8, 12321.3, 12312.8
), low = c(12301.3, 12300.3, 12294.8, 12300.3, 12302.8, 12300.8
), close.ha = c(12306.925, 12304.3, 12300.925, 12308.675, 12311.675, 
12305.425)), row.names = 825:830, class = "data.frame")
1 Answers

Try using inherit=FALSE, which will require you to reset the data and x.. Something like this:

pp %>%
  add_trace(
    data=dff,
    x=~time_terminal,
    y=~close.ha,
    inherit=F,
    name='close',
    mode='markers and lines',
    type='scatter'
)

Notice that I use y=~close.ha, as there is no close column in dff

Output: candlestick_with_lines

Related