<div align="center">
<img src="static/images/systematic-trading.jpeg" height=200 alt=""/>
<h1>Papers With Backtest Toolbox</h1>
</div>
The `pwb-toolbox` package is designed to provide tools and resources for systematic trading strategies. It includes datasets and strategy ideas to assist in developing and backtesting trading algorithms. For detailed instructions on how to use this package effectively, please refer to the associated Substack publication by visiting: https://blog.paperswithbacktest.com/.
## Installation
To install the pwb-toolbox package:
```bash
pip install pwb-toolbox
```
This package requires Python 3.10 or higher.
To login to Huggingface Hub with Access Token
```bash
huggingface-cli login
```
## Usage
The `pwb-toolbox` package offers a range of functionalities for systematic trading analysis. Here are some examples of how to utilize the package:
- Import `pwb_toolbox.datasets` and sequentially loads datasets for different asset classes, such as bonds, commodities, cryptocurrencies, ETFs, forex, indices, and stocks, using the `load_dataset` function:
```python
import pwb_toolbox.datasets as pwb_ds
df = pwb_ds.get_pricing(["AAPL", "MSFT", "GOOGL"])
df = pwb_ds.load_dataset("Bonds-Daily-Price")
df = pwb_ds.load_dataset("Commodities-Daily-Price")
df = pwb_ds.load_dataset("Cryptocurrencies-Daily-Price")
df = pwb_ds.load_dataset("ETFs-Daily-Price")
df = pwb_ds.load_dataset("Forex-Daily-Price")
df = pwb_ds.load_dataset("Indices-Daily-Price")
df = pwb_ds.load_dataset("Stocks-Daily-Price")
```
- Load daily stock price data for specific symbols using the load_dataset function. The first call retrieves data for Apple and Microsoft. The second call retrieves the same stocks but without price adjustments (`adjust=False`). The third call loads daily price data for the S&P 500 index:
```python
import pwb_toolbox.datasets as pwb_ds
df = pwb_ds.load_dataset(
"Stocks-Daily-Price",
["AAPL", "MSFT"],
)
df = pwb_ds.load_dataset(
"Stocks-Daily-Price",
["AAPL", "MSFT"],
adjust=False,
)
df = pwb_ds.load_dataset(
"Stocks-Daily-Price",
["sp500"],
)
```
- The `extend=True` argument instructs the function to return an extended historical data using indices, commodities, and bonds data.
```python
import pwb_toolbox.datasets as pwb_ds
df = pwb_ds.load_dataset(
"ETFs-Daily-Price",
["SPY", "IEF"],
extend=True,
)
```
- The argument `rate_to_price=False` specifies that bond yield rates should not be converted to price values in the returned data:
```python
import pwb_toolbox.datasets as pwb_ds
df = pwb_ds.load_dataset(
"Bonds-Daily-Price",
["US10Y"],
rate_to_price=False,
)
```
- The argument `to_usd=False` indicates that the data should not be converted to U.S. dollars, implying that it might be available in another currency.
```python
import pwb_toolbox.datasets as pwb_ds
df = pwb_ds.load_dataset(
"Indices-Daily-Price",
["US10Y"],
to_usd=False,
)
```
## Backtest engine
The `pwb_toolbox.backtest` module offers simple building blocks for running
Backtrader simulations. Alpha models generate `Insight` objects which are turned
into portfolio weights and executed via Backtrader orders.
```python
from pwb_toolbox.backtest.examples import GoldenCrossAlpha, EqualWeightPortfolio
from pwb_toolbox.backtest import run_backtest
from pwb_toolbox.backtest.execution_models import ImmediateExecutionModel
from pwb_toolbox.backtest.risk_models import MaximumTotalPortfolioExposure
from pwb_toolbox.backtest.universe_models import ManualUniverseSelectionModel
run_backtest(
ManualUniverseSelectionModel(["SPY", "QQQ"]),
GoldenCrossAlpha(),
EqualWeightPortfolio(),
execution=ImmediateExecutionModel(),
risk=MaximumTotalPortfolioExposure(max_exposure=1.0),
start="2015-01-01",
)
```
## Performance Analysis
After running a backtest you can analyze the returned equity series using the
`pwb_toolbox.performance` module.
```python
from pwb_toolbox.backtest.examples import GoldenCrossAlpha, EqualWeightPortfolio
from pwb_toolbox.backtest import run_backtest
from pwb_toolbox.backtest.execution_models import ImmediateExecutionModel
from pwb_toolbox.performance import total_return, cagr
from pwb_toolbox.performance.plots import plot_equity_curve
result, equity = run_backtest(
ManualUniverseSelectionModel(["SPY", "QQQ"]),
GoldenCrossAlpha(),
EqualWeightPortfolio(),
execution=ImmediateExecutionModel(),
start="2015-01-01",
)
print("Total return:", total_return(equity))
print("CAGR:", cagr(equity))
plot_equity_curve(equity)
```
Plotting utilities require `matplotlib`; some metrics also need `pandas`.
## Live trading with Interactive Brokers
`run_ib_strategy` streams Interactive Brokers data and orders. Install `ibapi` and either `atreyu-backtrader-api` or `ib_insync`.
```python
from pwb_toolbox.backtest import IBConnector, run_ib_strategy
from pwb_toolbox.backtest.example.engine import SimpleIBStrategy
data_cfg = [{"dataname": "AAPL", "name": "AAPL"}]
run_ib_strategy(
SimpleIBStrategy,
data_cfg,
host="127.0.0.1",
port=7497,
client_id=1,
)
```
Configure `host`, `port`, and `client_id` to match your TWS or Gateway settings. Test with an Interactive Brokers paper account before trading live.
## Contributing
Contributions to the `pwb-toolbox` package are welcome! If you have any improvements, new datasets, or strategy ideas to share, please follow these guidelines:
1. Fork the repository and create a new branch for your feature.
2. Make your changes and ensure they adhere to the package's coding style.
3. Write tests to validate the functionality or provide sample usage examples.
4. Submit a pull request, clearly explaining the purpose and benefits of your contribution.
Please note that all contributions are subject to review and approval by the maintainers.
## Build the Package
To build the package, run:
```bash
python -m pip install --upgrade build
rm -r dist
python -m build
```
To upload the package to PyPI, run:
```bash
twine upload dist/*
```
## License
The `pwb-toolbox` package is released under the MIT license. See the LICENSE file for more details.
## Contact
For any questions, issues, or suggestions regarding the `pwb-toolbox` package, please contact the maintainers or create an issue on the repository. We appreciate your feedback and involvement in improving the package.
Happy trading!
Raw data
{
"_id": null,
"home_page": "https://github.com/paperswithbacktest/pwb-toolbox",
"name": "pwb-toolbox",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": null,
"author": "Your Name",
"author_email": "hello@paperswithbacktest.com",
"download_url": "https://files.pythonhosted.org/packages/17/4d/ad7764f659d98d5ea9b9e9a2b9e22632c29e05e75716e6c9ed4735f5a68f/pwb_toolbox-0.1.8.tar.gz",
"platform": null,
"description": "<div align=\"center\">\n <img src=\"static/images/systematic-trading.jpeg\" height=200 alt=\"\"/>\n <h1>Papers With Backtest Toolbox</h1>\n</div>\n\nThe `pwb-toolbox` package is designed to provide tools and resources for systematic trading strategies. It includes datasets and strategy ideas to assist in developing and backtesting trading algorithms. For detailed instructions on how to use this package effectively, please refer to the associated Substack publication by visiting: https://blog.paperswithbacktest.com/.\n\n\n## Installation\n\nTo install the pwb-toolbox package:\n\n```bash\npip install pwb-toolbox\n```\nThis package requires Python 3.10 or higher.\n\nTo login to Huggingface Hub with Access Token\n\n```bash\nhuggingface-cli login\n```\n\n## Usage\n\nThe `pwb-toolbox` package offers a range of functionalities for systematic trading analysis. Here are some examples of how to utilize the package:\n\n- Import `pwb_toolbox.datasets` and sequentially loads datasets for different asset classes, such as bonds, commodities, cryptocurrencies, ETFs, forex, indices, and stocks, using the `load_dataset` function:\n\n```python\nimport pwb_toolbox.datasets as pwb_ds\n\ndf = pwb_ds.get_pricing([\"AAPL\", \"MSFT\", \"GOOGL\"])\ndf = pwb_ds.load_dataset(\"Bonds-Daily-Price\")\ndf = pwb_ds.load_dataset(\"Commodities-Daily-Price\")\ndf = pwb_ds.load_dataset(\"Cryptocurrencies-Daily-Price\")\ndf = pwb_ds.load_dataset(\"ETFs-Daily-Price\")\ndf = pwb_ds.load_dataset(\"Forex-Daily-Price\")\ndf = pwb_ds.load_dataset(\"Indices-Daily-Price\")\ndf = pwb_ds.load_dataset(\"Stocks-Daily-Price\")\n\n```\n\n- Load daily stock price data for specific symbols using the load_dataset function. The first call retrieves data for Apple and Microsoft. The second call retrieves the same stocks but without price adjustments (`adjust=False`). The third call loads daily price data for the S&P 500 index:\n\n```python\nimport pwb_toolbox.datasets as pwb_ds\n\ndf = pwb_ds.load_dataset(\n \"Stocks-Daily-Price\",\n [\"AAPL\", \"MSFT\"],\n)\n\ndf = pwb_ds.load_dataset(\n \"Stocks-Daily-Price\",\n [\"AAPL\", \"MSFT\"],\n adjust=False,\n)\n\ndf = pwb_ds.load_dataset(\n \"Stocks-Daily-Price\",\n [\"sp500\"],\n)\n```\n\n- The `extend=True` argument instructs the function to return an extended historical data using indices, commodities, and bonds data.\n\n```python\nimport pwb_toolbox.datasets as pwb_ds\n\ndf = pwb_ds.load_dataset(\n \"ETFs-Daily-Price\",\n [\"SPY\", \"IEF\"],\n extend=True,\n)\n```\n\n- The argument `rate_to_price=False` specifies that bond yield rates should not be converted to price values in the returned data:\n\n```python\nimport pwb_toolbox.datasets as pwb_ds\n\ndf = pwb_ds.load_dataset(\n \"Bonds-Daily-Price\",\n [\"US10Y\"],\n rate_to_price=False,\n)\n```\n\n- The argument `to_usd=False` indicates that the data should not be converted to U.S. dollars, implying that it might be available in another currency.\n\n```python\nimport pwb_toolbox.datasets as pwb_ds\n\ndf = pwb_ds.load_dataset(\n \"Indices-Daily-Price\",\n [\"US10Y\"],\n to_usd=False,\n)\n```\n\n## Backtest engine\n\nThe `pwb_toolbox.backtest` module offers simple building blocks for running\nBacktrader simulations. Alpha models generate `Insight` objects which are turned\ninto portfolio weights and executed via Backtrader orders.\n\n```python\nfrom pwb_toolbox.backtest.examples import GoldenCrossAlpha, EqualWeightPortfolio\nfrom pwb_toolbox.backtest import run_backtest\nfrom pwb_toolbox.backtest.execution_models import ImmediateExecutionModel\nfrom pwb_toolbox.backtest.risk_models import MaximumTotalPortfolioExposure\nfrom pwb_toolbox.backtest.universe_models import ManualUniverseSelectionModel\n\nrun_backtest(\n ManualUniverseSelectionModel([\"SPY\", \"QQQ\"]),\n GoldenCrossAlpha(),\n EqualWeightPortfolio(),\n execution=ImmediateExecutionModel(),\n risk=MaximumTotalPortfolioExposure(max_exposure=1.0),\n start=\"2015-01-01\",\n)\n```\n\n## Performance Analysis\n\nAfter running a backtest you can analyze the returned equity series using the\n`pwb_toolbox.performance` module.\n\n```python\nfrom pwb_toolbox.backtest.examples import GoldenCrossAlpha, EqualWeightPortfolio\nfrom pwb_toolbox.backtest import run_backtest\nfrom pwb_toolbox.backtest.execution_models import ImmediateExecutionModel\nfrom pwb_toolbox.performance import total_return, cagr\nfrom pwb_toolbox.performance.plots import plot_equity_curve\n\nresult, equity = run_backtest(\n ManualUniverseSelectionModel([\"SPY\", \"QQQ\"]),\n GoldenCrossAlpha(),\n EqualWeightPortfolio(),\n execution=ImmediateExecutionModel(),\n start=\"2015-01-01\",\n)\n\nprint(\"Total return:\", total_return(equity))\nprint(\"CAGR:\", cagr(equity))\n\nplot_equity_curve(equity)\n```\n\nPlotting utilities require `matplotlib`; some metrics also need `pandas`.\n\n## Live trading with Interactive Brokers\n\n`run_ib_strategy` streams Interactive Brokers data and orders. Install `ibapi` and either `atreyu-backtrader-api` or `ib_insync`.\n\n```python\nfrom pwb_toolbox.backtest import IBConnector, run_ib_strategy\nfrom pwb_toolbox.backtest.example.engine import SimpleIBStrategy\n\ndata_cfg = [{\"dataname\": \"AAPL\", \"name\": \"AAPL\"}]\nrun_ib_strategy(\n SimpleIBStrategy,\n data_cfg,\n host=\"127.0.0.1\",\n port=7497,\n client_id=1,\n)\n```\n\nConfigure `host`, `port`, and `client_id` to match your TWS or Gateway settings. Test with an Interactive Brokers paper account before trading live.\n\n## Contributing\n\nContributions to the `pwb-toolbox` package are welcome! If you have any improvements, new datasets, or strategy ideas to share, please follow these guidelines:\n\n1. Fork the repository and create a new branch for your feature.\n2. Make your changes and ensure they adhere to the package's coding style.\n3. Write tests to validate the functionality or provide sample usage examples.\n4. Submit a pull request, clearly explaining the purpose and benefits of your contribution.\n\nPlease note that all contributions are subject to review and approval by the maintainers.\n\n## Build the Package\n\nTo build the package, run:\n\n```bash\npython -m pip install --upgrade build\nrm -r dist\npython -m build\n```\n\nTo upload the package to PyPI, run:\n\n```bash\ntwine upload dist/*\n```\n\n## License\n\nThe `pwb-toolbox` package is released under the MIT license. See the LICENSE file for more details.\n\n## Contact\n\nFor any questions, issues, or suggestions regarding the `pwb-toolbox` package, please contact the maintainers or create an issue on the repository. We appreciate your feedback and involvement in improving the package.\nHappy trading!\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A toolbox library for quant traders",
"version": "0.1.8",
"project_urls": {
"Homepage": "https://github.com/paperswithbacktest/pwb-toolbox"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "6d89f53e06e201a2146178535ec4d1f532ecc5f6c9e4a1e58862f5451bb300f6",
"md5": "820398e1fcef1f285a982cf75475e923",
"sha256": "a21977180d6181002134f0036b270723831117fa4f4459117b49bcbf65ebeec6"
},
"downloads": -1,
"filename": "pwb_toolbox-0.1.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "820398e1fcef1f285a982cf75475e923",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 30242,
"upload_time": "2025-07-15T20:51:41",
"upload_time_iso_8601": "2025-07-15T20:51:41.873075Z",
"url": "https://files.pythonhosted.org/packages/6d/89/f53e06e201a2146178535ec4d1f532ecc5f6c9e4a1e58862f5451bb300f6/pwb_toolbox-0.1.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "174dad7764f659d98d5ea9b9e9a2b9e22632c29e05e75716e6c9ed4735f5a68f",
"md5": "083586139e3a84999dec254147e7109a",
"sha256": "f19ac06b26d09e0c9ef51a080366a867694a043b6035230ba392bd35308a28dc"
},
"downloads": -1,
"filename": "pwb_toolbox-0.1.8.tar.gz",
"has_sig": false,
"md5_digest": "083586139e3a84999dec254147e7109a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 31565,
"upload_time": "2025-07-15T20:51:43",
"upload_time_iso_8601": "2025-07-15T20:51:43.165890Z",
"url": "https://files.pythonhosted.org/packages/17/4d/ad7764f659d98d5ea9b9e9a2b9e22632c29e05e75716e6c9ed4735f5a68f/pwb_toolbox-0.1.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-15 20:51:43",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "paperswithbacktest",
"github_project": "pwb-toolbox",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "backtrader",
"specs": []
},
{
"name": "beautifulsoup4",
"specs": []
},
{
"name": "click",
"specs": []
},
{
"name": "datasets",
"specs": []
},
{
"name": "ffn",
"specs": []
},
{
"name": "huggingface_hub",
"specs": []
},
{
"name": "kili",
"specs": []
},
{
"name": "langchain",
"specs": []
},
{
"name": "lxml",
"specs": []
},
{
"name": "numba",
"specs": []
},
{
"name": "pandas",
"specs": []
},
{
"name": "pytz",
"specs": []
},
{
"name": "selenium",
"specs": []
},
{
"name": "tqdm",
"specs": []
},
{
"name": "twilio",
"specs": []
},
{
"name": "webdriver_manager",
"specs": []
},
{
"name": "ibapi",
"specs": []
},
{
"name": "ib_insync",
"specs": []
}
],
"lcname": "pwb-toolbox"
}