{"id":13487480,"url":"https://github.com/nkaz001/hftbacktest","last_synced_at":"2025-05-16T01:07:01.644Z","repository":{"id":62594681,"uuid":"528898425","full_name":"nkaz001/hftbacktest","owner":"nkaz001","description":"A high frequency trading and market making backtesting and trading bot in Python and Rust, which accounts for limit orders, queue positions, and latencies, utilizing full tick data for trades and order books, with real-world crypto market-making examples for Binance Futures","archived":false,"fork":false,"pushed_at":"2025-05-13T14:35:24.000Z","size":130076,"stargazers_count":2589,"open_issues_count":22,"forks_count":512,"subscribers_count":68,"default_branch":"master","last_synced_at":"2025-05-13T15:51:09.681Z","etag":null,"topics":["algorithmic-trading","algotrading","backtesting","backtesting-engine","backtesting-trading-strategies","binance","binance-futures","crypto-bot","crypto-trading","hft","high-frequency-trading","limit-order-book","market-maker","market-making","orderbook-tick-data","orderbooks","quantitative-trading","trading-algorithms","trading-bot","trading-simulator"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nkaz001.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":"ROADMAP.md","authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-08-25T14:56:36.000Z","updated_at":"2025-05-13T14:35:28.000Z","dependencies_parsed_at":"2023-12-10T13:27:10.801Z","dependency_job_id":"d674b552-e69b-4c0f-8af2-9c4de07c45fe","html_url":"https://github.com/nkaz001/hftbacktest","commit_stats":{"total_commits":56,"total_committers":2,"mean_commits":28.0,"dds":0.1428571428571429,"last_synced_commit":"9e4265c1b9b2b5ccb9af535a43a024bc10006ac6"},"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nkaz001%2Fhftbacktest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nkaz001%2Fhftbacktest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nkaz001%2Fhftbacktest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nkaz001%2Fhftbacktest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nkaz001","download_url":"https://codeload.github.com/nkaz001/hftbacktest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253991107,"owners_count":21995882,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithmic-trading","algotrading","backtesting","backtesting-engine","backtesting-trading-strategies","binance","binance-futures","crypto-bot","crypto-trading","hft","high-frequency-trading","limit-order-book","market-maker","market-making","orderbook-tick-data","orderbooks","quantitative-trading","trading-algorithms","trading-bot","trading-simulator"],"created_at":"2024-07-31T18:00:59.899Z","updated_at":"2025-05-16T01:06:56.630Z","avatar_url":"https://github.com/nkaz001.png","language":"Rust","funding_links":[],"categories":["Python","Backtest + live trading","Rust","Backtesting and Live Trading","Backtesting"],"sub_categories":["Trading \u0026 Backtesting","General purpose","General - Event Driven Frameworks"],"readme":"===========\nHftBacktest\n===========\n\n|codeql| |python| |pypi| |downloads| |rustc| |crates| |license| |docs| |roadmap| |github|\n\nHigh-Frequency Trading Backtesting Tool\n=======================================\n\nThis 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.\n\nKey Features\n============\n\nThe experimental features are currently in the early stages of development, having been completely rewritten in Rust to\nsupport the following features.\n\n* Working in `Numba \u003chttps://numba.pydata.org/\u003e`_ JIT function (Python).\n* Complete tick-by-tick simulation with a customizable time interval or based on the feed and order receipt.\n* Full order book reconstruction based on L2 Market-By-Price and L3 Market-By-Order feeds.\n* Backtest accounting for both feed and order latency, using provided models or your own custom model.\n* Order fill simulation that takes into account the order queue position, using provided models or your own custom model.\n* Backtesting of multi-asset and multi-exchange models\n* Deployment of a live trading bot using the same algorithm code: currently for Binance Futures and Bybit. (Rust-only)\n\nDocumentation\n=============\n\nSee `full document here \u003chttps://hftbacktest.readthedocs.io/\u003e`_.\n\nGetting started\n===============\n\nInstallation\n------------\n\nhftbacktest supports Python 3.10+. You can install hftbacktest using ``pip``:\n\n.. code-block:: console\n\n pip install hftbacktest\n\nOr you can clone the latest development version from the Git repository with:\n\n.. code-block:: console\n\n git clone https://github.com/nkaz001/hftbacktest\n\nData Source \u0026 Format\n--------------------\n\nPlease see `Data \u003chttps://hftbacktest.readthedocs.io/en/latest/data.html\u003e`_ or `Data Preparation \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Data%20Preparation.html\u003e`_.\n\nYou can also find some data `here \u003chttps://reach.stratosphere.capital/data/usdm/\u003e`_, hosted by the supporter.\n\nA Quick Example\n---------------\n\nGet a glimpse of what backtesting with hftbacktest looks like with these code snippets:\n\n.. code-block:: python\n\n    @njit\n    def market_making_algo(hbt):\n        asset_no = 0\n        tick_size = hbt.depth(asset_no).tick_size\n        lot_size = hbt.depth(asset_no).lot_size\n\n        # in nanoseconds\n        while hbt.elapse(10_000_000) == 0:\n            hbt.clear_inactive_orders(asset_no)\n\n            a = 1\n            b = 1\n            c = 1\n            hs = 1\n\n            # Alpha, it can be a combination of several indicators.\n            forecast = 0\n            # In HFT, it can be various measurements of short-term market movements,\n            # such as the high-low range in the last X minutes.\n            volatility = 0\n            # Delta risk, it can be a combination of several risks.\n            position = hbt.position(asset_no)\n            risk = (c + volatility) * position\n            half_spread = (c + volatility) * hs\n\n            max_notional_position = 1000\n            notional_qty = 100\n\n            depth = hbt.depth(asset_no)\n\n            mid_price = (depth.best_bid + depth.best_ask) / 2.0\n\n            # fair value pricing = mid_price + a * forecast\n            #                      or underlying(correlated asset) + adjustment(basis + cost + etc) + a * forecast\n            # risk skewing = -b * risk\n            reservation_price = mid_price + a * forecast - b * risk\n            new_bid = reservation_price - half_spread\n            new_ask = reservation_price + half_spread\n\n            new_bid_tick = min(np.round(new_bid / tick_size), depth.best_bid_tick)\n            new_ask_tick = max(np.round(new_ask / tick_size), depth.best_ask_tick)\n\n            order_qty = np.round(notional_qty / mid_price / lot_size) * lot_size\n\n            # Elapses a process time.\n            if not hbt.elapse(1_000_000) != 0:\n                return False\n\n            last_order_id = -1\n            update_bid = True\n            update_ask = True\n            buy_limit_exceeded = position * mid_price \u003e max_notional_position\n            sell_limit_exceeded = position * mid_price \u003c -max_notional_position\n            orders = hbt.orders(asset_no)\n            order_values = orders.values()\n            while order_values.has_next():\n                order = order_values.get()\n                if order.side == BUY:\n                    if order.price_tick == new_bid_tick or buy_limit_exceeded:\n                        update_bid = False\n                    if order.cancellable and (update_bid or buy_limit_exceeded):\n                        hbt.cancel(asset_no, order.order_id, False)\n                        last_order_id = order.order_id\n                elif order.side == SELL:\n                    if order.price_tick == new_ask_tick or sell_limit_exceeded:\n                        update_ask = False\n                    if order.cancellable and (update_ask or sell_limit_exceeded):\n                        hbt.cancel(asset_no, order.order_id, False)\n                        last_order_id = order.order_id\n\n            # It can be combined with a grid trading strategy by submitting multiple orders to capture better spreads and\n            # have queue position.\n            # This approach requires more sophisticated logic to efficiently manage resting orders in the order book.\n            if update_bid:\n                # There is only one order at a given price, with new_bid_tick used as the order ID.\n                order_id = new_bid_tick\n                hbt.submit_buy_order(asset_no, order_id, new_bid_tick * tick_size, order_qty, GTX, LIMIT, False)\n                last_order_id = order_id\n            if update_ask:\n                # There is only one order at a given price, with new_ask_tick used as the order ID.\n                order_id = new_ask_tick\n                hbt.submit_sell_order(asset_no, order_id, new_ask_tick * tick_size, order_qty, GTX, LIMIT, False)\n                last_order_id = order_id\n\n            # All order requests are considered to be requested at the same time.\n            # Waits until one of the order responses is received.\n            if last_order_id \u003e= 0:\n                # Waits for the order response for a maximum of 5 seconds.\n                timeout = 5_000_000_000\n                if not hbt.wait_order_response(asset_no, last_order_id, timeout):\n                    return False\n\n        return True\n\n\nTutorials\n=========\n* `Data Preparation \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Data%20Preparation.html\u003e`_\n* `Getting Started \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Getting%20Started.html\u003e`_\n* `Working with Market Depth and Trades \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Working%20with%20Market%20Depth%20and%20Trades.html\u003e`_\n* `Integrating Custom Data \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Integrating%20Custom%20Data.html\u003e`_\n* `Making Multiple Markets - Introduction \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Making%20Multiple%20Markets%20-%20Introduction.html\u003e`_\n* `High-Frequency Grid Trading \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/High-Frequency%20Grid%20Trading.html\u003e`_\n* `Impact of Order Latency \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Impact%20of%20Order%20Latency.html\u003e`_\n* `Order Latency Data \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Order%20Latency%20Data.html\u003e`_\n* `Guéant–Lehalle–Fernandez-Tapia Market Making Model and Grid Trading \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/GLFT%20Market%20Making%20Model%20and%20Grid%20Trading.html\u003e`_\n* `Making Multiple Markets \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Making%20Multiple%20Markets.html\u003e`_\n* `Risk Mitigation through Price Protection in Extreme Market Conditions \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Risk%20Mitigation%20through%20Price%20Protection%20in%20Extreme%20Market%20Conditions.html\u003e`_\n* `Level-3 Backtesting \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Level-3%20Backtesting.html\u003e`_\n* `Market Making with Alpha - Order Book Imbalance \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Market%20Making%20with%20Alpha%20-%20Order%20Book%20Imbalance.html\u003e`_\n* `Market Making with Alpha - Basis \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Market%20Making%20with%20Alpha%20-%20Basis.html\u003e`_\n* `Market Making with Alpha - APT \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Market%20Making%20with%20Alpha%20-%20APT.html\u003e`_\n* `Queue-Based Market Making in Large Tick Size Assets \u003chttps://hftbacktest.readthedocs.io/en/latest/tutorials/Queue-Based%20Market%20Making%20in%20Large%20Tick%20Size%20Assets.html\u003e`_\n\nExamples\n========\n\nYou can find more examples in `examples \u003chttps://github.com/nkaz001/hftbacktest/tree/master/examples\u003e`_ directory and `Rust examples \u003chttps://github.com/nkaz001/hftbacktest/blob/master/hftbacktest/examples/\u003e`_.\n\nThe complete process of backtesting Binance Futures\n---------------------------------------------------\n`high-frequency gridtrading \u003chttps://github.com/nkaz001/hftbacktest/blob/master/hftbacktest/examples/gridtrading.ipynb\u003e`_: The complete process of backtesting Binance Futures using a high-frequency grid trading strategy implemented in Rust.\n\nMigration to V2\n===============\nPlease see the `migration guide \u003chttps://hftbacktest.readthedocs.io/en/latest/migration2.html\u003e`_.\n\nRoadmap\n=======\n\nPlease see the `roadmap \u003chttps://github.com/nkaz001/hftbacktest/blob/master/ROADMAP.md\u003e`_.\n\nContributing\n============\n\nThank you for considering contributing to hftbacktest! Welcome any and all help to improve the project. If you have an\nidea for an enhancement or a bug fix, please open an issue or discussion on GitHub to discuss it.\n\nThe following items are examples of contributions you can make to this project:\n\nPlease see the `roadmap \u003chttps://github.com/nkaz001/hftbacktest/blob/master/ROADMAP.md\u003e`_.\n\n.. |python| image:: https://shields.io/badge/python-3.10-blue\n    :alt: Python Version\n    :target: https://www.python.org/\n\n.. |codeql| image:: https://github.com/nkaz001/hftbacktest/actions/workflows/codeql.yml/badge.svg?branch=master\u0026event=push\n    :alt: CodeQL\n    :target: https://github.com/nkaz001/hftbacktest/actions/workflows/codeql.yml\n\n.. |pypi| image:: https://badge.fury.io/py/hftbacktest.svg\n    :alt: Package Version\n    :target: https://pypi.org/project/hftbacktest\n\n.. |downloads| image:: https://static.pepy.tech/badge/hftbacktest\n    :alt: Downloads\n    :target: https://pepy.tech/project/hftbacktest\n\n.. |crates| image:: https://img.shields.io/crates/v/hftbacktest.svg\n    :alt: Rust crates.io version\n    :target: https://crates.io/crates/hftbacktest\n\n.. |license| image:: https://img.shields.io/badge/License-MIT-green.svg\n    :alt: License\n    :target: https://github.com/nkaz001/hftbacktest/blob/master/LICENSE\n\n.. |docs| image:: https://readthedocs.org/projects/hftbacktest/badge/?version=latest\n    :target: https://hftbacktest.readthedocs.io/en/latest/?badge=latest\n    :alt: Documentation Status\n\n.. |roadmap| image:: https://img.shields.io/badge/Roadmap-gray\n    :target: https://github.com/nkaz001/hftbacktest/blob/master/ROADMAP.md\n    :alt: Roadmap\n\n.. |github| image:: https://img.shields.io/github/stars/nkaz001/hftbacktest?style=social\n    :target: https://github.com/nkaz001/hftbacktest\n    :alt: Github\n\n.. |rustc| image:: https://shields.io/badge/rustc-1.85-blue\n    :alt: Rust Version\n    :target: https://www.rust-lang.org/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnkaz001%2Fhftbacktest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnkaz001%2Fhftbacktest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnkaz001%2Fhftbacktest/lists"}