Skip to main content

Access

Repository (GitLab): https://gitlab.com/fsbc/theses/quantbacktest PyPI: https://pypi.org/project/quantbacktest/

Master's thesis: https://ssrn.com/abstract=3620154

Setup

Install the project via the shell: pip install quantbacktest.

Update to a newer version via the shell (do this twice!): pip install quantbacktest --upgrade.

Exemplary usage

from quantbacktest import backtest_visualizer

# Importing modules from this repository
import sys

# For managing dates
from datetime import datetime

# For allowing for flexible time differences (frequencies)
from pandas.tseries.offsets import Timedelta


display_options = {
    'boolean_plot_heatmap': False,
    'boolean_test': False,  # If multi-asset strategy is used, this will cause sampling of the signals to speed up the run for testing during development.
    'warning_no_price_for_last_day': False,
    'warning_no_price_during_execution': False,
    'warning_no_price_for_intermediate_valuation': True,
    'warning_alternative_date': False,
    'warning_calculate_daily_returns_alternative_date': False,
    'warning_no_price_for_calculate_daily_returns': False,
    'warning_buy_order_could_not_be_filled': True,
    'warning_sell_order_could_not_be_filled': True,
    'errors_on_benchmark_gap': True,
    'boolean_plot_equity_curve': False,
    'boolean_save_equity_curve_to_disk': True,
    'string_results_directory': '/home/janspoerer/code/janspoerer/tmp/results'
}

general_settings = {
    'rounding_decimal_places': 4,
    'rounding_decimal_places_for_security_quantities': 0,
}

excel_worksheet_name = 'weights'

strategy_hyperparameters = {
    'maximum_deviation_in_days': 300,
    'prices_table_id_column_name': 'token_itin',
    'excel_worksheet_name': excel_worksheet_name,  # Set this to None if CSV is used!
    # For OpenMetrics: 9.8
    'buy_parameter_space': [9.8],  # [11, 20] # Times 10! Will be divided by 10.
    # For OpenMetrics: 9.7
    'sell_parameter_space': [9.7],  # [5, 9] # Times 10! Will be divided by 10.
    'maximum_relative_exposure_per_buy': 0.34,
    'frequency': Timedelta(days=1),
    'moving_average_window_in_days': 14,
    'id': 'TP3B-248N-Q',
    'boolean_allow_partially_filled_orders': True,
    'string_file_path_with_signal_data': '/home/janspoerer/code/janspoerer/quantbacktest/quantbacktest/assets/strategy_tables/test.csv'
}

constraints = {
    'maximum_individual_asset_exposure_all': 1.0,  # Not yet implemented
    'maximum_individual_asset_exposure_individual': {},  # Not yet implemented
    'maximum_gross_exposure': 1.0,  # Already implemented
    'boolean_allow_shortselling': False,  # Shortselling not yet implemented
    'minimum_cash': 100,
}

comments = {
    'display_options': repr(display_options),
    'strategy_hyperparameters': repr(strategy_hyperparameters)
}

backtest_visualizer(
    file_path_with_price_data='/home/janspoerer/code/janspoerer/quantbacktest/quantbacktest/assets/raw_itsa_data/20190717_itsa_tokenbase_top600_wtd302_token_daily.csv',
    # ONLY LEAVE THIS LINE UNCOMMENTED IF YOU WANT TO USE ETH-ADDRESSES AS ASSET IDENTIFIERS!
    # file_path_with_token_data='raw_itsa_data/20190717_itsa_tokenbase_top600_wtd301_token.csv',  # Only for multi-asset strategies.
    name_of_foreign_key_in_price_data_table='token_itin',
    name_of_foreign_key_in_token_metadata_table='token_itin',
    # 1: execute_strategy_white_noise()
    # 2: Not used anymore, can be reassigned
    # 3: execute_strategy_multi_asset() -> Uses strategy table
    # 4: execute_strategy_ma_crossover()
    int_chosen_strategy=4,
    dict_crypto_options={
        'general': {
            'percentage_buying_fees_and_spread': 0.005,  # 0.26% is the taker fee for low-volume clients at kraken.com https://www.kraken.com/features/fee-schedule
            'percentage_selling_fees_and_spread': 0.005,  # 0.26% is the taker fee for low-volume clients at kraken.com https://www.kraken.com/features/fee-schedule
            # Additional fees may apply for depositing money.
            'absolute_fee_buy_order': 0.0,
            'absolute_fee_sell_order': 0.0,
        }
    },
    float_budget_in_usd=1000000.00,
    strategy_hyperparameters=strategy_hyperparameters,
    margin_loan_rate=0.05,
    list_times_of_split_for_robustness_test=[
        [datetime(2014, 1, 1), datetime(2019, 5, 30)]
    ],
    benchmark_data_specifications={
        'name_of_column_with_benchmark_primary_key': 'id',  # Will be id after processing. Columns will be renamed.
        'benchmark_key': 'TP3B-248N-Q',  # Ether: T22F-QJGB-N, Bitcoin: TP3B-248N-Q
        'file_path_with_benchmark_data': '/home/janspoerer/code/janspoerer/quantbacktest/quantbacktest/assets/raw_itsa_data/20190717_itsa_tokenbase_top600_wtd302_token_daily.csv',
        'risk_free_rate': 0.02
    },
    display_options=display_options,
    constraints=constraints,
    general_settings=general_settings,
    comments=comments,
)

Information for maintainers/contributors

To make changes available in GitLab and as a pip install, please first push your changes to a new branch to GitLab and merge them.

  1. Update the version number in VERSION.
  2. Build wheel: python setup.py sdist bdist_wheel.
  3. Upload to PyPI: twine upload --skip-existing dist/*.*
  4. Get the current version on your machine: pip install quantbacktest --upgrade

Maintainers can also refer to this great guide: https://realpython.com/pypi-publish-python-package/#versioning-your-package

Further reference to quant trading in general

Quantopian offers state-of-the art backtesting for quantitative trading strategies for equity markets. Their YouTube channel hosts some excellent, generally applicable talks from renowned experts:

Release files for quantbacktest 0.0.33

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for quantbacktest 0.0.33
File Size Uploaded
quantbacktest-0.0.33.tar.gz 18.7 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for quantbacktest 0.0.33
File Interpreter ABI Platform
quantbacktest-0.0.33-py3-none-any.whl Python 3 none any Details

Total release size: 37.8 MB

Release files / quantbacktest-0.0.33.tar.gz

Download URL quantbacktest-0.0.33.tar.gz
Size 18.7 MB
Tags Source
SHA-256 checksum
How to use checksums
f8d3f56b42164c019335562ea78d737f0828513949ebd4a0cda74103c22d0307
BLAKE2b-256 checksum
How to use checksums
fd5b6b0929f89c348311ef3af9bbd29b8bbab0489546ba8ddb89669e021d8f8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1.post20200604 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.5

Release files / quantbacktest-0.0.33-py3-none-any.whl

Download URL quantbacktest-0.0.33-py3-none-any.whl
Size 19.1 MB
Tags Python 3
SHA-256 checksum
How to use checksums
aa80fd998c63531bbbe06636a10e7952a79bed5ef7095c9f9d496db8c58284fa
BLAKE2b-256 checksum
How to use checksums
1d029e91b2a83ab0285e3dfca330abae8e6a265b4c3521f2a65bb8e25f51171a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1.post20200604 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.5

Release history Release notifications | RSS feed

This release

0.0.33 This release

2 release files

0.0.32

2 release files

0.0.31

2 release files

0.0.30

2 release files

0.0.29

2 release files

0.0.28

2 release files

0.0.27

2 release files

0.0.26

2 release files

0.0.25

2 release files

0.0.24

2 release files

0.0.23

2 release files

0.0.22

2 release files

0.0.21

2 release files

0.0.20

2 release files

0.0.19

1 release file

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page