Skip to main content

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

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 — the DataFrame library. Since every indicator here is a plain pl.Expr, its expression docs cover most of what you can do with them: windows, groups, lazy frames, and the rest.
  • PyO3 — Rust bindings for Python, and the polars plugin interface these kernels are written against. Worth reading if you want to write indicators of your own.
  • Maturin — builds and publishes Rust extensions as Python wheels. The tool to reach for if you take that route.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bartons-0.1.1.tar.gz (586.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bartons-0.1.1-cp311-abi3-manylinux_2_34_aarch64.whl (6.5 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.34+ ARM64

File details

Details for the file bartons-0.1.1.tar.gz.

File metadata

  • Download URL: bartons-0.1.1.tar.gz
  • Upload date:
  • Size: 586.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for bartons-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1f3f53190ba4edd13291cc1c054ffddfaf4006fa997822721e64594718f4c2e3
MD5 2a032f335c18aac611cf0b3cde14981f
BLAKE2b-256 36c1cb012df729d227650cfeefe5a0fa85c8857e3d33ed8df6182c4e210d5c58

See more details on using hashes here.

File details

Details for the file bartons-0.1.1-cp311-abi3-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for bartons-0.1.1-cp311-abi3-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 80851f9ecb065ca4c59bee8147bd04b1df4a72aa1911d725d0576641744bd018
MD5 56428a5f677265258f2206ff41502fa2
BLAKE2b-256 f1b6083230c7ab438409d5bfda82c2506995e5ff71c3b8863bcf23bddf8bb07e

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.7

6 files

0.1.6

6 files

0.1.5

6 files

0.1.4

6 files

0.1.2

2 files

This release

0.1.1 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page