Skip to main content

file: ../README.rst

Project description

CodeQL Python Version Package Version Downloads Rust Version Rust crates.io version License Documentation Status Roadmap Github

The master branch is switched to hftbacktest-2.0.0-alpha, which uses the Rust implementation as the backend. If you want to see the current version 1.8.4, please check out the corresponding tag.

High-Frequency Trading Backtesting Tool

This framework is designed for developing high-frequency trading and market-making strategies. It focuses on accounting for both feed and order latencies, as well as the order queue position for order fill simulation. The framework aims to provide more accurate market replay-based backtesting, based on full order book and trade tick feed data.

Key Features

The experimental features are currently in the early stages of development, having been completely rewritten in Rust to support the following features.

  • Working in Numba JIT function (Python).

  • Complete tick-by-tick simulation with a customizable time interval or based on the feed and order receipt.

  • Full order book reconstruction based on L2 Market-By-Price and L3 Market-By-Order (Rust-only, WIP) feeds.

  • Backtest accounting for both feed and order latency, using provided models or your own custom model.

  • Order fill simulation that takes into account the order queue position, using provided models or your own custom model.

  • Backtesting of multi-asset and multi-exchange models

  • Deployment of a live trading bot using the same algorithm code: currently for Binance Futures and Bybit. (Rust-only)

Example: The complete process of backtesting Binance Futures

high-frequency gridtrading: The complete process of backtesting Binance Futures using a high-frequency grid trading strategy implemented in Rust.

Documentation

See full document here.

Getting started

Installation

hftbacktest supports Python 3.10+. You can install hftbacktest using pip:

pip install hftbacktest

Or you can clone the latest development version from the Git repository with:

git clone https://github.com/nkaz001/hftbacktest

Data Source & Format

Please see Data or Data Preparation.

A Quick Example

Get a glimpse of what backtesting with hftbacktest looks like with these code snippets:

@njit
def simple_two_sided_quote(hbt):
    max_position = 5
    half_spread = hbt.tick_size * 20
    skew = 1
    order_qty = 0.1
    last_order_id = -1
    order_id = 0
    asset_no = 0

    # Checks every 0.1s
    while hbt.elapse(100_000_000) == 0:
        # Clears cancelled, filled or expired orders.
        hbt.clear_inactive_orders(asset_no)

        # Gets the market depth.
        depth = hbt.depth(asset_no)

        # Obtains the current mid-price and computes the reservation price.
        mid_price = (depth.best_bid + depth.best_ask) / 2.0
        reservation_price = mid_price - skew * hbt.position(asset_no) * depth.tick_size

        buy_order_price = reservation_price - half_spread
        sell_order_price = reservation_price + half_spread

        last_order_id = -1
        # Cancel all outstanding orders
        orders = hbt.orders(asset_no)
        values = orders.values()
        while True:
            order = values.next()
            if order is None:
                break
            if order.cancellable:
                hbt.cancel(asset_no, order.order_id)
                last_order_id = order.order_id

        # All order requests are considered to be requested at the same time.
        # Waits until one of the order cancellation responses is received.
        if last_order_id >= 0:
            hbt.wait_order_response(asset_no, last_order_id)

        # Clears cancelled, filled or expired orders.
        hbt.clear_inactive_orders(asset_no)

            last_order_id = -1
        if hbt.position < max_position:
            # Submits a new post-only limit bid order.
            order_id += 1
            hbt.submit_buy_order(
                asset_no,
                order_id,
                buy_order_price,
                order_qty,
                GTX,
                LIMIT,
                False
            )
            last_order_id = order_id

        if hbt.position > -max_position:
            # Submits a new post-only limit ask order.
            order_id += 1
            hbt.submit_sell_order(
                asset_no,
                order_id,
                sell_order_price,
                order_qty,
                GTX,
                LIMIT,
                False
            )
            last_order_id = order_id

        # All order requests are considered to be requested at the same time.
        # Waits until one of the order responses is received.
        if last_order_id >= 0:
            hbt.wait_order_response(asset_no, last_order_id)

Tutorials

Examples

You can find more examples in examples directory and Rust examples.

Roadmap

Currently, new features are being implemented in Rust due to the limitations of Numba, as performance is crucial given the size of the high-frequency data. The imminent task is to integrate hftbacktest in Python with hftbacktest in Rust by using the Rust implementation as the backend. Meanwhile, the data format, which is currently different, needs to be unified. On the pure Python side, the performance reporting tool should be improved to provide more performance metrics with increased speed.

Please see the roadmap.

Contributing

Thank you for considering contributing to hftbacktest! Welcome any and all help to improve the project. If you have an idea for an enhancement or a bug fix, please open an issue or discussion on GitHub to discuss it.

The following items are examples of contributions you can make to this project:

Please see the roadmap.

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

hftbacktest-2.0.0rc0.tar.gz (4.2 MB view details)

Uploaded Source

Built Distribution

hftbacktest-2.0.0rc0-cp310-cp310-manylinux_2_34_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.34+ x86-64

File details

Details for the file hftbacktest-2.0.0rc0.tar.gz.

File metadata

  • Download URL: hftbacktest-2.0.0rc0.tar.gz
  • Upload date:
  • Size: 4.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.7.0

File hashes

Hashes for hftbacktest-2.0.0rc0.tar.gz
Algorithm Hash digest
SHA256 8447a78c54aa39a110de1befda22e038451bfdf498bf90a14e9f1b7af7edf73a
MD5 058fae5a1d35346688567192bb98c53a
BLAKE2b-256 26b3cf364c9c29c2c3e89c91be99ffcaeddbaeadbca249d8601a9d60296a8d49

See more details on using hashes here.

File details

Details for the file hftbacktest-2.0.0rc0-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for hftbacktest-2.0.0rc0-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 f10062ad409d3d9fa2e6f84de3012b01b858bd12329f939695e3d7e46d75113b
MD5 30b61707fbe2e1d7bf6c09caaf58b00f
BLAKE2b-256 50c212d4370029439da7709f13d4adee7c3970f8f3ec0dfd47f555895b11f67c

See more details on using hashes here.

Supported by

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