PyO3 is the Rust library that lets you write Python extension modules in Rust, so a trader can keep strategy code, data handling and broker calls in Python while the hot loops over ticks, indicators or Monte Carlo paths run as compiled Rust. Below I explain what a thin PyO3 wrapper is, where the speedup comes from, a small working example you can build in a few minutes, and when it is not worth the effort.
Key Takeaways
- PyO3 plus the maturin build tool turns a Rust function into an importable Python module or a pip-installable wheel.
- Speedups come from removing interpreter overhead, releasing the GIL for multi-core work and cache-friendly memory layout.
- Rewrite only the profiled hot path (often a small fraction of the code); keep everything else in Python.
- In my test, a rolling z-score over 1 million prices ran about 15x faster through PyO3 than the same loop in pure Python, even including the cost of copying the list into Rust.
- It is overkill when the bottleneck is the broker API, the network or the database.
What is a thin PyO3 wrapper?
A thin PyO3 wrapper is a small layer of Python-friendly code sitting on top of high-performance Rust functions, letting you call Rust from Python as if it were a native library, with near-zero overhead at the boundary.

Think of it like this. You write your strategy logic, dataframes, broker calls, and dashboards in Python because that ecosystem (pandas, NumPy, FastAPI, Streamlit, broker SDKs) is unbeatable for traders. But the hot loops, the parts that crunch millions of ticks, compute indicators bar by bar, or run Monte Carlo simulations, get rewritten in Rust. PyO3 is the bridge. The “thin” part means the wrapper does almost nothing except marshal data in and results out. All the real work happens in compiled, memory-safe, parallel-capable Rust.
For a trader, the practical translation is: you keep your Python workflow, but specific bottlenecks run 10x to 100x faster.
Pure Python vs Python + PyO3 + Rust
Pure Python is interpreted. Every line is read, parsed, and executed by the CPython interpreter at runtime. Variables carry type information that gets checked dynamically. Loops over a million ticks mean a million trips through the interpreter, each with object allocation, reference counting, and the Global Interpreter Lock (GIL) blocking true multi-threading.
Rust, in contrast, is compiled ahead of time into native machine code. Types are known at compile time, memory layout is predictable, there is no garbage collector pausing your backtest, and you can release the GIL to genuinely parallelize across CPU cores.
Here is the conceptual difference for a trader running a simple rolling z-score over 10 million ticks.
| Aspect | Pure Python | Python + PyO3 + Rust |
|---|---|---|
| Execution | Interpreted line by line | Compiled native code |
| Typical speedup on numeric loops | 1x (baseline) | 20x to 100x |
| Multi-core usage | Blocked by GIL | True parallelism via Rayon |
| Memory safety | Runtime errors possible | Compile-time guarantees |
| Distribution | Just pip install | pip install a precompiled wheel |
| Development speed | Very fast | Slower, but only for the hot path |
| Debugging | Easy, interactive | Harder, needs Rust tooling |
The key insight: you do not rewrite your whole platform in Rust. You profile, find the 5 percent of code consuming 80 percent of runtime, and replace only that with a PyO3 module. Everything else stays Python.
Concretely, libraries traders already touch follow this pattern. Polars (the pandas alternative) is Rust with a PyO3 wrapper. Pydantic v2 rewrote its validation core in Rust via PyO3 and got dramatically faster. Tokenizers from Hugging Face do the same. The pattern is proven.
A Minimal PyO3 Example for Traders: Rolling Z-Score
Here is a complete, small PyO3 module that computes a rolling z-score of a price series, the example used in the table above. I built and tested it with PyO3 0.30 and maturin 1.16 on Python 3.13. You need the Rust toolchain (from rustup) and maturin (pip install maturin).
1. Cargo.toml
[package]
name = "fastz"
version = "0.1.0"
edition = "2021"
[lib]
name = "fastz"
crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.30", features = ["extension-module"] }
2. src/lib.rs
use pyo3::prelude::*;
/// Rolling z-score of a price series: (x - mean) / std over `window` values.
#[pyfunction]
fn rolling_zscore(py: Python<'_>, prices: Vec<f64>, window: usize) -> Vec<f64> {
// Release the GIL while Rust does the number crunching
py.detach(|| {
let n = prices.len();
let mut out = vec![f64::NAN; n];
if window < 2 || window > n {
return out;
}
let (mut sum, mut sum_sq) = (0.0, 0.0);
for i in 0..n {
let x = prices[i];
sum += x;
sum_sq += x * x;
if i >= window {
let old = prices[i - window];
sum -= old;
sum_sq -= old * old;
}
if i + 1 >= window {
let w = window as f64;
let mean = sum / w;
let var = (sum_sq / w - mean * mean).max(0.0);
let std = var.sqrt();
out[i] = if std > 0.0 { (x - mean) / std } else { 0.0 };
}
}
out
})
}
#[pymodule]
fn fastz(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(rolling_zscore, m)?)?;
Ok(())
}
3. Build and Call It from Python
# inside an activated virtual environment, in the project folder
pip install maturin
maturin develop --release
python -c "from fastz import rolling_zscore; print(rolling_zscore([100, 101, 102, 101, 103, 105], 3))"
maturin develop compiles the crate and installs it into the active virtual environment; maturin build --release produces a wheel you can share or install on another machine. On my 2-core test machine, 1 million prices with a 50-period window took 0.86 seconds as a pure Python loop and 0.06 seconds through PyO3, with identical results. Most of the remaining time is spent converting the Python list into a Rust Vec; passing NumPy arrays through the numpy crate avoids that copy and widens the gap further.
Two version notes, since PyO3 moves quickly. PyO3 0.26 (August 2025) renamed Python::allow_threads to Python::detach and Python::with_gil to Python::attach, so older tutorials using the old names will not compile against current versions. And the module function now takes &Bound<'_, PyModule>, which is what the PyO3 user guide and maturin docs show today.
How does the speedup actually happen?
Three things stack together.
First, no interpreter overhead. A Rust loop iterating 10 million times executes as raw CPU instructions. A Python loop iterating 10 million times executes 10 million interpreter cycles, each one orders of magnitude slower.
Second, GIL release. PyO3 lets your Rust function declare py.detach(...) (called py.allow_threads(...) before PyO3 0.26) so while it crunches numbers, the Python GIL is released. Other Python threads can run. Your Rust code itself can use Rayon to fan out across all your CPU cores. A 16-core machine actually behaves like a 16-core machine, not a glorified single-core one.
Third, memory layout. Rust gives you contiguous arrays, cache-friendly structs, and SIMD-friendly numeric operations. Python lists of floats are actually lists of pointers to boxed float objects scattered across the heap. The CPU cache hates that.
When you stack interpreter elimination, parallelism, and cache-friendly memory together, the 50x to 100x numbers you see in Polars benchmarks stop sounding like marketing.
Use cases that matter for traders and investors
Tick-level backtesting. If you have years of NSE F&O tick data and want to backtest an intraday strategy bar by bar, pure Python with for-loops is painful. Vectorized pandas helps but breaks down for path-dependent logic like trailing stops, partial fills, or order book reconstruction. A PyO3 module that takes a NumPy array of ticks and runs your event loop in Rust can compress an overnight backtest into minutes.
Indicator libraries. Writing custom indicators in Python is fine for one symbol, one timeframe. Running 200 indicators across 5000 symbols on 1-minute bars for a screener is where pure Python collapses. A Rust core exposed through PyO3 handles this comfortably. This is essentially what powers Polars expressions and what TA-Lib achieves with its C core.
Options pricing and Greeks. Black-Scholes is a closed form, fast even in Python. But American option pricing via binomial trees, Monte Carlo for exotic payoffs, or computing full Greeks surfaces across an option chain every few seconds during market hours is heavy. Rust with parallel Monte Carlo paths via Rayon is a natural fit. For an options-focused platform like OpenBull, this is exactly the kind of hot path worth isolating.
Order book reconstruction and microstructure analytics. Rebuilding a limit order book from L2 updates, computing imbalance, volume-weighted spreads, and tick rule classifications across millions of messages benefits enormously from Rust. Python can drive the analysis, Rust does the heavy lifting.
Real-time risk and portfolio analytics. VaR, expected shortfall, correlation matrices across hundreds of positions, scenario analysis with thousands of paths. The math is straightforward, the volume is the problem. PyO3 lets a FastAPI endpoint compute portfolio risk in milliseconds instead of seconds.
Feature engineering for ML strategies. Generating thousands of rolling features (z-scores, ranks, momentum windows, autocorrelations) across many symbols and lookbacks is embarrassingly parallel. A Rust core called from Python notebooks turns a coffee-break wait into an instant result.
Multi-account trade orchestration. Platforms that mirror trades across many accounts (the AlgoMirror pattern) have a hot path in the routing and reconciliation loop. Rust can handle the high-frequency reconciliation while Python handles broker SDKs, UI, and configuration.
Where it is overkill
Be honest about this. PyO3 is not free. You take on Rust as a build dependency, a learning curve, and a slower iteration loop. It is the wrong choice when your bottleneck is the network (broker API latency), the database (slow queries on unindexed tables), or your own logic (a strategy that calls pandas inefficiently in a Python loop when a vectorized version would do).
Before You Reach for PyO3: A Quick Checklist
- Profile first with
cProfileorpy-spyand confirm the time is in a CPU-bound Python loop. - Try vectorising with NumPy or Polars. Many indicator loops do not need custom code at all.
- Check existing libraries. TA-Lib, Polars and Nautilus Trader already have compiled cores.
- Only then move the remaining path-dependent loop (trailing stops, order book updates, event loops) to Rust behind a small PyO3 function.
- Keep the interface narrow: arrays in, arrays out. The thinner the wrapper, the easier it is to test against the Python version.
This is the same reasoning behind why we built OpenAlgo in Python rather than Go or Rust: the platform layer stays in Python for reach and ease of contribution, and only measured hot paths deserve a compiled language. Nautilus Trader is an example of the other end, a full trading engine with a Rust core and a Python API.
Profile first. If your backtest spends 90 percent of its time waiting on broker API responses, no amount of Rust will help. If it spends 90 percent in a tight Python loop computing indicators, PyO3 is exactly the right tool.
A reasonable mental model
Treat PyO3 the way you treat your broker’s colocation server. You do not move everything there. You move only the parts where latency or throughput is the actual constraint. Python remains the language you reason in, share strategies in, and ship dashboards in. Rust becomes the engine room you visit only when the speedometer demands it.
For most retail and prosumer trading platforms, the right architecture is Python everywhere by default, with one or two carefully chosen Rust crates wrapped in PyO3 handling the genuinely hot paths. Polars for dataframes, a custom indicator crate for your specific feature library, maybe a backtest engine crate if your strategies are path-dependent. That is the sweet spot.
Frequently Asked Questions
What is PyO3 used for?
PyO3 provides Rust bindings for Python. It is used to write Python extension modules in Rust, and to embed Python in Rust programs. Polars, Pydantic v2 and Hugging Face Tokenizers are well-known libraries built this way.
Is Rust faster than Python for trading backtests?
For CPU-bound, path-dependent loops, yes, often by 10x to 100x. For vectorised NumPy code or workloads dominated by API and network latency, the difference is small.
What is maturin?
Maturin is the build tool for PyO3 projects. It compiles the Rust crate and packages it as a Python wheel, or installs it directly into a virtual environment with maturin develop.
Do I need to learn Rust to use PyO3-based libraries?
No. Libraries such as Polars are installed with pip and used from Python like any other package. You only need Rust if you want to write your own extension module.
Learn by watching: free videos every week
Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.

