Skip to content

About

Ruby FFI wrapper for TA-Lib (Technical Analysis Library)

Resources

Stars

9 stars

Watchers

2 watching

Forks

Repository files navigation

TALibFFI

Tests Gem version

ta_lib_ffi exposes the technical indicators in your installed TA-Lib C library as Ruby methods. A three-period simple moving average returns its first value at the third price.

require "ta_lib_ffi"

close = [10, 11, 12, 13, 14]
TALibFFI.sma(close, time_period: 3)
# => [11.0, 12.0, 13.0]

Installation

You need Ruby 3.1 or later and the TA-Lib C shared library 0.6.4 or later. If you already use C library 0.6.4, you can keep it when upgrading the gem to 0.4.0.

  • macOS — Run brew install ta-lib.
  • Debian or Ubuntu — Download the .deb matching dpkg --print-architecture. For example, on amd64, install the downloaded file with sudo dpkg -i ./ta-lib_0.8.3_amd64.deb. Debian packages
  • Windows x64 — Run the TA-Lib 0.8.3 x64 installer.

For a custom installation, set TA_LIB_PATH to the full path of the shared library (.so, .dylib or .dll) before requiring the gem. TA-Lib installation guide

Add the gem to your application with bundle add ta_lib_ffi, or install it directly with gem install ta_lib_ffi.

Inputs and results

Pass price fields together in the order the indicator expects. For Average True Range (ATR), pass high, low and close as one argument. All input series must be nonempty and have the same length.

high  = [11, 13, 12, 14, 15]
low   = [9, 10, 9, 11, 12]
close = [10, 12, 10, 13, 14]

TALibFFI.atr([high, low, close], time_period: 2)
# => [3.0, 3.5, 3.25]

Single-output indicators return an array. Indicators with several outputs return a hash of arrays keyed by output name. Each array starts at the first input position for which the indicator has enough history.

TALibFFI.minmax([12, 10, 14, 11, 13], time_period: 3)
# => { min: [10.0, 10.0, 11.0], max: [14.0, 14.0, 14.0] }

C library compatibility

The loaded C library determines which methods are available and their default parameters. CI tests Ruby 3.1 through the latest stable Ruby 4.x across Linux, macOS and Windows with TA-Lib C 0.6.4 and 0.8.3.

TA-Lib C Indicators Default BBANDS period ALMA available
0.6.4 161 5 No
0.7.1 161 5 No
0.8.1 201 20 No
0.8.3 237 20 Yes

Pass time_period: explicitly to use the same BBANDS window across versions. Use respond_to? when calling an indicator that some installations may lack, such as the Arnaud Legoux Moving Average (ALMA), or inspect TALibFFI.group_table and TALibFFI.function_table(group) to list loaded indicators.

if TALibFFI.respond_to?(:alma)
  prices = (1..8).map(&:to_f)
  TALibFFI.alma(prices, time_period: 4, sigma: 4.0, offset: 0.75).map { |value| value.round(4) }
  # => [2.8847, 3.8847, 4.8847, 5.8847, 6.8847]
end

When upgrading to gem 0.4.0, check your keyword arguments. A misspelling such as timeperiod: now raises TALibFFI::TALibError and the error lists the accepted keywords. Changelog

API documentation

The gem ships method signatures generated with TA-Lib C 0.8.3. Online API reference

To build the reference for your installed C library, run bundle install and bundle exec rake yard from a checkout, then open doc/TALibFFI.html. The task also updates the generated method comments in lib/ta_lib_ffi.rb. Use TA-Lib C 0.8.3 when refreshing the signatures for this release.

Development

From a checkout, run bundle install followed by bundle exec rake to run RSpec and RuboCop. To run just the C comparison tests, use bundle exec rspec spec/ta_lib_ffi_c_reference_spec.rb. They call each indicator through Ruby and directly through the installed C library with the same inputs, then compare output values, types, lengths and keys.

Bug reports and pull requests are welcome. GitHub repository

License

MIT License

About

Ruby FFI wrapper for TA-Lib (Technical Analysis Library)

Resources

Stars

9 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages