Skip to contents

Price Charts

Candlestick Chart

talib::chart(
  x = talib::NVDA,
  type = "candlestick"
)

OHLC Chart

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

By default the chart-function inherits name of the argument x as the title. This can be modified via the title-argument.

talib::chart(
  x = talib::BTC,
  type = "ohlc",
  title = "SPDR S&P 500 ETF Trust"
)

Charting indicators

The various indicators can be added to the price chart—or charted seperately—with the indicator-function. The indicator-function will attach itself to the last chart.

talib::indicator(
    FUN = talib::MACD
)
#> Warning: Ignoring 33 observations

If you have already called the chart-function, and want to plot the indicator seperately you will have to clear the existing chart—this is done by calling talib::chart() without any arguments. The indicator can now be re-charted using the indicator-function.

{
  ## clear the chart
  ## by calling talib::chart()
  ## without any arguments
  talib::chart()

  ## rechart the indicator
  ## directly
  talib::indicator(
    FUN = talib::MACD,
    data = talib::BTC
  )
}
#> Warning: Ignoring 33 observations

Notice here that you have to pass a data argument via ... which gets passed downstream to MACD.

Modifying charts

The charts can be globally modified via options use ?talib::chart to see the full range of modifications available for the charting. Below is an example on how to modify the color scheme of the charts.

## modify options
## to create charts in
## light and color-deficiency mode
options(
  talib.chart.dark = FALSE,
  talib.chart.deficieny = TRUE
)

{
  ## price chart
  ## with defaults
  talib::chart(
    talib::SPY
  )

  ## add RSI indicator
  talib::indicator(
    FUN = talib::RSI
  )

  ## add Bollinger Bands
  talib::indicator(
    FUN = talib::BBANDS
  )
  
}

Modifying x-axis labels

Internally chart() and indicator() are working on the range of the the data—ie. 1:nrow(data)—to easy the process of subsetting and for the efficiency. The x-axis can be labelled with dates from the data via the idx-argument.

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

Charting Subsets

The downstream indicator functions uses model.frame as the main function. This is useful in cases where the interest lies in indicators across a specific range without wanting to subset the data itself, or if you want to change indicators over time.

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

  talib::indicator(
    talib::BBANDS,
    subset = 1:nrow(talib::BTC) %in% c(50:100)
  )

  talib::indicator(
    talib::ACCBANDS,
    subset = 1:nrow(talib::BTC) %in% c(101:151)
  )
}