Skip to content

Latest commit

 

History

History
72 lines (50 loc) · 5.3 KB

README.md

File metadata and controls

72 lines (50 loc) · 5.3 KB

fin-primitives: your trading code probably can't agree with itself

English | 简体中文 | 日本語 | 한국어

crates.io docs.rs CI MIT

Most trading code computes the same indicator more than one way and never notices. In a 73.5K-star stock tool, the screener, the alerts and the report each had their own RSI, and on the same 200-bar series one said oversold (26.36) while the others said neutral (50.19) (the merged fix moved all three onto this crate). fin-primitives is one implementation of 700+ indicators, matched to TA-Lib to 2e-11 or better on 22 shared indicators, with exact decimal prices that refuse bad values. Python teams already run it without writing a line of Rust: that 73.5K-star Python project now ships it as a runtime dependency.

  • Rejects bad prices, like a negative one.
  • Rolls trades into candles.
  • Runs an order book.
  • Prices options with Greeks.
  • Checks drawdown and equity limits.

Why it exists. An indicator computed three ways is three indicators. The fix is one implementation that every part of a codebase (and every language it is written in) calls.

Why this instead of TA-Lib or pandas-ta. TA-Lib's numbers are the reference, but it needs a C library installed before pip install works. pandas-ta on PyPI today is only the 0.4.x betas, which require Python 3.12 or later and pin numba==0.61.2 (checked Nov 20 2026); 0.3.14b0, the version most requirements.txt files still name, is gone. fin-primitives matches TA-Lib to 2e-11 or better on 22 shared indicators, ships abi3 wheels for Python 3.9 and up with no dependencies of its own (Linux, macOS, Windows, musl, WebAssembly), and from fin_primitives import ta keeps pandas-ta's function names.

Why today. pip install fin-primitives or cargo add fin-primitives, and indicator-diff tells you in one command whether the RSI you already have is Wilder's.

Who it is for: trading bots, screeners and backtests in Python or Rust whose indicators feed a decision, and anyone porting one between the two.

Trade-offs, on purpose: prices are exact decimals that reject bad values instead of propagating NaN; one documented formula per indicator (Wilder's where Wilder defined it) rather than a switch for every variant.

Install

cargo add fin-primitives rust_decimal rust_decimal_macros

Use

use fin_primitives::signals::{indicators::Rsi, BarInput, Signal, SignalValue};
use fin_primitives::types::Price;
use rust_decimal_macros::dec;

fn main() -> Result<(), fin_primitives::FinError> {
    assert!(Price::new(dec!(-1)).is_err()); // bad values never get in

    let mut rsi = Rsi::new("rsi14", 14)?;
    let closes = [dec!(44.34), dec!(44.09), dec!(44.15), dec!(43.61), dec!(44.33), dec!(44.83), dec!(45.10), dec!(45.42),
                  dec!(45.84), dec!(46.08), dec!(45.89), dec!(46.03), dec!(45.61), dec!(46.28), dec!(46.28), dec!(46.00)];
    for close in closes {
        if let SignalValue::Scalar(v) = rsi.update(&BarInput::from_close(close))? {
            println!("RSI(14) = {}", v.round_dp(2)); // 70.46, then 66.25
        }
    }
    Ok(())
}

Which RSI do you have?

Most indicator bugs are not arithmetic bugs. They are choices nobody wrote down: a simple average where Wilder used his smoothing, ewm(span=14) where Wilder's alpha is 1/14, a seed on the wrong bar, a warm-up filled with 50. indicator-diff runs one price series through every RSI, ATR and MACD installed (TA-Lib, pandas-ta, ta, the hand-rolled versions that get copied between projects, and yours) and names the formula each one actually computes, with the size of the gap and how many bars change verdict.

pip install fin-primitives pandas
indicator-diff --demo                       # the 500-bar standard series
indicator-diff prices.csv --load bot/ta.py --expr "rsi=compute_rsi(close, 14)"   # exits 1 if it is not Wilder's

We ran the indicator code of 20 popular open-source trading repos through it (plus two that inherit one library's numbers). All 20 differ from the textbook value somewhere, and 13 use a formula that never converges to Wilder's (three of them on purpose, following the Tongdaxin platform). The most common: Cutler's simple-average RSI, 10.2 points off Wilder's on the last bar of the standard series and on the other side of 30 or 70 on 92 bars, and a simple-average ATR, 5.4 % high and never converging. pandas-ta 0.4.71b0 gives different numbers depending on whether TA-Lib is installed. We had the same bug: until 4.0 our own Atr::new was a simple average. It is Wilder's now, everywhere in the crate.

Docs · crates.io · Site

More: docs/DETAILS.md · Adding it to another project: docs/INTEGRATION.md

MIT license. Does not place orders. Not financial advice.