bartons
Financial and technical-analysis expressions for polars, implemented in Rust as a native plugin (PyO3 + maturin).
Each indicator is a factory returning a pl.Expr, so it composes with the rest of
polars — inside select, with_columns, over, lazy frames, and so on.
Install
Requires Python 3.11+ and polars>=1.28,<1.44. Wheels are cp311-abi3, so one
wheel per platform covers every Python from 3.11 up.
pip install bartons
Usage
import polars as pl
from bartons.indicators import ATR, CCI, EMA, MACD, RSI, SMA, TYPPRICE
from bartons.samples import sample_prices
prices = sample_prices("daily")
prices.select("date", "close", EMA(20), RSI(14), ATR(14)).tail(3)
┌────────────┬────────────┬────────────┬───────────┬──────────┐
│ date ┆ close ┆ ema ┆ rsi ┆ atr │
╞════════════╪════════════╪════════════╪═══════════╪══════════╡
│ 2024-08-07 ┆ 209.820007 ┆ 217.642081 ┆ 40.192313 ┆ 6.920431 │
│ 2024-08-08 ┆ 213.309998 ┆ 217.229501 ┆ 45.237928 ┆ 6.809686 │
│ 2024-08-09 ┆ 216.240005 ┆ 217.135264 ┆ 49.118920 ┆ 6.666851 │
└────────────┴────────────┴────────────┴───────────┴──────────┘
Each indicator names its output after itself, so it adds a column rather than overwriting the one it read, and siblings reading the same source do not collide. The name is the bare factory name, so two parameterizations of one indicator still want an explicit alias:
prices.with_columns(EMA(20), SMA(20)) # -> "ema", "sma"
prices.with_columns(EMA(20).alias("fast"), EMA(50).alias("slow"))
Single-source indicators default to pl.col("close") and also accept an explicit
source, which makes them chain with pipe:
EMA(20) # close by default
EMA(pl.col("open"), 20) # explicit source
pl.col("close").pipe(EMA, 5).pipe(RSI, 14)
TRANGE, ATR and the price transforms read high, low and close (plus
open, for AVGPRICE), each overridable by keyword. CCI is single-source
like the rest, but defaults its source to TYPPRICE() rather than
pl.col("close"):
CCI(20) # standard, over typical price
CCI(20, src=TYPPRICE(high="h", low="l", close="c")) # other column names
pl.col("close").pipe(CCI, 20) # over some other series
Indicators
EMA(period) |
Exponential moving average |
SMA(period) |
Simple moving average |
RMA(period) |
Wilder's running moving average |
WMA(period) |
Weighted moving average |
RSI(period) |
Wilder's relative strength index |
TRANGE() |
True range |
ATR(period) |
Average true range |
MACD(fast=12, slow=26, signal=9) |
MACD, signal and histogram expressions |
MAD(period=20) |
Rolling mean absolute deviation |
AVGPRICE() |
Average price, (open + high + low + close) / 4 |
MEDPRICE() |
Median price, (high + low) / 2 |
TYPPRICE() |
Typical price, (high + low + close) / 3 |
WCLPRICE() |
Weighted close price, (high + low + 2 * close) / 4 |
CCI(period=20) |
Commodity Channel Index |
KER(period=10) |
Kaufman efficiency ratio |
KAMA(period=10, fastn=2, slown=30) |
Kaufman adaptive moving average |
SAR(afs=0.02, maxaf=0.2) |
Parabolic Stop and Reverse |
STREAK(condition) |
Consecutive true count |
The price transforms are named after TA-Lib. Note that TA-Lib — and so bartons
— calls (high + low) / 2 MEDPRICE, reserving MIDPRICE for the rolling
midpoint of the highest high and lowest low over a period. Some libraries use
midprice for the first of those.
Multi-output native indicators return an ExprBundle, which Polars accepts as
one argument and expands into ordinary columns:
prices.with_columns(MACD())
prices.with_columns(*MACD(), SMA(20)) # splat when mixing with other expressions
Eager API
The compiled kernels are also callable directly on a pl.Series, bypassing the
expression layer:
from bartons import kernels
kernels.ema(prices["close"], period=20)
Parameters are keyword-only here. This path needs polars>=1.28; the expression
API alone works further back.
Related Projects
- polars-talib — a Polars extension exposing TA-Lib indicators and candlestick-pattern functions as Polars expressions.
- polars-ta — an expression-oriented collection of technical-analysis, WorldQuant, and Tongdaxin operators for Polars.
- Polars — a fast DataFrame library with Rust and Python APIs, an expression engine, lazy query optimization, and Arrow-compatible memory.
- PyO3 — Rust bindings for creating native Python modules and calling between Rust and Python.
- Maturin — a build and publishing tool for Python packages implemented in Rust.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bartons-0.1.2.tar.gz.
File metadata
- Download URL: bartons-0.1.2.tar.gz
- Upload date:
- Size: 592.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
maturin/1.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69550a38a69e4fc44877e49c952801949d2b77e5fe6e42937472a492e53b3970
|
|
| MD5 |
5bdf8474f128b92a7e633cce6fee84fa
|
|
| BLAKE2b-256 |
6dde5540064e8f7fdacbab942d111d61bb8483e1a365ee4900bc0dc357c597f9
|
File details
Details for the file bartons-0.1.2-cp311-abi3-manylinux_2_34_aarch64.whl.
File metadata
- Download URL: bartons-0.1.2-cp311-abi3-manylinux_2_34_aarch64.whl
- Upload date:
- Size: 6.5 MB
- Tags: CPython 3.11+, manylinux: glibc 2.34+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via:
maturin/1.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4139712e3d75c0577cd555e6decae3152ff2b12e38d4c331c04236c45fef4c71
|
|
| MD5 |
4c3a593bcea2443012959ee2e4181479
|
|
| BLAKE2b-256 |
860e2ee21fa39b9fe25798c0ec28fa1feeb1947b74bb5b809c17bb56cc03ac01
|