Backtesting Framework for Algorithmic Trading
Project description
Modular Backtest
About the project
Modular Backtest is backtesting framework for algorithmic trading written in Python, focuses on modularity and resusability. The framework is built on top of zipline-reloaded, a Pythonic algorithmic trading library. It consists of the following modules with according responsibility.
-
Asset selection
select the assets to be included in the universe. -
Signal generation
generate signal sentiment for the given assets. -
Portfolio construction
construct portfolio allocations based on the given signals. -
Risk managements
adjust the portfolio allocations. -
Order execution
create buy/sell orders responding to the constructed allocations. -
Data
downlod and ingest equity's historical prices via public API. Currently support Yahoo Finance. -
Factors
compute statistics and technical indicators used in trading.
Getting started
Installation
pip install modular-backtest
Data ingestion
After installed, add the line from modular_backtest.data.bundles import yahoo to ~/.zipline/extension.py and then run
zipline ingest -b yahoo-finance
Usages
from datetime import datetime
import pandas as pd
from modular_backtest.data.bundles import yahoo
from modular_backtest.engine import BacktestEngine
from modular_backtest.factors.handler import FactorHandler
from modular_backtest.models.asset_selection import ManualAssetSelection
from modular_backtest.models.handler import ModelHandler
from modular_backtest.models.order_execution import InstantOrderExecution
from modular_backtest.models.portfolio_construction import (
EqualWeightPortfolioConstruction,
)
from modular_backtest.models.risk_management import VoidRiskManagement
from modular_backtest.models.signal_generation import StaticSignalGeneration
models = ModelHandler(
ManualAssetSelection(["AAPL", "MSFT", "AMZN", "SPY", "QQQ"]),
StaticSignalGeneration(),
EqualWeightPortfolioConstruction(),
InstantOrderExecution(),
VoidRiskManagement(),
)
engine = BacktestEngine(models=models, factors=FactorHandler())
res = engine.run(
start=datetime(2024, 1, 1), end=datetime(2024, 12, 31), bundle=yahoo.NAME
)
res.data.portfolio_value
# | | portfolio_value |
# |:--------------------------|------------------:|
# | 2024-01-02 21:00:00+00:00 | 100000 |
# | 2024-01-03 21:00:00+00:00 | 99852.6 |
# | 2024-01-04 21:00:00+00:00 | 98775.5 |
# | 2024-01-05 21:00:00+00:00 | 98826.4 |
# | 2024-01-08 21:00:00+00:00 | 100890 |
# | 2024-01-09 21:00:00+00:00 | 101222 |
# | 2024-01-10 21:00:00+00:00 | 102281 |
# | 2024-01-11 21:00:00+00:00 | 102544 |
# | 2024-01-12 21:00:00+00:00 | 102735 |
# | 2024-01-16 21:00:00+00:00 | 102308 |
# | 2024-01-17 21:00:00+00:00 | 101740 |
# | 2024-01-18 21:00:00+00:00 | 103333 |
# | 2024-01-19 21:00:00+00:00 | 104815 |
# | 2024-01-22 21:00:00+00:00 | 104953 |
# | 2024-01-23 21:00:00+00:00 | 105534 |
# | 2024-01-24 21:00:00+00:00 | 105908 |
# | 2024-01-25 21:00:00+00:00 | 106253 |
# | 2024-01-26 21:00:00+00:00 | 106046 |
# | 2024-01-29 21:00:00+00:00 | 106948 |
# | 2024-01-30 21:00:00+00:00 | 106023 |
# | 2024-01-31 21:00:00+00:00 | 103769 |
# | 2024-02-01 21:00:00+00:00 | 105433 |
# | 2024-02-02 21:00:00+00:00 | 107957 |
# ...
# | 2024-12-26 21:00:00+00:00 | 135358 |
# | 2024-12-27 21:00:00+00:00 | 133499 |
# | 2024-12-30 21:00:00+00:00 | 131844 |
# | 2024-12-31 21:00:00+00:00 | 130903 |
Documentation
License
Distributed under the MIT License. See LICENSE for more information.
Maintainers
Modular Backtest is currently maintained by kfuangsung (kachain.f@outlook.com).
Acknowledgments
- zipline-reloaded: A Pythonic Algorithmic Trading Library (forked from quantopian/zipline)
Disclaimer
Please remember that past performance may not be indicative of future results. Different types of investments involve varying degrees of risk, and there can be no assurance that the future performance of any specific investment, investment strategy, or product made reference to directly or indirectly in this page, will be profitable, equal any corresponding indicated historical performance level(s), or be suitable for your portfolio.
The hypothetical backtested performance does not represent the results of actual trading and does not and is not intended to indicate the past performance or future performance of investment strategy.
The hypothetical backtested performance results for each strategy include estimated values for transaction costs of buying and selling securities, which may not be accurate. Investment management fees (including without limitation management fees and performance fees), custody and other costs, and taxes are not included in performance results.
The hypothetical performance does not reflect the reinvestment of dividends and distributions therefrom, interest, capital gains and withholding taxes.
Simulated returns may be dependent on the market and economic conditions that existed during the period. Future market or economic conditions can adversely affect the returns.
Project details
Release history Release notifications | RSS feed
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 modular_backtest-0.0.1.post0.tar.gz.
File metadata
- Download URL: modular_backtest-0.0.1.post0.tar.gz
- Upload date:
- Size: 837.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f7b552d3fce59b4dbee3dc855eca8abc93f3729f07dba098bbcb20668d31f6e
|
|
| MD5 |
08549588d74f2b220b83c3f7124b6924
|
|
| BLAKE2b-256 |
7b8134a71f567a2b17a00ad13b8c7ed274ef33ec2519275a1d2776ae9e76c167
|
File details
Details for the file modular_backtest-0.0.1.post0-py3-none-any.whl.
File metadata
- Download URL: modular_backtest-0.0.1.post0-py3-none-any.whl
- Upload date:
- Size: 34.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2736deaa64f29818936063d3a250ea4a1f35f1cbaff1aad7b842dabf86a28c67
|
|
| MD5 |
2d8d13dbbdd89f7de7941f6422179509
|
|
| BLAKE2b-256 |
e32ed07f7fe485ee0f3fa63d1f20edcd0ce6d4b7605d1471c4e8400db6c822e1
|