Skip to main content

PyPI version Python versions CI Docs License Code style: ruff

QTrade

QTrade is a small, modular Python library for backtesting trading strategies and training reinforcement learning agents on financial data.

Features

  • Single-asset and portfolio backtesting: pass one DataFrame for single-asset, or a dict[str, DataFrame] for multi-asset / portfolio strategies sharing one cash pool.
  • Walk-forward optimization: rolling-window training + immediate out-of-sample test, the standard antidote to in-sample overfitting.
  • RL trading environment: a Gymnasium TradingEnv with pluggable Action / Observation / Reward schemes, ready to drop into stable-baselines3 and friends.
  • Bokeh reports: equity, drawdown, per-asset OHLC panels, trade scatter — saved as a single self-contained HTML.
  • Curated metrics: Sharpe, Sortino, Calmar, Omega, max drawdown duration, win rate, profit factor, etc.

See COMPARISON.md for an honest take on where QTrade fits next to backtrader / vectorbt / Backtesting.py.

Installation

pip install qtrade-lib

Optional extras:

pip install "qtrade-lib[rl]"   # adds stable-baselines3 for RL evaluation utils

To work from source:

git clone https://github.com/gguan/qtrade.git
cd qtrade
pip install -e ".[dev]"
pre-commit install
pytest

Quickstart

import yfinance as yf
from qtrade.backtest import Strategy, Backtest

class SmaCrossover(Strategy):
    n1 = 5
    n2 = 20

    def prepare(self):
        for df in self._data.values():
            df['sma1'] = df['Close'].rolling(self.n1).mean()
            df['sma2'] = df['Close'].rolling(self.n2).mean()

    def on_bar_close(self):
        s1, s2 = self.data['sma1'], self.data['sma2']
        if s1.iloc[-2] < s2.iloc[-2] and s1.iloc[-1] > s2.iloc[-1]:
            self.buy()
        elif s1.iloc[-2] > s2.iloc[-2] and s1.iloc[-1] < s2.iloc[-1]:
            self.close()

data = yf.download("GC=F", start="2023-01-01", end="2024-01-01",
                   interval="1d", multi_level_index=False)

bt = Backtest(data, SmaCrossover, cash=10_000, trade_on_close=True)
bt.run()
bt.show_stats()
bt.plot()

For a multi-asset / walk-forward example, see examples/portfolio_strategy.py.

Documentation

Requirements

  • Python ≥ 3.10
  • Runtime dependencies declared in pyproject.toml (numpy, pandas, scipy, matplotlib, bokeh, gymnasium, mplfinance, tqdm).

References

This project is inspired by:

License

MIT — see LICENSE.

Metadata

Release files for qtrade-lib 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for qtrade-lib 0.5.0
File Size Uploaded Provenance Actions
qtrade_lib-0.5.0.tar.gz 7.3 MB With provenance Details for qtrade_lib-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for qtrade-lib 0.5.0
File Interpreter ABI Platform Actions
qtrade_lib-0.5.0-py3-none-any.whl Python 3 none any Details for qtrade_lib-0.5.0-py3-none-any.whl

Total release size: 7.3 MB

Release files / qtrade_lib-0.5.0.tar.gz

Download URL qtrade_lib-0.5.0.tar.gz
Size 7.3 MB
Tags Source
SHA-256 checksum
How to use checksums
75d32e8c7df0721a6edd062777fb4de2ae4e7c122849c493fcb02d38c7f16d44
BLAKE2b-256 checksum
How to use checksums
df9162ffa57201df7e8df57ffbfc7dea2a67d93e48d5af18bfffa2cf34894443
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 8, 2026.

Transparency log

Release files / qtrade_lib-0.5.0-py3-none-any.whl

Download URL qtrade_lib-0.5.0-py3-none-any.whl
Size 45.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
368ae187ee824c5cf821801e7d45cfb315d17a38277b99cc206f494cfa6e3229
BLAKE2b-256 checksum
How to use checksums
b963886c61aea474ac5d39ffd03c4ecefe3e505e2639ec89373e0438394c3ae7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 8, 2026.

Transparency log

Security for qtrade-lib 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for qtrade_lib-0.5.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 8, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page