GithubHelp home page GithubHelp logo

yvictor / polars_ta_extension Goto Github PK

View Code? Open in Web Editor NEW
60.0 3.0 11.0 416 KB

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

License: MIT License

Makefile 0.01% Python 43.75% Rust 56.21% C 0.02%
polars pyo3 ta-lib talib talib-binding

polars_ta_extension's Introduction

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

polars_ta_extension's People

Contributors

yvictor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

polars_ta_extension's Issues

Failed to install on rust

So, in my project I did cargo add talib-sys
The build fails with this error:

   Compiling talib-sys v0.1.0
error: could not find native static library `ta_lib`, perhaps an -L flag is missing?

error: could not compile `talib-sys` (lib) due to 1 previous error

I figured that the solution is to find out the location. In my case:
~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/talib-sys-0.1.0

Then, i cloned this repo into /tmp, built it myself and did:
cp /tmp/polars_ta_extension/talib-sys/dependencies ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/talib-sys-0.1.0

That allowed me to build properly.
Maybe the cargo version is not up to date?

Request for Manylinux aarch64.whl Support

Environment:

  • Operating System: macOS (Apple M1)
  • Python Version: Python 3.12.3
  • pip Version: pip 24.0

Description:
Following the successful resolution of the previous issue regarding ARM-compatible wheels for M1 Macs, I have an additional request. It would be highly beneficial if support for the manylinux aarch64.whl could be added. This would greatly aid in testing the library in Docker environments and ensure compatibility across different systems.

Steps to Reproduce:

  1. Attempt to install the library in a Docker environment on an ARM architecture machine.
  2. Encounter issues due to the lack of manylinux aarch64.whl support.

Expected Behavior:
The library should install successfully in Docker environments on ARM architecture with the addition of manylinux aarch64.whl support.

Additional Context:
Adding this support would greatly enhance the usability and testing capabilities of the library across diverse environments.

Thank you for considering this request and for your continued support!

Best regards,
@0xAndrii

ModuleNotFoundError: No module named 'polars.utils'

After upgrading to polars 1.0, the following error occurred.
Would you guide me how to fix it?

from polars.utils.udfs import _get_shared_lib_location
ModuleNotFoundError: No module named 'polars.utils'

Request for ARM-compatible wheels for M1 Macs

Environment

  • Operating System: macOS (Apple M1)
  • Python Version: Python 3.12.3
  • pip Version: pip 24.0

Description

I am trying to install polars-talib==0.1.2 on my M1 Mac but am encountering an error indicating that there are no matching distributions found. This suggests that there might not be pre-built wheels available for ARM architecture.

Steps to Reproduce

  1. Run pip install polars-talib==0.1.2
  2. Encounter error messages indicating incompatibility.

× No solution found when resolving dependencies: ╰─▶ Because polars-talib==0.1.2 is unusable because no wheels are available with a matching Python implementation and you require polars-talib==0.1.2, we can conclude that the requirements are unsatisfiable.

Possible Solution

It would be highly beneficial for Mac users with ARM architecture if ARM-compatible wheels could be made available.

Additional Context

Really appreciate your help in making this library accessible on different architectures.

CCI returns different results every time is runs

I've been facing weird issue when trying to calculate CCI (ta.cci).
Each time I run the exact same code, on the same data, and same arguments, I get different values for the feature.

the code:

raw_data.with_columns(cci=pl.col("adjusted_close").ta.cci(pl.col("adjusted_high"), pl.col("adjusted_low"), timeperiod=7).over("ticker")).collect())

I've attached a screen recording of the issue as it's the most simple way to display it.

Recording.2024-07-22.130315.mp4

where raw_data is a lazyframe with simple structure of row per day per ticker, nothing special.
The raw data is sorted.

I tried with and without filtering the lazyframe to only one ticker, sorting by date asc and desc, but nothing helped.

err: TA_OUT_OF_RANGE_END_INDEX

using this script (based on the example [here]:

df_data = (df_data
.with_columns(pl.col("LDPS_w").ta.wma(lookback_period).over(["ticker", "timeframe"]).alias("LDPS_w")))

I get this error:

"polars.exceptions.ComputeError: the plugin failed with message: Could not compute indicator, err: TA_OUT_OF_RANGE_END_INDEX"

could you please help me understand what I should be doing?

apologies, my mistake, works fine

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.