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 modular_backtest.data.bundles import yahoo to ~/.zipline/extension.py the 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.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.tar.gz (837.6 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-py3-none-any.whl (34.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modular_backtest-0.0.1.tar.gz
  • Upload date:
  • Size: 837.6 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.tar.gz
Algorithm Hash digest
SHA256 693d56b34e269e96dee817ce56417e813b4d65f556553657dcf44be6f89e5e65
MD5 3ced9c890463b050edf170cab237789a
BLAKE2b-256 3768c6996dc4dcc2e9c5269422598b58c68e1cca4f1cfd7a0b95ee2f27c1a58c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for modular_backtest-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 90ff8c9de2daec223eea4349f909e9960e57bd2bcda9d687601045877f6946d1
MD5 b700dd16127bc8c38f8d6d62f402fd85
BLAKE2b-256 0d12d05bddda017aa656ee45c25b985a9010164b8d63c82e79f035dbdebc54f8

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