Giter Club home page Giter Club logo

hftbacktest's Introduction

HftBacktest

|Codacy |CodeQL |Package Version |Downloads |License

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.

Getting started

Installation

hftbacktest supports Python 3.7+. 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 https://github.com/nkaz001/collect-binancefutures regarding collecting and converting the feed data or datautils directory.

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

    bid_order_price_tick_as_id = -1
    ask_order_price_tick_as_id = -1

    while hbt.run:
        # Check every 0.1s
        if not hbt.elapse(0.1 * 1e6):
            return False

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

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

        bid_order_price = reservation_price - half_spread
        ask_order_price = reservation_price + half_spread

        # Cancel the existing bid order.
        existing_bid_order = hbt.orders.get(bid_order_price_tick_as_id)
        if existing_bid_order is not None and existing_bid_order.cancellable:
            hbt.cancel(existing_bid_order.order_id)
            last_order_id = existing_bid_order.order_id

        # Cancel the existing ask order.
        existing_ask_order = hbt.orders.get(ask_order_price_tick_as_id)
        if existing_ask_order is not None and existing_ask_order.cancellable:
            hbt.cancel(existing_ask_order.order_id)
            last_order_id = existing_ask_order.order_id

        if hbt.position < max_position:
            # Submit a new post-only limit bid order.
            bid_order_price_tick_as_id = round(bid_order_price / hbt.tick_size)
            hbt.submit_buy_order(
                bid_order_price_tick_as_id,
                bid_order_price,
                order_qty,
                GTX
            )
            last_order_id = bid_order_price_tick_as_id

        if hbt.position > -max_position:
            # Submit a new post-only limit ask order.
            ask_order_price_tick_as_id = round(ask_order_price / hbt.tick_size)
            hbt.submit_sell_order(
                ask_order_price_tick_as_id,
                ask_order_price,
                order_qty,
                GTX
            )
            last_order_id = ask_order_price_tick_as_id

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

        # Record the current state for stat calculation.
        stat.record(hbt)
    return True

Examples

You can find more examples in examples directory.

Documentation

hftbacktest's People

Contributors

nkaz001 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.