{talib} is a new
R-package for Technical Analysis (TA) and Candlestick
Pattern Recognition (Yeah, the patterns traders bet their lifesavings
on….). In this post I will show basic example on how {talib} works, and how it
compares performance-wise with {TTR}.
Basic example
In this example I will identify all ‘Harami’ patterns, and calculate the Bollinger Bands of the SPDR S&P 500 ETF (SPY).
Identify Harami patterns
x <- talib::harami(
talib::SPY
)
talib::harami() is a S3 function and returns a
matrix of the same length of the input. The number of
identified patterns can counted as non-zero entires.
cat(
"identified patterns:",
sum(x[, 1] != 0, na.rm = TRUE)
)
#> identified patterns: 35
The Harami pattern can be bullish (1) or bearish (-1) and counted the same way
cat(
"identified bullish patterns:",
sum(x[, 1] == 1, na.rm = TRUE)
)
#> identified bullish patterns: 20
cat(
"identified bearish patterns:",
sum(x[, 1] == -1, na.rm = TRUE)
)
#> identified bearish patterns: 15
Charting
The Harami pattern can be plotted using talib::chart()
with talib::bollinger_bands() to add Bollinger Bands to the
chart.
{
talib::chart(talib::SPY)
talib::indicator(talib::harami)
talib::indicator(talib::bollinger_bands)
}
Benchmarks
An often asked question about {talib} in relation to {TTR}, is what it “brings to the table”. Other than Candlestick Patterns and interactive charts, it brings speed and efficiency.
To demonstrate the difference in speed, I will create a univariate price series with 1 million entries.
set.seed(1903)
x <- runif(n = 1e6, min = 100, max = 150)
The univariate series x will be passed into the
Bollinger Bands from each package:
bench::mark(
talib::bollinger_bands(x),
TTR::BBands(x),
min_iterations = 10,
check = FALSE
)[, c(1, 2, 3, 5)]
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 2 × 4
#> expression min median mem_alloc
#> <bch:expr> <bch:tm> <bch:tm> <bch:byt>
#> 1 talib::bollinger_bands(x) 6.65ms 9.07ms 22.9MB
#> 2 TTR::BBands(x) 65.12ms 72.42ms 139.3MB
In this benchmark {talib} is faster, and more memory efficient, than {TTR}.
{talib} is still
under development, and will most likely not be submitted to CRAN before
next year. Until then it can be installed from Github:
pak::pak("serkor1/ta-lib-R")
Feel free to stop by the repository here: https://github.com/serkor1/ta-lib-R.
Created on 2025-11-16 with reprex v2.1.1