The {talib} package ships with a charting system built
on two backends: plotly (interactive, default) and
ggplot2 (static). Charts are composed in two steps:
create a price chart with chart(), then layer on indicators
with indicator().
chart() takes any OHLCV object coercible to a
data.frame. The package includes three built-in
datasets—BTC, NVDA, and SPY—used
throughout this vignette.
The chart title defaults to the name of the object passed to
x. Override it with the title argument.
Two chart types are supported via the type argument:
"candlestick" (default) and "ohlc".
Use indicator() to attach technical indicators to the
most recent chart(). Indicators that overlay on price
(e.g., Bollinger Bands, moving averages) are drawn on the main panel.
Indicators with their own scale (e.g., RSI, MACD) are drawn in
sub-panels below.
{
talib::chart(talib::BTC)
talib::indicator(talib::SMA, n = 7)
talib::indicator(talib::SMA, n = 14)
talib::indicator(talib::RSI)
}Multiple sub-panel indicators stack vertically. The main chart
occupies 70% of the height by default (controlled by
talib.chart.main).
{
talib::chart(talib::BTC)
talib::indicator(talib::BBANDS)
talib::indicator(talib::MACD)
talib::indicator(talib::trading_volume)
}By default, each sub-panel indicator (RSI, MACD, etc.) gets its own
panel. To merge multiple indicators onto the same panel, pass them as
calls to indicator():
{
talib::chart(talib::BTC)
talib::indicator(
talib::RSI(n = 10),
talib::RSI(n = 14),
talib::RSI(n = 21)
)
}Each indicator keeps its own arguments and receives a distinct color from the active theme’s color palette. This works with any number of indicators—and they don’t have to be the same type:
Combined panels and regular panels can be freely mixed in the same chart:
{
talib::chart(talib::BTC)
talib::indicator(talib::BBANDS)
talib::indicator(
talib::RSI(n = 10),
talib::RSI(n = 14),
talib::RSI(n = 21)
)
talib::indicator(talib::MACD)
}Note: the syntax matters.
indicator(RSI, n = 14)passes a function and arguments separately (single indicator).indicator(RSI(n = 14), RSI(n = 21))passes calls (combined panel). A single call likeindicator(RSI(n = 14))also works and is equivalent to the single-indicator form.
If no chart() has been called, indicator()
can plot an indicator on its own—but you must supply the
data argument explicitly.
Calling chart() without arguments resets the internal
charting environment. This is useful when you want to start a fresh
chart or plot a standalone indicator after a previous
chart() call.
Internally, the charting system uses integer positions
(1:nrow(data)) on the x-axis for efficient alignment
between the main chart and sub-panels. By default, the axis shows these
integer positions or the row names of the data.
Pass a vector to the idx argument to display custom
labels (e.g., dates).