Skip to main content

Backtesting Framework for Algorithmic Trading

Project description

Modular Backtest

logo

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

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modular_backtest-0.0.1.post1.tar.gz (837.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modular_backtest-0.0.1.post1-py3-none-any.whl (34.2 kB view details)

Uploaded Python 3

File details

Details for the file modular_backtest-0.0.1.post1.tar.gz.

File metadata

  • Download URL: modular_backtest-0.0.1.post1.tar.gz
  • Upload date:
  • Size: 837.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for modular_backtest-0.0.1.post1.tar.gz
Algorithm Hash digest
SHA256 55775b7e9c65b3f521782d5a7ea209fe549e97e05a7200286d3fbcc0420b7261
MD5 6993ceb4d9d44574e7723c4e7befb8ba
BLAKE2b-256 3493dbd261a540388c949d5628b03d2cfe220f5eb8801661b177fe83509d23f2

See more details on using hashes here.

File details

Details for the file modular_backtest-0.0.1.post1-py3-none-any.whl.

File metadata

File hashes

Hashes for modular_backtest-0.0.1.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 e104b761171d6fa0b6a093bb3f880ac5a9e45b4a763033415c29ff2b3ed197a6
MD5 0b02a0d9c6712bcd05d89f8473f87341
BLAKE2b-256 b35b6d2da05bce09b6ffe471c6e5ab62408e746f8f915b93108d8febbc496e44

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page