Skip to main content

High-frequency trading and market making backtesting tool

Project description

|Codacy |CodeQL |Package Version |Downloads |License Documentation Status

High-Frequency Trading Backtesting Tool in Python

This Python 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

  • Working in Numba JIT function.

  • Complete tick-by-tick simulation with a variable time interval.

  • Full order book reconstruction based on L2 feeds(Market-By-Price).

  • 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.

Documentation

See full document here.

Getting started

Installation

hftbacktest supports Python 3.8+. 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, stat):
    max_position = 5
    half_spread = hbt.tick_size * 20
    skew = 1
    order_qty = 0.1
    last_order_id = -1
    order_id = 0

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

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

        buy_order_price = reservation_price - half_spread
        sell_order_price = reservation_price + half_spread

        last_order_id = -1
        # Cancel all outstanding orders
        for order in hbt.orders.values():
            if order.cancellable:
                hbt.cancel(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(last_order_id)

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

            last_order_id = -1
        if hbt.position < max_position:
            # Submits a new post-only limit bid order.
            order_id += 1
            hbt.submit_buy_order(
                order_id,
                buy_order_price,
                order_qty,
                GTX
            )
            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(
                order_id,
                sell_order_price,
                order_qty,
                GTX
            )
            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(last_order_id)

        # Records the current state for stat calculation.
        stat.record(hbt)

Examples

You can find more examples in examples directory.

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-1.6.2.tar.gz (49.6 kB view details)

Uploaded Source

File details

Details for the file hftbacktest-1.6.2.tar.gz.

File metadata

  • Download URL: hftbacktest-1.6.2.tar.gz
  • Upload date:
  • Size: 49.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for hftbacktest-1.6.2.tar.gz
Algorithm Hash digest
SHA256 c45591b3b0d340de0487bd4387e365d014b69ccd358717e237dd4e00741e5821
MD5 9d8b13d5c718a74599d7db34ea0dde1f
BLAKE2b-256 b72e663568d011016f1eb737cea9924b437a12432449f80368ef01d4530665a0

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