Compute Daily Returns in Polars — Finance Tutorial

Codegiz — Built by Claude AI · Beginner ·🛠️ AI Tools & Apps ·2mo ago

About this lesson

Daily returns are the language of finance. Every alpha model, every risk metric, every backtest opens with the same operation — take the close column, divide today by yesterday, subtract one. Trivial on a single ticker. The interesting question is what happens when fourteen tickers stack in the same frame. Source code: https://github.com/GoCelesteAI/polars-for-finance The answer in Polars is the .over clause — a window function that runs any expression per group while keeping the result aligned to the original row order. pl.col Close pct_change over Ticker computes six years of daily returns for fourteen tickers in one line, in parallel, with a query optimizer behind it. SQL window function semantics, Python syntax, Rust execution. This is the episode where the Polars expression API meets the bookkeeping work that powers every finance pipeline you will ever write. What You'll Build: - returns.py — sort the cached prices by Ticker and Date, then add daily_ret and log_ret columns in a single with_columns call. Fourteen-ticker frame, twenty eight thousand rows, two new columns computed in parallel. - The .over Ticker window pattern — append .over to any expression and Polars partitions, runs, and stitches the result back into the original frame. Same shape as SQL OVER PARTITION BY. - The log return variant — pl.col Close .log minus pl.col Close .log .shift one .over Ticker. The version you want for cumulative math because log returns add across periods. - The proof: count nulls in daily_ret. Fourteen nulls means the window ran per group. One null means you forgot the .over clause and the returns are silently garbage at every ticker boundary. - The .over composition pattern — rolling means, cumulative max for drawdown, ranks within ticker. Same shape, different inner expression. The window clause unlocks every windowed operation in the library. Timestamps: 0:00 - Intro — returns, the language of finance 0:18 - Preview — pct_change in one line for 14 tickers 1:00 -

Original Description

Daily returns are the language of finance. Every alpha model, every risk metric, every backtest opens with the same operation — take the close column, divide today by yesterday, subtract one. Trivial on a single ticker. The interesting question is what happens when fourteen tickers stack in the same frame. Source code: https://github.com/GoCelesteAI/polars-for-finance The answer in Polars is the .over clause — a window function that runs any expression per group while keeping the result aligned to the original row order. pl.col Close pct_change over Ticker computes six years of daily returns for fourteen tickers in one line, in parallel, with a query optimizer behind it. SQL window function semantics, Python syntax, Rust execution. This is the episode where the Polars expression API meets the bookkeeping work that powers every finance pipeline you will ever write. What You'll Build: - returns.py — sort the cached prices by Ticker and Date, then add daily_ret and log_ret columns in a single with_columns call. Fourteen-ticker frame, twenty eight thousand rows, two new columns computed in parallel. - The .over Ticker window pattern — append .over to any expression and Polars partitions, runs, and stitches the result back into the original frame. Same shape as SQL OVER PARTITION BY. - The log return variant — pl.col Close .log minus pl.col Close .log .shift one .over Ticker. The version you want for cumulative math because log returns add across periods. - The proof: count nulls in daily_ret. Fourteen nulls means the window ran per group. One null means you forgot the .over clause and the returns are silently garbage at every ticker boundary. - The .over composition pattern — rolling means, cumulative max for drawdown, ranks within ticker. Same shape, different inner expression. The window clause unlocks every windowed operation in the library. Timestamps: 0:00 - Intro — returns, the language of finance 0:18 - Preview — pct_change in one line for 14 tickers 1:00 -
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

Up next
ChatGPT and yelp just made a deal
Matt Diamante
Watch →