A Python library for backtesting trading strategies and applying reinforcement learning to trading.
Project description
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
TradingEnvwith pluggable Action / Observation / Reward schemes, ready to drop intostable-baselines3and 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
- User Guide
- Multi-asset / portfolio backtests
- RL trading environment
- API reference
- Releasing
- Changelog
Requirements
- Python ≥ 3.10
- Runtime dependencies declared in
pyproject.toml(numpy, pandas, scipy, matplotlib, bokeh, gymnasium, mplfinance, tqdm).
References
This project is inspired by:
- backtesting.py — single-asset backtest API design
- tensortrade — RL trading framework
License
MIT — see LICENSE.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file qtrade_lib-0.5.0.tar.gz.
File metadata
- Download URL: qtrade_lib-0.5.0.tar.gz
- Upload date:
- Size: 7.3 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75d32e8c7df0721a6edd062777fb4de2ae4e7c122849c493fcb02d38c7f16d44
|
|
| MD5 |
e87cc7a5129f211c09eecaeb5f2323bd
|
|
| BLAKE2b-256 |
df9162ffa57201df7e8df57ffbfc7dea2a67d93e48d5af18bfffa2cf34894443
|
Provenance
The following attestation bundles were made for qtrade_lib-0.5.0.tar.gz:
Publisher:
release.yml on gguan/qtrade
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
qtrade_lib-0.5.0.tar.gz -
Subject digest:
75d32e8c7df0721a6edd062777fb4de2ae4e7c122849c493fcb02d38c7f16d44 - Sigstore transparency entry: 1470863729
- Sigstore integration time:
-
Permalink:
gguan/qtrade@6988520a1044a8181d0c6b3f14f121a04437fe9d -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/gguan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@6988520a1044a8181d0c6b3f14f121a04437fe9d -
Trigger Event:
push
-
Statement type:
File details
Details for the file qtrade_lib-0.5.0-py3-none-any.whl.
File metadata
- Download URL: qtrade_lib-0.5.0-py3-none-any.whl
- Upload date:
- Size: 45.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
368ae187ee824c5cf821801e7d45cfb315d17a38277b99cc206f494cfa6e3229
|
|
| MD5 |
7fdcda5c98cdc974bbd28eece7e6f6c3
|
|
| BLAKE2b-256 |
b963886c61aea474ac5d39ffd03c4ecefe3e505e2639ec89373e0438394c3ae7
|
Provenance
The following attestation bundles were made for qtrade_lib-0.5.0-py3-none-any.whl:
Publisher:
release.yml on gguan/qtrade
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
qtrade_lib-0.5.0-py3-none-any.whl -
Subject digest:
368ae187ee824c5cf821801e7d45cfb315d17a38277b99cc206f494cfa6e3229 - Sigstore transparency entry: 1470863848
- Sigstore integration time:
-
Permalink:
gguan/qtrade@6988520a1044a8181d0c6b3f14f121a04437fe9d -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/gguan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@6988520a1044a8181d0c6b3f14f121a04437fe9d -
Trigger Event:
push
-
Statement type: