### Install polars_talib Source: https://github.com/yvictor/polars_ta_extension/blob/master/README.md Installs the polars_talib package using pip. This is the primary dependency for using the TA-Lib extension with Polars. ```bash pip install polars_talib ``` -------------------------------- ### Install talib Crate using Cargo Source: https://github.com/yvictor/polars_ta_extension/blob/master/talib/README.md This command adds the 'talib' crate as a dependency to your Rust project. Ensure you have Cargo installed and configured. ```bash cargo add talib ``` -------------------------------- ### Python Calculation Example Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/basic.ipynb This Python code snippet demonstrates a simple arithmetic calculation used in the performance comparison. It involves addition, multiplication, and division. ```python (1.2 + 19.2) * 1000 / 135 ``` -------------------------------- ### Calculate Exponential Moving Average (EMA) in Polars Source: https://context7.com/yvictor/polars_ta_extension/llms.txt Demonstrates calculating EMA with different time periods using the method chaining syntax (.ta.ema()). The example uses sample closing price data and highlights that earlier values will be null until sufficient data is present. ```python import polars as pl import polars_talib as plta df = pl.DataFrame({ "close": [100.0, 102.0, 104.0, 103.0, 105.0, 107.0, 106.0, 108.0] }) # Calculate multiple EMAs result = df.with_columns( pl.col("close").ta.ema(5).alias("ema_5"), pl.col("close").ta.ema(10).alias("ema_10"), pl.col("close").ta.ema(20).alias("ema_20") ) print(result) ``` -------------------------------- ### Get List of Supported TA-Lib Functions in polars_talib Source: https://github.com/yvictor/polars_ta_extension/blob/master/README.md Retrieves a list of all TA-Lib indicator functions that are supported and implemented within the polars_talib extension. This function is useful for discovering available technical analysis tools. ```python import polars_talib as plta # list of functions plta.get_functions() ``` -------------------------------- ### Calculate Simple Moving Average (SMA) in Polars Source: https://context7.com/yvictor/polars_ta_extension/llms.txt Shows how to calculate SMA using both method chaining (.ta.sma()) and functional syntax (plta.sma()). It includes examples with explicit time periods and default parameters, demonstrating usage with sample OHLCV data. ```python import polars as pl import polars_talib as plta # Sample OHLCV data df = pl.DataFrame({ "open": [100.0, 102.0, 101.0, 103.0, 105.0], "high": [103.0, 104.0, 103.0, 106.0, 107.0], "low": [99.0, 101.0, 100.0, 102.0, 104.0], "close": [102.0, 103.0, 101.0, 105.0, 106.0], "volume": [1000, 1100, 900, 1200, 1300] }) # Method syntax: using .ta namespace result = df.with_columns( pl.col("close").ta.sma(timeperiod=3).alias("sma_3") ) # Functional syntax: using plta module result = df.with_columns( plta.sma(pl.col("close"), timeperiod=3).alias("sma_3") ) # Default parameters (uses "close" column, timeperiod=30) result = df.with_columns( plta.sma().alias("sma_30") ) print(result) ``` -------------------------------- ### Get TA-Lib Functions Grouped by Category in polars_talib Source: https://github.com/yvictor/polars_ta_extension/blob/master/README.md Retrieves a dictionary where TA-Lib indicator functions are grouped by their respective categories (e.g., Overlap Studies, Momentum Indicators). This helps in understanding the organization of available technical indicators. ```python import polars_talib as plta # dict of functions by group plta.get_function_groups() ``` -------------------------------- ### Initialize and Explore Polars TA-Lib Source: https://context7.com/yvictor/polars_ta_extension/llms.txt Demonstrates importing the library, checking the TA-Lib version, listing all available functions, and retrieving functions grouped by category. This helps in understanding the library's capabilities and structure. ```python import polars as pl import polars_talib as plta # Library initialization happens automatically on import # Check TA-Lib version print(plta.__talib_version__) # "0.4.0" # List all available functions functions = plta.get_functions() print(functions[:5]) # ['ht_dcperiod', 'ht_dcphase', 'ht_phasor', 'ht_sine', 'ht_trendmode'] # Get functions grouped by category groups = plta.get_function_groups() print(groups.keys()) # dict_keys(['Cycle Indicators', 'Math Operators', 'Momentum Indicators', ...]) ``` -------------------------------- ### Import Libraries for Polars and TA Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Imports necessary libraries including polars, polars_talib, pandas, and talib.abstract for technical analysis. ```python import time import polars as pl import polars_talib as plta import pandas as pd import talib.abstract as ta ``` -------------------------------- ### Calculate WCLPrice with Polars TA Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Calculates the Weighted Closing Price using polars_talib, grouped by 'Symbol', and measures the execution time. ```python start_t = time.time() p.with_columns( plta.wclprice().over("Symbol").alias("wclprice") ).collect() end_t = time.time() pl_wclprice_spend_t = end_t - start_t spend_records["pl_wclprice"] = pl_wclprice_spend_t ``` -------------------------------- ### Calculate WCLPrice with Pandas TA Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Calculates the Weighted Closing Price using pandas and talib.abstract, grouped by 'Ticker', and measures the execution time. ```python start_t = time.time() df["wclprice"] = df.groupby("Ticker").apply(lambda x: ta.WCLPRICE(x)).droplevel(0) end_t = time.time() pd_wclprice_spend_t = end_t - start_t spend_records["pd_wclprice"] = pd_wclprice_spend_t ``` -------------------------------- ### Read and Prepare Data with Pandas Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Reads a Parquet file into a Pandas DataFrame, sets a multi-index, and converts specific column names to lowercase. ```python start_t = time.time() df = ( pd.read_parquet("us_market_cap2000.parquet") .set_index(["Ticker", "Date"]) .rename(columns={c: c.lower() for c in ["Open", "High", "Low", "Close"]}) ) end_t = time.time() pd_read_spend = end_t - start_t spend_records["pd_read"] = pd_read_spend ``` -------------------------------- ### Performance Comparison: Polars with polars_talib vs. Pandas with ta Source: https://github.com/yvictor/polars_ta_extension/blob/master/README.md Measures the execution time of calculating multiple technical indicators (SMA, MACD, STOCH, WCLPRICE) using both Polars with polars_talib and Pandas with the standard TA-Lib library. This benchmark highlights the significant performance advantage of the Polars-based solution. ```python %%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() ``` -------------------------------- ### Create Sample OHLCV DataFrame for Trading Strategy in Polars Source: https://context7.com/yvictor/polars_ta_extension/llms.txt This Python code snippet demonstrates how to create a sample Polars DataFrame containing typical OHLCV (Open, High, Low, Close, Volume) data with timestamps and symbols, suitable for backtesting trading strategies. It uses Polars' datetime functionality to generate a date range. ```python import polars as pl import polars_talib as plta # Load or create OHLCV data df = pl.DataFrame({ "timestamp": pl.datetime_range(pl.datetime(2024, 1, 1), pl.datetime(2024, 3, 1), "1d", eager=True)[:50], "symbol": ["AAPL"] * 50, "open": [150.0 + i * 0.5 for i in range(50)], "high": [152.0 + i * 0.5 for i in range(50)], "low": [149.0 + i * 0.5 for i in range(50)], "close": [151.0 + i * 0.5 for i in range(50)], "volume": [1000000 + i * 10000 for i in range(50)] }) ``` -------------------------------- ### Read and Prepare Data with Polars Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Scans a Parquet file into a Polars DataFrame, selecting specific columns and converting float column names to lowercase. ```python p = pl.scan_parquet("us_market_cap2000.parquet").select( pl.col("Date"), pl.col("Ticker").alias("Symbol"), pl.selectors.float().name.to_lowercase() ) ``` -------------------------------- ### Create Benchmark DataFrame Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Constructs a Polars DataFrame from the collected performance records, categorizing operations by 'kind' (polars/pandas) and 'op' (read/sma/macd/stoch/wclprice). ```python df_bench = pl.DataFrame( { "kind": [k.split("_")[0] for k in spend_records.keys()], "op": [k.split("_")[1] for k in spend_records.keys()], "time": [v for v in spend_records.values()], } ).with_columns( pl.when(pl.col("kind")=="pl").then(pl.lit("polars")).otherwise(pl.lit("pandas")).alias("stack") ) ``` -------------------------------- ### Performance Comparison: Polars TA-Lib vs. Pandas TA-Lib Source: https://context7.com/yvictor/polars_ta_extension/llms.txt This snippet compares the performance of calculating technical indicators (SMA, MACD, RSI) on a large dataset with multiple symbols using Polars with polars_talib extension versus Pandas with the TA-Lib library. It measures the execution time for each approach and calculates the speedup factor. ```python import polars as pl import polars_talib as plta import pandas as pd import talib import time # Create large dataset with multiple symbols n_symbols = 100 n_bars = 1000 data = [] for symbol_id in range(n_symbols): for bar in range(n_bars): data.append({ "Symbol": f"SYM{symbol_id:03d}", "close": 100.0 + bar * 0.1, "high": 102.0 + bar * 0.1, "low": 99.0 + bar * 0.1, "open": 100.5 + bar * 0.1 }) # Polars approach pl_df = pl.DataFrame(data) start = time.time() pl_result = pl_df.with_columns([ plta.sma(pl.col("close"), timeperiod=5).over("Symbol").alias("sma5"), plta.macd(pl.col("close"), fastperiod=10, slowperiod=20, signalperiod=5) .over("Symbol") .struct.field("macd") .alias("macd"), plta.rsi(pl.col("close"), timeperiod=14).over("Symbol").alias("rsi") ]) polars_time = time.time() - start print(f"Polars + polars_talib: {polars_time:.3f}s") # Pandas approach pd_df = pd.DataFrame(data) start = time.time() pd_df["sma5"] = pd_df.groupby("Symbol")["close"].transform(lambda x: talib.SMA(x, timeperiod=5)) pd_df["macd"] = pd_df.groupby("Symbol")["close"].transform(lambda x: talib.MACD(x, fastperiod=10, slowperiod=20, signalperiod=5)[0]) pd_df["rsi"] = pd_df.groupby("Symbol")["close"].transform(lambda x: talib.RSI(x, timeperiod=14)) pandas_time = time.time() - start print(f"Pandas + talib: {pandas_time:.3f}s") print(f"Speedup: {pandas_time / polars_time:.1f}x") # Expected output: 50-150x faster depending on dataset size and complexity ``` -------------------------------- ### Calculate RSI using talib in Rust Source: https://github.com/yvictor/polars_ta_extension/blob/master/talib/README.md This Rust code snippet demonstrates how to use the 'talib' crate to calculate the Relative Strength Index (RSI). It includes initializing the library, defining input data (close prices), setting parameters for RSI calculation, processing the results, and shutting down the library. Error handling for the RSI calculation is also included. ```rust use talib::common::{TimePeriodKwargs, ta_initialize, ta_shutdown}; use talib::momentum::ta_rsi; fn main() { // Initialize Ta-Lib let _ = ta_initialize(); // Sample close prices let close_prices: Vec = vec![ 1.087010, 1.087120, 1.087080, 1.087170, 1.087110, 1.087010, 1.087100, 1.087120, 1.087110, 1.087080, 1.087000, 1.086630, 1.086630, 1.086610, 1.086630, 1.086640, 1.086650, 1.086650, 1.086670, 1.086630, ]; // Specify the RSI calculation parameters let kwargs = TimePeriodKwargs { timeperiod: 5 }; // or with builder let kwargs = TimePeriodKwargsBuilder::default() .timeperiod(5) .build() .unwrap(); // Calculate RSI let res = ta_rsi(close_prices.as_ptr(), close_prices.len(), &kwargs); // Process the result match res { Ok(rsi_values) => { for (index, value) in rsi_values.iter().enumerate() { println!("RSI at index {}: {}", index, value); } } Err(e) => { println!("Error: {:?}", e); } } // Shutdown Ta-Lib let _ = ta_shutdown(); } ``` -------------------------------- ### Import polars and polars_talib Source: https://github.com/yvictor/polars_ta_extension/blob/master/README.md Imports the necessary libraries, polars for DataFrame manipulation and polars_talib for technical analysis functions. ```python import polars import polars_talib as plta ``` -------------------------------- ### Download Stock Data with yfinance Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/basic.ipynb Downloads historical stock data for a list of tickers using the yfinance library. It supports downloading data for up to 2000 tickers and specifies a date range. The data is grouped by ticker and then stacked into a single DataFrame. ```python import yfinance as yf import polars as pl import polars_talib as plta import pandas as pd import talib.abstract as ta tickers = pl.scan_csv("nasdaq_screener.csv").sort("Market Cap", descending=True).filter( pl.col("Market Cap").is_not_null() & (pl.col("Market Cap") > 0) ).select(pl.col("Symbol").str.strip_chars(" "), pl.col("Market Cap")).collect()["Symbol"].to_list() data = yf.download(tickers[:2000], start='2001-01-01', end='2024-12-31', group_by='ticker', threads=32).stack(level=0).reset_index() ``` -------------------------------- ### Calculate SMA with Polars TA Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Calculates the Simple Moving Average (SMA) using polars_talib over a 5-period window, grouped by 'Symbol', and measures the execution time. ```python start_t = time.time() p.with_columns( plta.sma(timeperiod=5).over("Symbol").alias("sma5") ).collect() end_t = time.time() pl_sma_spend_t = end_t - start_t spend_records["pl_sma"] = pl_sma_spend_t ``` -------------------------------- ### Calculate Stochastic Oscillator with Polars TA Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Calculates the Stochastic Oscillator (%K and %D) using polars_talib with specified periods, grouped by 'Symbol', and measures the execution time. ```python start_t = time.time() p.with_columns( plta.stoch( pl.col("high"), pl.col("low"), pl.col("close"), fastk_period=14, slowk_period=7, slowd_period=7, ) .over("Symbol") .alias("stoch") ).with_columns( pl.col("stoch").struct.field("slowk"), pl.col("stoch").struct.field("slowd") ).select(pl.exclude("stoch")).collect() end_t = time.time() pl_stoch_spend_t = end_t - start_t spend_records["pl_stoch"] = pl_stoch_spend_t ``` -------------------------------- ### Calculate MACD with Pandas TA Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Calculates the MACD, its signal line, and histogram using pandas and talib.abstract, grouped by 'Ticker', and measures the execution time. ```python start_t = time.time() g = df.groupby("Ticker")["close"] df["macd"] = g.transform(lambda x: ta.MACD(x, fastperiod=10, slowperiod=20, signalperiod=5)[0]) df["macdsignal"] = g.transform( lambda x: ta.MACD(x, fastperiod=10, slowperiod=20, signalperiod=5)[1] ) df["macdhist"] = g.transform(lambda x: ta.MACD(x, fastperiod=10, slowperiod=20, signalperiod=5)[2]) end_t = time.time() pd_macd_spend_t = end_t - start_t spend_records["pd_macd"] = pd_macd_spend_t ``` -------------------------------- ### Calculate Stochastic Oscillator with Pandas TA Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Calculates the Stochastic Oscillator (%K and %D) using pandas and talib.abstract, grouped by 'Ticker', and measures the execution time. ```python start_t = time.time() g = df.groupby("Ticker") df["slowk"] = g.apply( lambda x: ta.STOCH(x, fastk_period=14, slowk_period=7, slowd_period=7) ).droplevel(0)["slowk"] df["slowd"] = g.apply( lambda x: ta.STOCH(x, fastk_period=14, slowk_period=7, slowd_period=7) ).droplevel(0)["slowd"] end_t = time.time() pd_stoch_spend_t = end_t - start_t spend_records["pd_stoch"] = pd_stoch_spend_t ``` -------------------------------- ### Use polars_talib Functions Directly like talib.abstract Source: https://github.com/yvictor/polars_ta_extension/blob/master/README.md Illustrates how to call TA-Lib functions directly from the plta module, similar to the `talib.abstract` interface. This approach allows for more flexibility in specifying parameters and can be used with or without explicit column selections. ```python 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 Comparison: Pandas with ta Source: https://github.com/yvictor/polars_ta_extension/blob/master/README.md Measures the execution time of calculating multiple technical indicators (SMA, MACD, STOCH, WCLPRICE) using Pandas with the standard TA-Lib library. This benchmark is provided for comparison with the Polars-based solution. ```python %%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"] ``` -------------------------------- ### Calculate Price Transforms using polars_talib in Polars Source: https://context7.com/yvictor/polars_ta_extension/llms.txt This code calculates various price transform indicators (Average Price, Median Price, Typical Price, Weighted Close Price) using the polars_talib library. It demonstrates both explicit column passing and default column name usage. The input is a Polars DataFrame with 'open', 'high', 'low', and 'close' columns. ```python import polars as pl import polars_talib as plta df = pl.DataFrame({ "open": [100.0, 102.0, 101.0, 103.0, 105.0], "high": [103.0, 104.0, 103.0, 106.0, 107.0], "low": [99.0, 101.0, 100.0, 102.0, 104.0], "close": [102.0, 103.0, 101.0, 105.0, 106.0] }) # Calculate various price transforms result = df.with_columns( plta.avgprice(pl.col("open"), pl.col("high"), pl.col("low"), pl.col("close")) .alias("avg_price"), plta.medprice(pl.col("high"), pl.col("low")) .alias("median_price"), plta.typprice(pl.col("high"), pl.col("low"), pl.col("close")) .alias("typical_price"), plta.wclprice(pl.col("high"), pl.col("low"), pl.col("close")) .alias("weighted_close_price") ) # Using method syntax with default column names result = df.with_columns( plta.wclprice().alias("wcl") # Defaults to high/low/close columns ) print(result) ``` -------------------------------- ### Plot Benchmark Results Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Generates a bar chart comparing the execution times ('time') for each operation ('op'), separated by the library stack ('stack'). ```python df_bench.plot.bar(x="op", y="time", by="stack") ``` -------------------------------- ### Calculate MACD with Polars TA Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Calculates the MACD indicator using polars_talib with specified periods, extracts MACD, signal line, and histogram, and measures the execution time. ```python start_t = time.time() p.with_columns( plta.macd(fastperiod=10, slowperiod=20, signalperiod=5).over("Symbol").alias("macd") ).with_columns( pl.col("macd").struct.field("macd"), pl.col("macd").struct.field("macdsignal"), pl.col("macd").struct.field("macdhist") ).collect() end_t = time.time() pl_macd_spend_t = end_t - start_t spend_records["pl_macd"] = pl_macd_spend_t ``` -------------------------------- ### Apply SMA using pandas 'apply' Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/basic.ipynb Calculates SMA for each group (Ticker) using pandas 'apply' method. This method is less efficient than 'transform' and is demonstrated for comparison. The '%%timeit' command measures execution time. ```python %%timeit df["sma5"] = df.groupby("Ticker").apply(lambda x: ta.SMA(x, timeperiod=5)).droplevel(0) ``` -------------------------------- ### Display Benchmark Results Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Displays the benchmark results DataFrame, showing operation type, time taken, and the library stack (polars or pandas). ```python df_bench ``` -------------------------------- ### Calculate Statistical Functions using polars_talib in Polars Source: https://context7.com/yvictor/polars_ta_extension/llms.txt This Python script demonstrates the calculation of various statistical functions including linear regression (slope, intercept, angle), correlation, beta, standard deviation, variance, and time series forecast using the polars_talib library. It requires input DataFrames with appropriate columns (e.g., 'x', 'y') and specified time periods. ```python import polars as pl import polars_talib as plta df = pl.DataFrame({ "x": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0], "y": [2.0, 4.0, 5.0, 4.0, 5.0, 7.0, 8.0, 9.0, 10.0, 11.0] }) # Linear regression result = df.with_columns( plta.linearreg(pl.col("x"), timeperiod=5).alias("linreg"), plta.linearreg_slope(pl.col("x"), timeperiod=5).alias("slope"), plta.linearreg_intercept(pl.col("x"), timeperiod=5).alias("intercept"), plta.linearreg_angle(pl.col("x"), timeperiod=5).alias("angle") ) # Correlation and Beta result = df.with_columns( plta.correl(pl.col("x"), pl.col("y"), timeperiod=5).alias("correlation"), plta.beta(pl.col("x"), pl.col("y"), timeperiod=5).alias("beta") ) # Standard deviation and variance result = df.with_columns( plta.stddev(pl.col("x"), timeperiod=5, nbdev=1.0).alias("stddev"), plta.var(pl.col("x"), timeperiod=5, nbdev=1.0).alias("variance") ) # Time Series Forecast result = df.with_columns( plta.tsf(pl.col("x"), timeperiod=5).alias("forecast") ) print(result) ``` -------------------------------- ### Calculate SMA with Pandas TA Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Calculates the Simple Moving Average (SMA) for a 5-period window on the 'close' price, grouped by 'Ticker', and measures the execution time. ```python start_t = time.time() df["sma5"] = df.groupby("Ticker")["close"].transform(lambda x: ta.SMA(x, timeperiod=5)) end_t = time.time() pd_sma_spend_t = end_t - start_t spend_records["pd_sma"] = pd_sma_spend_t ``` -------------------------------- ### Apply SMA using pandas 'transform' Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/basic.ipynb Calculates SMA for each group (Ticker) using pandas 'transform' method. This is generally faster than 'apply' but still significantly slower than polars' optimized approach. The '%%timeit' command measures execution time. ```python %%timeit df["sma5"] = df.groupby("Ticker")["close"].transform(lambda x: ta.SMA(x, timeperiod=5)) ``` -------------------------------- ### Apply SMA using polars_talib with 'over' syntax Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/basic.ipynb Applies the Simple Moving Average (SMA) calculation to each symbol in a DataFrame using the polars_talib extension. The 'over' syntax enables efficient group-wise operations. This method is significantly faster than pandas equivalents, including file reading and data transformation. ```python %%timeit df = p.with_columns( plta.sma(timeperiod=5).over("Symbol").alias("sma5"), ).filter( pl.col("Symbol") == "NVDA" ).collect() ``` -------------------------------- ### Benchmark Polars Data Reading Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/bench.ipynb Measures the time taken to collect data into a Polars DataFrame and records it. ```python spend_records = {} start_t = time.time() p.collect() end_t = time.time() pl_read_spend = end_t - start_t spend_records["pl_read"] = pl_read_spend ``` -------------------------------- ### Scan Parquet and Prepare for Polars TA Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/basic.ipynb Scans a parquet file containing stock data and selects specific columns. It renames the 'Ticker' column to 'Symbol' and converts all float column names to lowercase. This is a preparatory step for using polars_talib functions. ```python p = pl.scan_parquet("us_market_cap2000.parquet").select( pl.col("Date"), pl.col("Ticker").alias("Symbol"), pl.selectors.float().name.to_lowercase() ) ``` -------------------------------- ### Read and transform data with pandas Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/basic.ipynb Reads a Parquet file and renames columns to lowercase using pandas. This operation is used as a baseline for performance comparison. The '%%timeit' magic command measures the execution time. ```python %%timeit df = pd.read_parquet("us_market_cap2000.parquet").set_index(["Ticker", "Date"]).rename( columns={c: c.lower() for c in ["Open", "High", "Low", "Close"]} ) ``` ```python df = pd.read_parquet("us_market_cap2000.parquet").set_index(["Ticker", "Date"]).rename( columns={c: c.lower() for c in ["Open", "High", "Low", "Close"]} ) ``` -------------------------------- ### Apply multiple TA functions using pandas Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/basic.ipynb Applies multiple technical analysis functions (SMA, MACD, STOCH, WCLPRICE) using pandas 'transform' and 'apply'. This demonstrates the more verbose and less efficient syntax in pandas, especially for functions with multiple outputs, leading to significantly longer execution times. ```python %%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) ``` -------------------------------- ### Calculate Technical Indicators on Multi-Symbol DataFrames using 'over' in Polars Source: https://github.com/yvictor/polars_ta_extension/blob/master/README.md Shows how to apply TA-Lib indicators to a Polars DataFrame containing multiple symbols using the `.over("symbol")` syntax. This is useful for calculations that should be performed independently for each group (e.g., each stock symbol). ```python 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"), ) ``` -------------------------------- ### Calculate Multiple Technical Indicators and Trading Signals in Polars Source: https://context7.com/yvictor/polars_ta_extension/llms.txt This snippet demonstrates how to calculate various technical indicators (SMA, EMA, RSI, MACD, ATR, Bollinger Bands, OBV) and generate buy/sell trading signals using the polars_ta_extension library within a Polars DataFrame. It also shows how to extract components from complex indicator outputs and filter signals. ```python import polars as pl import polars_talib as plta # Assuming df is a Polars DataFrame with columns like 'close', 'high', 'low', 'volume', 'timestamp' # Example DataFrame creation (replace with your actual data loading) df = pl.DataFrame({ "timestamp": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], "close": [10, 11, 12, 11, 12, 13, 14, 13, 14, 15, 16, 15, 16, 17, 18, 17, 18, 19, 20, 19], "high": [11, 12, 13, 12, 13, 14, 15, 14, 15, 16, 17, 16, 17, 18, 19, 18, 19, 20, 21, 20], "low": [9, 10, 11, 10, 11, 12, 13, 12, 13, 14, 15, 14, 15, 16, 17, 16, 17, 18, 19, 18], "volume": [100, 110, 120, 115, 125, 130, 140, 135, 145, 150, 160, 155, 165, 170, 180, 175, 185, 190, 200, 195], }) # Calculate multiple indicators signals = df.with_columns([ # Trend indicators plta.sma(pl.col("close"), timeperiod=10).alias("sma_10"), plta.sma(pl.col("close"), timeperiod=20).alias("sma_20"), plta.ema(pl.col("close"), timeperiod=12).alias("ema_12"), # Momentum indicators plta.rsi(pl.col("close"), timeperiod=14).alias("rsi"), plta.macd(pl.col("close"), fastperiod=12, slowperiod=26, signalperiod=9).alias("macd_struct"), # Volatility plta.atr(pl.col("high"), pl.col("low"), pl.col("close"), timeperiod=14).alias("atr"), plta.bbands(pl.col("close"), timeperiod=20, nbdevup=2.0, nbdevdn=2.0).alias("bbands"), # Volume plta.obv(pl.col("close"), pl.col("volume")).alias("obv"), ]).with_columns([ # Extract MACD components pl.col("macd_struct").struct.field("macd").alias("macd"), pl.col("macd_struct").struct.field("macdsignal").alias("macd_signal"), pl.col("macd_struct").struct.field("macdhist").alias("macd_hist"), # Extract Bollinger Bands pl.col("bbands").struct.field("upperband").alias("bb_upper"), pl.col("bbands").struct.field("middleband").alias("bb_middle"), pl.col("bbands").struct.field("lowerband").alias("bb_lower"), ]).drop(["macd_struct", "bbands"]) # Generate trading signals signals = signals.with_columns([ # Bullish signal: SMA crossover + RSI not overbought + MACD positive ( (pl.col("sma_10") > pl.col("sma_20")) & (pl.col("sma_10").shift(1) <= pl.col("sma_20").shift(1)) & (pl.col("rsi") < 70) & (pl.col("macd") > pl.col("macd_signal")) ).alias("buy_signal"), # Bearish signal: SMA crossunder + RSI not oversold + MACD negative ( (pl.col("sma_10") < pl.col("sma_20")) & (pl.col("sma_10").shift(1) >= pl.col("sma_20").shift(1)) & (pl.col("rsi") > 30) & (pl.col("macd") < pl.col("macd_signal")) ).alias("sell_signal"), ]) # Filter to signal rows buy_signals = signals.filter(pl.col("buy_signal")) sell_signals = signals.filter(pl.col("sell_signal")) print("Buy Signals:") print(buy_signals.select(["timestamp", "close", "rsi", "macd", "sma_10", "sma_20"])) print("\nSell Signals:") print(sell_signals.select(["timestamp", "close", "rsi", "macd", "sma_10", "sma_20"])) ``` -------------------------------- ### Calculate Hilbert Transform Indicators in Polars Source: https://context7.com/yvictor/polars_ta_extension/llms.txt This Python code calculates various Hilbert Transform indicators (Dominant Cycle Period, Trend vs Cycle Mode, Sine Wave, Phasor Components) using the polars_talib library. It handles indicators that return single values, structs, and demonstrates extracting specific fields from struct outputs. The input is a Polars DataFrame with a 'close' column. ```python import polars as pl import polars_talib as plta df = pl.DataFrame({ "close": [100.0, 102.0, 101.0, 103.0, 105.0, 104.0, 106.0, 108.0, 107.0, 109.0, 110.0, 112.0, 111.0, 113.0, 115.0, 114.0, 116.0, 118.0, 117.0, 119.0] * 3 }) # Hilbert Transform - Dominant Cycle Period result = df.with_columns( plta.ht_dcperiod(pl.col("close")).alias("ht_dcperiod") ) # Hilbert Transform - Trend vs Cycle Mode result = df.with_columns( plta.ht_trendmode(pl.col("close")).alias("ht_trendmode") ) # Hilbert Transform - Sine Wave (returns struct) result = df.with_columns( plta.ht_sine(pl.col("close")).alias("ht_sine_struct") ).with_columns( pl.col("ht_sine_struct").struct.field("sine").alias("sine"), pl.col("ht_sine_struct").struct.field("leadsine").alias("leadsine") ).drop("ht_sine_struct") # Hilbert Transform - Phasor Components result = df.with_columns( plta.ht_phasor().alias("phasor") ).with_columns( pl.col("phasor").struct.field("inphase").alias("inphase"), pl.col("phasor").struct.field("quadrature").alias("quadrature") ).drop("phasor") print(result) ``` -------------------------------- ### Multi-Symbol Analysis with .over() in Polars TA Source: https://context7.com/yvictor/polars_ta_extension/llms.txt Demonstrates performing technical analysis calculations on data containing multiple symbols. Although the code snippet itself does not contain TA calculations, it sets up a DataFrame structured for such analysis, implying that subsequent TA functions would be applied using `.over('symbol')` to compute indicators independently for each symbol. ```python import polars as pl import polars_talib as plta # Data with multiple symbols df = pl.DataFrame({ "symbol": ["AAPL"] * 10 + ["GOOGL"] * 10 + ["MSFT"] * 10, "timestamp": list(range(10)) * 3, "close": [100, 102, 101, 103, 105, 104, 106, 108, 107, 109] * 3, "high": [103, 104, 103, 106, 107, 106, 108, 110, 109, 111] * 3, "low": [99, 101, 100, 102, 104, 103, 105, 107, 106, 108] * 3, "volume": [1000, 1100, 900, 1200, 1300, 1150, 1250, 1350, 1400, 1500] * 3 }) ``` -------------------------------- ### Calculate Stochastic Oscillator using Polars TA Extension Source: https://context7.com/yvictor/polars_ta_extension/llms.txt Calculates the Stochastic Oscillator, which includes the %K (fast or slow) and %D lines. This indicator compares a particular closing price of a security to a range of its prices over a certain period. It is used to generate overbought and oversold signals. ```python import polars as pl import polars_talib as plta df = pl.DataFrame({ "high": [103.0, 104.0, 105.0, 106.0, 107.0, 108.0, 109.0, 110.0, 111.0, 112.0] * 2, "low": [99.0, 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0] * 2, "close": [102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, 109.0, 110.0, 111.0] * 2 }) # Stochastic returns %K and %D lines in a struct result = df.with_columns( plta.stoch( pl.col("high"), pl.col("low"), pl.col("close"), fastk_period=14, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0 ).alias("stoch") ).with_columns( pl.col("stoch").struct.field("slowk").alias("stoch_k"), pl.col("stoch").struct.field("slowd").alias("stoch_d") ).drop("stoch") print(result) # %K and %D oscillate between 0 and 100 ``` -------------------------------- ### Calculate Technical Indicators with Polars TA Extension (Python) Source: https://github.com/yvictor/polars_ta_extension/blob/master/examples/basic.ipynb This Python code snippet demonstrates how to use the Polars TA extension to calculate multiple technical indicators including SMA, MACD, Stochastic Oscillator, and Weighted Close Price. It applies these indicators over different 'Symbol' groups and then extracts specific fields from the MACD and Stochastic outputs. Finally, it filters the data for a specific symbol ('AAPL') and collects the results. ```python 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() ``` -------------------------------- ### Calculate Technical Indicators on Single Symbol DataFrames in Polars Source: https://github.com/yvictor/polars_ta_extension/blob/master/README.md Demonstrates how to apply various TA-Lib indicators (EMA, MACD, CDL2Crows, WCLPRICE) to a Polars DataFrame for a single symbol. It utilizes the `.ta` accessor provided by polars_talib. ```python 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"), ) ```