Skip to main content

Polars extension for Ta-Lib: Support Ta-Lib functions in Polars expressions

Project description

Polars Extension for Ta-Lib

Getting Started

pip install polars_talib

and

import polars
import polars_talib as plta

Usage

single symbol usage

df.with_columns(
    pl.col("close").ta.ema(5).alias("ema5"),
    pl.col("close").ta.macd(12, 26, 9).struct.field("macd"),
    pl.col("close").ta.macd(12, 26, 9).struct.field("macdsignal"),
    pl.col("open").ta.cdl2crows(pl.col("high"), pl.col("low"), pl.col("close")).alias("cdl2crows"),
    pl.col("close").ta.wclprice("high", "low").alias("wclprice"),
)

multiple symbol usage using over syntax

df.with_columns(
    pl.col("close").ta.ema(5).over("symbol").alias("ema5"),
    pl.col("close").ta.macd(12, 26, 9).over("symbol").struct.field("macd"),
    pl.col("close").ta.macd(12, 26, 9).over("symbol").struct.field("macdsignal"),
    pl.col("open").ta.cdl2crows(
        pl.col("high"), pl.col("low"), pl.col("close")
    ).over("symbol").alias("cdl2crows"),
    pl.col("close").ta.wclprice("high", "low").over("symbol").alias("wclprice"),
)

usage just like talib.abstract with more flexible

df.with_columns(
    plta.ht_dcperiod(),
    plta.ht_dcperiod(pl.col("close")),
    plta.aroon(),
    plta.aroon(pl.col("high"), pl.col("low"), timeperiod=10),
    plta.wclprice(),
    plta.wclprice(
        pl.col("high"), pl.col("low"), pl.col("close"), 
        timeperiod=10
    ),
)

Performance

Polars with polars_talib

%%timeit
df = p.with_columns(
    plta.sma(timeperiod=5).over("Symbol").alias("sma5"),
    plta.macd(fastperiod=10, slowperiod=20, signalperiod=5).over("Symbol").alias("macd"),
    plta.stoch(pl.col("high"), pl.col("low"), pl.col("close"), fastk_period=14, slowk_period=7, slowd_period=7).over("Symbol").alias("stoch"),
    plta.wclprice().over("Symbol").alias("wclprice"),
).with_columns(
    pl.col("macd").struct.field("macd"),
    pl.col("macd").struct.field("macdsignal"),
    pl.col("macd").struct.field("macdhist"),
    pl.col("stoch").struct.field("slowk"),
    pl.col("stoch").struct.field("slowd"),
).select(
    pl.exclude("stoch")
).filter(
    pl.col("Symbol") == "AAPL"
).collect()

135 ms ± 5.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Pandas with talib

%%timeit
df["sma5"] = df.groupby("Ticker")["close"].transform(lambda x: ta.SMA(x, timeperiod=5))
df["macd"] = df.groupby("Ticker")["close"].transform(lambda x: ta.MACD(x, fastperiod=10, slowperiod=20, signalperiod=5)[0])
df["macdsignal"] = df.groupby("Ticker")["close"].transform(lambda x: ta.MACD(x, fastperiod=10, slowperiod=20, signalperiod=5)[1])
df["macdhist"] = df.groupby("Ticker")["close"].transform(lambda x: ta.MACD(x, fastperiod=10, slowperiod=20, signalperiod=5)[2])
df["slowk"] = df.groupby("Ticker").apply(lambda x: ta.STOCH(x, fastk_period=14, slowk_period=7, slowd_period=7)).droplevel(0)["slowk"] 
df["slowd"] = df.groupby("Ticker").apply(lambda x: ta.STOCH(x, fastk_period=14, slowk_period=7, slowd_period=7)).droplevel(0)["slowd"]
df["wclprice"] = df.groupby("Ticker").apply(lambda x: ta.WCLPRICE(x)).droplevel(0)
df.loc["AAPL"]

19.2 s ± 367 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

It's about 150x faster, see more detail in basic.ipynb

Supported Indicators and Functions

import polars_talib as plta

# list of functions
plta.get_functions()

# dict of functions by group
plta.get_function_groups()

Indicator Groups

  • Overlap Studies
  • Momentum Indicators
  • Volume Indicators
  • Volatility Indicators
  • Price Transform
  • Cycle Indicators
  • Pattern Recognition
Overlap Studies
bbands               Bollinger Bands
dema                 Double Exponential Moving Average
ema                  Exponential Moving Average
ht_trendline         Hilbert Transform - Instantaneous Trendline
kama                 Kaufman Adaptive Moving Average
ma                   Moving average
mama                 MESA Adaptive Moving Average
mavp                 Moving average with variable period
midpoint             MidPoint over period
midprice             Midpoint Price over period
sar                  Parabolic SAR
sarext               Parabolic SAR - Extended
sma                  Simple Moving Average
t3                   Triple Exponential Moving Average (T3)
tema                 Triple Exponential Moving Average
trima                Triangular Moving Average
wma                  Weighted Moving Average
Momentum Indicators
adx                  Average Directional Movement Index
adxr                 Average Directional Movement Index Rating
apo                  Absolute Price Oscillator
aroon                Aroon
aroonosc             Aroon Oscillator
bop                  Balance Of Power
cci                  Commodity Channel Index
cmo                  Chande Momentum Oscillator
dx                   Directional Movement Index
macd                 Moving Average Convergence/Divergence
macdext              MACD with controllable MA type
macdfix              Moving Average Convergence/Divergence Fix 12/26
mfi                  Money Flow Index
minus_di             Minus Directional Indicator
minus_dm             Minus Directional Movement
mom                  Momentum
plus_di              Plus Directional Indicator
plus_dm              Plus Directional Movement
ppo                  Percentage Price Oscillator
roc                  Rate of change : ((price/prevPrice)-1)*100
rocp                 Rate of change Percentage: (price-prevPrice)/prevPrice
rocr                 Rate of change ratio: (price/prevPrice)
rocr100              Rate of change ratio 100 scale: (price/prevPrice)*100
rsi                  Relative Strength Index
stoch                Stochastic
stochf               Stochastic Fast
stochrsi             Stochastic Relative Strength Index
trix                 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA
ultosc               Ultimate Oscillator
willr                Williams' %R
Volume Indicators
ad                   Chaikin A/D Line
adosc                Chaikin A/D Oscillator
obv                  On Balance Volume
Cycle Indicators
ht_dcperiod          Hilbert Transform - Dominant Cycle Period
ht_dcphase           Hilbert Transform - Dominant Cycle Phase
ht_phasor            Hilbert Transform - Phasor Components
ht_sine              Hilbert Transform - SineWave
ht_trendmode         Hilbert Transform - Trend vs Cycle Mode
Price Transform
avgprice             Average Price
medprice             Median Price
typprice             Typical Price
wclprice             Weighted Close Price
Volatility Indicators
atr                  Average True Range
natr                 Normalized Average True Range
trange               True Range
Pattern Recognition
cdl2crows            Two Crows
cdl3blackcrows       Three Black Crows
cdl3inside           Three Inside Up/Down
cdl3linestrike       Three-Line Strike
cdl3outside          Three Outside Up/Down
cdl3starsinsoutH     Three Stars In The South
cdl3whitesoldieRS    Three Advancing White Soldiers
cdlabandonedbabY     Abandoned Baby
cdladvanceblock      Advance Block
cdlbelthold          Belt-hold
cdlbreakaway         Breakaway
cdlclosingmarubOZU   Closing Marubozu
cdlconcealbabysWALL  Concealing Baby Swallow
cdlcounterattacK     Counterattack
cdldarkcloudcovER    Dark Cloud Cover
cdldoji              Doji
cdldojistar          Doji Star
cdldragonflydojI     Dragonfly Doji
cdlengulfing         Engulfing Pattern
cdleveningdojisTAR   Evening Doji Star
cdleveningstar       Evening Star
cdlgapsidesidewHITE  Up/Down-gap side-by-side white lines
cdlgravestonedoJI    Gravestone Doji
cdlhammer            Hammer
cdlhangingman        Hanging Man
cdlharami            Harami Pattern
cdlharamicross       Harami Cross Pattern
cdlhighwave          High-Wave Candle
cdlhikkake           Hikkake Pattern
cdlhikkakemod        Modified Hikkake Pattern
cdlhomingpigeon      Homing Pigeon
cdlidentical3crOWS   Identical Three Crows
cdlinneck            In-Neck Pattern
cdlinvertedhammER    Inverted Hammer
cdlkicking           Kicking
cdlkickingbylenGTH   Kicking - bull/bear determined by the longer marubozu
cdlladderbottom      Ladder Bottom
cdllongleggeddoJI    Long Legged Doji
cdllongline          Long Line Candle
cdlmarubozu          Marubozu
cdlmatchinglow       Matching Low
cdlmathold           Mat Hold
cdlmorningdojisTAR   Morning Doji Star
cdlmorningstar       Morning Star
cdlonneck            On-Neck Pattern
cdlpiercing          Piercing Pattern
cdlrickshawman       Rickshaw Man
cdlrisefall3metHODS  Rising/Falling Three Methods
cdlseparatingliNES   Separating Lines
cdlshootingstar      Shooting Star
cdlshortline         Short Line Candle
cdlspinningtop       Spinning Top
cdlstalledpatteRN    Stalled Pattern
cdlsticksandwicH     Stick Sandwich
cdltakuri            Takuri (Dragonfly Doji with very long lower shadow)
cdltasukigap         Tasuki Gap
cdlthrusting         Thrusting Pattern
cdltristar           Tristar Pattern
cdlunique3river      Unique 3 River
cdlupsidegap2crOWS   Upside Gap Two Crows
cdlxsidegap3metHODS  Upside/Downside Gap Three Methods
Statistic Functions
beta                 Beta
correl               Pearson's Correlation Coefficient (r)
linearreg            Linear Regression
linearreg_angle      Linear Regression Angle
linearreg_intercept  Linear Regression Intercept
linearreg_slope      Linear Regression Slope
stddev               Standard Deviation
tsf                  Time Series Forecast
var                  Variance

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

polars_talib-0.1.5-cp37-abi3-win_amd64.whl (3.5 MB view details)

Uploaded CPython 3.7+Windows x86-64

polars_talib-0.1.5-cp37-abi3-win32.whl (2.9 MB view details)

Uploaded CPython 3.7+Windows x86

polars_talib-0.1.5-cp37-abi3-manylinux_2_38_aarch64.whl (4.1 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.38+ ARM64

polars_talib-0.1.5-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.5 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ x86-64

polars_talib-0.1.5-cp37-abi3-macosx_11_0_arm64.whl (3.6 MB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

polars_talib-0.1.5-cp37-abi3-macosx_10_12_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.7+macOS 10.12+ x86-64

File details

Details for the file polars_talib-0.1.5-cp37-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for polars_talib-0.1.5-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 7fb2d64d3b92eb2625ac667e5395b1baf704ed6ee8a1e250eaacbde47d8d10d0
MD5 728fc1d968f51edefdd95e78959ca7d1
BLAKE2b-256 51b88084249359790c98f463569e98331fd5686d82fce7e50790333e49bda609

See more details on using hashes here.

File details

Details for the file polars_talib-0.1.5-cp37-abi3-win32.whl.

File metadata

  • Download URL: polars_talib-0.1.5-cp37-abi3-win32.whl
  • Upload date:
  • Size: 2.9 MB
  • Tags: CPython 3.7+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.1

File hashes

Hashes for polars_talib-0.1.5-cp37-abi3-win32.whl
Algorithm Hash digest
SHA256 61638172eefa7e8cf531ce88ddbf11ccf2fdea4ef06246b89126e026c055dcc8
MD5 6a0f653168f8c018f6c9fb35292d00f2
BLAKE2b-256 06bd2f53bb4cf3d2037ee43a7b85c4e984caeebb69ca17d76146832e04d69230

See more details on using hashes here.

File details

Details for the file polars_talib-0.1.5-cp37-abi3-manylinux_2_38_aarch64.whl.

File metadata

File hashes

Hashes for polars_talib-0.1.5-cp37-abi3-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 da82f845f5f30de0108d1490eaf91c7a7e55d603f75cebd0112f56cb0d82b4fe
MD5 d7c437c4f8abec9c747c28109afa2d9b
BLAKE2b-256 0d1b442bcdef40486474215e49fdb5eac58c474e32e465390ef08521568f9bf6

See more details on using hashes here.

File details

Details for the file polars_talib-0.1.5-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_talib-0.1.5-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d794e0e4a4f0c28870adc8f0b7b9a89761d232db8c3534d7f8ef382cfcc6fab0
MD5 03f0ee0d4591e01f610a0da70867a407
BLAKE2b-256 3989db0dcfd565b9089c6acf15f976a81c6010a5c8c3b4c5f713cc80846a4430

See more details on using hashes here.

File details

Details for the file polars_talib-0.1.5-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_talib-0.1.5-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 990a488c17e96d8c816e01cef0d8eed925291a9da607081c38fc767ef665af42
MD5 03cb51d65769725fc1cc9afd77728c70
BLAKE2b-256 1da908c203d8e256d3490f960e390ec4046d7079df027eef13bae7c14976a0fd

See more details on using hashes here.

File details

Details for the file polars_talib-0.1.5-cp37-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_talib-0.1.5-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1af11b5dbc6b50d69af4ec809cb2ca12378fe8d7b91d0871f59de5d23948634f
MD5 a159525a358d204037d0ace838a23586
BLAKE2b-256 c95a7fb40579f8f37e86f4f90eb319a32760c0e4c0d919acb419916493533a06

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page