chart() creates interactive candlestick or OHLC bar charts from financial
price data. It initializes the charting environment so that subsequent calls
to indicator() can attach technical indicators as subcharts.
Calling chart() without any arguments resets the charting environment,
clearing all stored chart state (main chart, subcharts, and data).
See vignette(topic = "charting", package = "talib") for a comprehensive
guide on building multi-panel technical analysis charts.
Arguments
- x
An OHLC-V data.frame (or object coercible to one) with columns named
open,high,low,close, and optionallyvolume. Column names are case-sensitive.- type
A character string, either
"candlestick"(default) or"ohlc". Candlestick charts use filled/hollow bodies with wicks; OHLC charts use vertical bars with horizontal open/close ticks.- idx
An optional vector with the same length as the number of rows in
x. Replaces the default x-axis labels (row names or integer index). Useful for custom date formatting or non-standard index types.- title
An optional character string for the chart title. If omitted, the title is inferred from the variable name passed to
x.- ...
Additional parameters passed to the backend chart constructor (e.g.,
plotly::plot_ly()).
Value
A chart object whose class depends on the active backend:
"plotly"backend: aplotlyobject (interactive HTML widget)."ggplot2"backend: aggobject (static plot).
When called without arguments, returns NULL invisibly.
Details
chart() acts as the entry point for the package's charting system. It
stores the OHLC data and the main price chart internally so that subsequent
indicator() calls can attach panels below the price chart without
requiring the data to be passed again.
The chart title is automatically inferred from the name of the object passed
to x (e.g., chart(BTC) produces the title "BTC"). The title also
displays the number of observations and, when available, the date range.
Two rendering backends are supported:
"plotly"(default)Produces interactive HTML charts with hover tooltips, pan/zoom, and built-in drawing tools (lines, rectangles). Requires the plotly package.
"ggplot2"Produces static charts suitable for reports and publications. Requires the ggplot2 package.
Options
The following options() control chart appearance and behavior:
talib.chart.backend[character]"plotly"by default. Set to"ggplot2"for static charts.talib.chart.slider[logical]FALSEby default. IfTRUE, a range slider is added below the x-axis for interactive zooming (plotly backend only).talib.chart.slider.size[numeric]0.05by default. Controls the height of the range slider as a fraction of the total chart height.talib.chart.legend[logical]TRUEby default. IfFALSE, legends are hidden on all panels.talib.chart.scale[numeric]1by default. A scaling factor applied to all font sizes. Values greater than 1 increase font size.talib.chart.main[numeric]0.7by default. The fraction of total chart height allocated to the main price panel when subcharts are present.
Colors are controlled via ggplot2::set_theme(). See ggplot2::set_theme() for available
themes and color customization.
State and concurrency
The chart() + indicator() pair follows the same active-target
model as base R's plot() / lines(). When chart() is called it
stashes a per-pipeline state object in its caller's evaluation frame;
subsequent indicator() calls retrieve the state by walking up the
call stack.
The practical consequences:
chart()and the subsequentindicator()calls must live in the same enclosing frame - the same REPL session, the same function body, the samerenderPlot()/renderPlotly()block, the samelocal({...})expression, the sametestthat::test_that({...})block, etc. Splitting them across unrelated helpers is not supported.Two unrelated function bodies (or two parallel
renderPlot()callbacks in a Shiny app, or twofuture::future()blocks) each get their own frame, so their chart states are isolated by construction - withouttalibtaking any dependency on Shiny, promises, or futures.Calling
chart()with no arguments clears the state in the caller's frame, mirroring a freshplot()call on a new device.
See also
indicator() to attach technical indicators, ggplot2::set_theme() to
customize chart colors.
Other Charting:
chart_themes,
indicator(),
set_theme()
Examples
## charting OHLC data with {talib}
data(BTC, package = "talib")
## candlestick chart (default)
talib::chart(BTC)
## OHLC bar chart
talib::chart(BTC, type = "ohlc")
## chart with a custom title
talib::chart(BTC, title = "Bitcoin / USD")
## reset the charting environment
talib::chart()
