Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

{talib} is a new
R package built on TA-Lib, which is now
available on CRAN. The R-package is targeted at individuals
and, perhaps, institutions who, in some form or the other, interacts
with the financial markets using technical analysis.
The library is built with minimal dependencies for long-term
stability and freedom in mind. All functions are built around
data.frame– and matrix-classes which are
portable to all other data-containers with minimal effort.
Everything in the library is built ‘bottom-up’ for maximum speed and
memory efficiency. Each indicator interacts directly with R’s C API via
.Call().
In this blog post I will give a brief introduction to the interface and the most important aspects of the package. The library also includes static and interactive financial charts, which will be covered in a different post.
A quick introduction to the interface
In this section I will briefly introduce the most important aspects
of the function calls, formals and how <NA> are
handled. Below is a simple starting point; calculating the Bollinger
Bands for Bitcoin:
tail(
talib::bollinger_bands(
talib::BTC
)
)
#> UpperBand MiddleBand LowerBand
#> 2024-12-26 01:00:00 100487.38 96698.61 92909.83
#> 2024-12-27 01:00:00 100670.65 96512.96 92355.27
#> 2024-12-28 01:00:00 100632.13 96581.91 92531.69
#> 2024-12-29 01:00:00 99628.77 95576.60 91524.43
#> 2024-12-30 01:00:00 96403.53 94231.31 92059.09
#> 2024-12-31 01:00:00 95441.13 93774.23 92107.34
In itself a simple call. Below are the formals:
str(formals(talib::bollinger_bands)) #> Dotted pair list of 8 #> $ x : symbol #> $ cols : symbol #> $ ma : language SMA(n = 5) #> $ sd : num 2 #> $ sd_down : symbol #> $ sd_up : symbol #> $ na.bridge: logi FALSE #> $ ... : symbol
All functions share the same signature x,
cols, na.bridge and ..., while
everything else is indicator-specific. The cols-argument is
missing, but has a default value hard-coded against the upstream TA-Lib – this is also true
for the remaining indicators. The cols-argument accepts a
one-sided formula as follows:
tail(
talib::bollinger_bands(
talib::BTC,
cols = ~close
)
)
#> UpperBand MiddleBand LowerBand
#> 2024-12-26 01:00:00 100487.38 96698.61 92909.83
#> 2024-12-27 01:00:00 100670.65 96512.96 92355.27
#> 2024-12-28 01:00:00 100632.13 96581.91 92531.69
#> 2024-12-29 01:00:00 99628.77 95576.60 91524.43
#> 2024-12-30 01:00:00 96403.53 94231.31 92059.09
#> 2024-12-31 01:00:00 95441.13 93774.23 92107.34
In this case the resulting data.frame is the same as
above, as the default column for which the bands are calculated is the
closing price of the asset.
All indicators are wrapped by model.frame() and its
functionality can be accessed via ... as follows:
tail(
talib::bollinger_bands(
talib::BTC,
cols = ~close,
subset = 1:nrow(talib::BTC) %in% 1:100
)
)
#> UpperBand MiddleBand LowerBand
#> 2024-04-04 02:00:00 72605.20 68193.82 63782.44
#> 2024-04-05 02:00:00 70646.91 67502.88 64358.84
#> 2024-04-06 02:00:00 70087.36 67344.94 64602.52
#> 2024-04-07 02:00:00 70475.41 68122.29 65769.16
#> 2024-04-08 02:00:00 71820.87 69249.88 66678.89
#> 2024-04-09 02:00:00 71848.20 69372.65 66897.09
Here we only calculate the indicator on a subset of the
BTC. While this may seem like a redundant ‘wow’-feature at
first glance, its primary justification is in the charting interface
where only parts of an indicator is of visual interest.
The <NA>-handling in {talib} works a
bit differently than na.rm. Before I demonstrate this, we
add some missing values randomly to BTC
BTC <- talib::BTC BTC$close[sample(1:100, size = 20)] <- NA
The naive approach is to calculate the indicator directly:
tail(
talib::bollinger_bands(
BTC
)
)
#> UpperBand MiddleBand LowerBand
#> 2024-12-26 01:00:00 NA NA NA
#> 2024-12-27 01:00:00 NA NA NA
#> 2024-12-28 01:00:00 NA NA NA
#> 2024-12-29 01:00:00 NA NA NA
#> 2024-12-30 01:00:00 NA NA NA
#> 2024-12-31 01:00:00 NA NA NA
Which returns <NA> for all values. This is the
default behaviour. The function faithfully returns the full object with
the same number of rows – filled with
<NA>-values.
To avoid this you can set na.bridge = TRUE as
follows:
tail(
object <- talib::bollinger_bands(
BTC,
na.bridge = TRUE
)
)
#> UpperBand MiddleBand LowerBand
#> 2024-12-26 01:00:00 100487.38 96698.61 92909.83
#> 2024-12-27 01:00:00 100670.65 96512.96 92355.27
#> 2024-12-28 01:00:00 100632.13 96581.91 92531.69
#> 2024-12-29 01:00:00 99628.77 95576.60 91524.43
#> 2024-12-30 01:00:00 96403.53 94231.31 92059.09
#> 2024-12-31 01:00:00 95441.13 93774.23 92107.34
Again, with the same number of rows as BTC. This
behaviour is true for all functions: N-rows in,
N-rows out. What na.bridge is doing under the
hood, is to extract all <NA>-values, calculate the
indicator and then re-adds them in their original position.
Installation
{talib} is finally on CRAN, and can be installed as
follows:
install.packages("talib")
It can also be built from source with additional
CMake-flags:
install.packages( "talib", type = "source", configure.args = "-O3 -march=native" )
Contributing and submitting bug-reports
{talib} is still in its early stage so contributions,
even if small, bug-reports, suggestions and critiques are gratefully
accepted.
Visit the repository here: https://github.com/serkor1/ta-lib-R.
Created on 2026-04-24 with reprex v2.1.1
{talib}: Technical Analysis using R was first posted on April 27, 2026 at 1:00 am.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.