{talib}: Candlestick Pattern Recognition in R

R-bloggers 2025-11-16

[This article was first published on R-posts.com, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

{talib} is a newR-package for Technical Analysis (TA) and CandlestickPattern Recognition (Yeah, the patterns traders bet their lifesavingson….). In this post I will show basic example on how {talib} works, and how itcompares performance-wise with {TTR}.

Basic example

In this example I will identify all ‘Harami’ patterns, and calculatethe 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 amatrix of the same length of the input. The number ofidentified 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 thesame way

cat(  "identified bullish patterns:",  sum(x[, 1] == 1, na.rm = TRUE))#> identified bullish patterns: 20cat(  "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 thechart.

{  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 “bringsto the table”. Other than Candlestick Patterns and interactive charts,it brings speed and efficiency.

To demonstrate the difference in speed, I will create a univariateprice series with 1 million entries.

set.seed(1903)x <- runif(n = 1e6, min = 100, max = 150)

The univariate series x will be passed into theBollinger 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, andmore memory efficient, than {TTR}.

{talib} is stillunder development, and will most likely not be submitted to CRAN beforenext 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


{talib}: Candlestick Pattern Recognition in R was first posted on November 16, 2025 at 8:06 pm.
To leave a comment for the author, please follow the link and comment on their blog: R-posts.com.

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.
Continue reading: {talib}: Candlestick Pattern Recognition in R