Financial Charts

Serkan Korkmaz

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

Getting started

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.

talib::chart(
  x = talib::NVDA
)

The chart title defaults to the name of the object passed to x. Override it with the title argument.

talib::chart(
  x = talib::BTC,
  title = "Bitcoin (USDC)"
)

Chart types

Two chart types are supported via the type argument: "candlestick" (default) and "ohlc".

talib::chart(
  x = talib::BTC,
  type = "ohlc"
)

Adding indicators

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

Combining indicators on one panel

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:

{
  talib::chart(talib::BTC)
  talib::indicator(
    talib::RSI(n = 14),
    talib::MACD()
  )
}

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 like indicator(RSI(n = 14)) also works and is equivalent to the single-indicator form.

Standalone indicators

If no chart() has been called, indicator() can plot an indicator on its own—but you must supply the data argument explicitly.

{
  ## clear any existing chart
  talib::chart()

  ## plot MACD by itself
  talib::indicator(
    FUN  = talib::MACD,
    data = talib::BTC
  )
}

Clearing the chart state

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.

{
  ## first chart
  talib::chart(talib::NVDA)
  talib::indicator(talib::RSI)
}
{
  ## reset and start fresh
  talib::chart()

  talib::chart(talib::BTC)
  talib::indicator(talib::MACD)
}

X-axis labels

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

{
  talib::chart(
    x   = talib::BTC,
    idx = rownames(talib::BTC)
  )
}