Skip to contents

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.

Usage

chart(x, type = "candlestick", idx = NULL, title, ...)

Arguments

x

An OHLC-V data.frame (or object coercible to one) with columns named open, high, low, close, and optionally volume. 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: a plotly object (interactive HTML widget).

  • "ggplot2" backend: a gg object (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]

FALSE by default. If TRUE, a range slider is added below the x-axis for interactive zooming (plotly backend only).

talib.chart.slider.size [numeric]

0.05 by default. Controls the height of the range slider as a fraction of the total chart height.

talib.chart.legend [logical]

TRUE by default. If FALSE, legends are hidden on all panels.

talib.chart.scale [numeric]

1 by default. A scaling factor applied to all font sizes. Values greater than 1 increase font size.

talib.chart.main [numeric]

0.7 by 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.

See also

indicator() to attach technical indicators, ggplot2::set_theme() to customize chart colors.

Other Charting: chart_themes, indicator(), set_theme()

Author

Serkan Korkmaz

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()