{"repo":"nkaz001/hftbacktest","free":true,"listed":false,"github":"https://github.com/nkaz001/hftbacktest","clone":"git clone https://github.com/nkaz001/hftbacktest.git","description":"Free, open source, a high frequency trading and market making backtesting and trading bot, which accounts for limit orders, queue positions, and latencies, utilizing full tick data for trades and order books(Level-2 and Level-3), with real-world crypto trading examples for Binance and Bybit","language":"Rust","stars":4367,"topics":["algorithmic-trading","algotrading","backtesting","backtesting-engine","backtesting-trading-strategies","binance","crypto-bot","crypto-trading","hft","high-frequency-trading","limit-order-book","market-maker","market-making","orderbook","orderbook-tick-data","quantitative-trading","trading-algorithms","trading-simulator","trading-strategies","tradingbot"],"license":"MIT","category":"quant_trading_toolkit","readme_excerpt":"===========\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\n* Working in `Numba <https://numba.pydata.org/>`_ 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 Level-2 Market-By-Price and Level-3 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 for quick prototyping and testing using the same algorithm code: currently for Binance Futures and Bybit. (Rust-only)\n\nDocumentation\n=============\n\nSee `full document here <https://hftbacktest.readthedocs.io/>`_.\n\nTutorials you’ll likely find interesting:\n\n* `High-Frequency Grid Trading - Simplified from GLFT <https://hftbacktest.readthedocs.io/en/latest/tutorials/High-Frequency%20Grid%20Trading%20-%20Simplified%20from%20GLFT.html>`_\n* `Market Making with Alpha - Order Book Imbalance <https://hftbacktest.readthedocs.io/en/latest/tutorials/Market%20Making%20with%20Alpha%20-%20Order%20Book%20Imbalance.html>`_\n* `Market Making with Alpha - APT <https://hftbacktest.readthedocs.io/en/latest/tutorials/Market%20Making%20with%20Alpha%20-%20APT.html>`_\n* `Accelerated Backtesting <https://hftbacktest.readthedocs.io/en/latest/tutorials/Accelerated%20Backtesting.html>`_\n* `Pricing Framework <https://hftbacktest.readthedocs.io/en/latest/tutorials/Pricing%20Framework.html>`_\n\nWhy Accurate Backtesting Matters — Not Just Conservative Approach\n=================================================================\n\nTrading is a highly competitive field where only the small edges usually exist, but they can still make a significant\ndifference. Because of this, backtesting must accurately simulate real-world conditions.: It should neither rely on an\noverly pessimistic approach that hides these small edges and profit opportunities, nor on an overly optimistic one that\noverstates them through unrealistic simulation. Or at the very least, you should clearly understand what differs from\nlive trading and by how much, since sometimes fully accurate backtesting is not practical due to the time it requires.\n\nThis is not about overfitting at the start—before you even consider issues like overfitting, you need confidence that\nyour backtesting truly reflects real-world execution. For example, if you run a live trading strategy in January 2025,\nthe backtest for that exact period should produce results that closely align with the actual results. Once you’ve\nvalidated that your backtesting can accurately reproduce live trading results, then you can proceed to deeper research,\noptimization, and considerations around overfitting.\n\nAccurate backtesting is the foundation. Without it, all further analysis—whether conservative or aggressive—becomes\nunreliable.\n\nGetting started\n===============\n\nInstallation\n------------\n\nhftbacktest supports Python 3.11+. 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 & Format\n--------------------\n\nPlease see `Data <https://hftbacktest.readthedocs.io/en/latest/data.html>`_ or `Data Preparation <https://hftbacktest.readthedocs.io/en/latest/tutorials/Data%20Preparation.html>`_.\n\nYou can also find some data `here <https://reach.stratosphere.capital/data/usdm/>`_, 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 > max_notional_position\n            sell_limit_exceeded = position * mid_price < -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 >= 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 <https://hftbacktest.readthedocs.io/en/latest/tutorials/Data%20Preparation.html>`_\n* `Getting Started <https://hftbacktest.readthedocs.io/en/latest/tutorials/Getting%20Started.html>`_\n* `Working with Market Depth and Trades <https://hftbacktest.readthedocs.io/en/latest/tutorials/Working%20with%20Market%20Depth%20and%20Trades.html>`_\n* `Integrating Custom Data <https://hftbacktest.readthedocs.io/en/latest/tutorials/Integrating%20Custom%20Data.html>`_\n* `Making Multiple Markets - Introduction <https://hftbacktest.readthedocs.io/en/latest/tutorials/Making%20Multiple%20Markets%20-%20Introduction.html>`_\n* `High-Frequency Grid Trading <https://hftbacktest.readthedocs.io/en/latest/tutorials/High-Frequency%20Grid%20Trading.html>`_\n* `High-Frequency Grid Trading - Comparison Across Other Exchanges <https://hftbacktest.readthedocs.io/en/latest/tutorials/High-Frequency%20Grid%20Trading%20-%20Comparison%20Across%20Other%20Exchanges.html>`_\n* `High-Frequency Grid Trading - Simplified from GLFT <https://hftbacktest.readthedocs.io/en/latest/tutorials/High-Frequency%20Grid%20Trading%20-%20Simplified%20from%20GLFT.html>`_\n* `Impact of Order Latency <https://hftbacktest.readthedocs.io/en/latest/tutorials/Impact%20of%20Order%20Latency.html>`_\n* `Order Latency Data <https://hftbacktest.readthedocs.io/en/latest/tuto","default_branch":"master","files":232,"tree":[".cargo/config.toml",".gitattributes",".github/workflows/codeql.yml",".github/workflows/release-python.yml",".github/workflows/stale.yml",".gitignore",".readthedocs.yml","CODE_OF_CONDUCT.md","Cargo.toml","LICENSE","README.rst","ROADMAP.md","collector/Cargo.toml","collector/src/binance/http.rs","collector/src/binance/mod.rs","collector/src/binancefuturescm/http.rs","collector/src/binancefuturescm/mod.rs","collector/src/binancefuturesum/http.rs","collector/src/binancefuturesum/mod.rs","collector/src/bybit/http.rs","collector/src/bybit/mod.rs","collector/src/error.rs","collector/src/file.rs","collector/src/hyperliquid/http.rs","collector/src/hyperliquid/mod.rs","collector/src/main.rs","collector/src/throttler.rs","connector/Cargo.toml","connector/README.md","connector/examples/binancefutures.toml","connector/examples/binancespot.toml","connector/examples/bybit.toml","connector/src/binancefutures/market_data_stream.rs","connector/src/binancefutures/mod.rs","connector/src/binancefutures/msg/mod.rs","connector/src/binancefutures/msg/rest.rs","connector/src/binancefutures/msg/stream.rs","connector/src/binancefutures/ordermanager.rs","connector/src/binancefutures/rest.rs","connector/src/binancefutures/user_data_stream.rs","connector/src/binancespot/market_data_stream.rs","connector/src/binancespot/mod.rs","connector/src/binancespot/msg/mod.rs","connector/src/binancespot/msg/rest.rs","connector/src/binancespot/msg/stream.rs","connector/src/binancespot/ordermanager.rs","connector/src/binancespot/rest.rs","connector/src/binancespot/user_data_stream.rs","connector/src/bybit/mod.rs","connector/src/bybit/msg.rs","connector/src/bybit/ordermanager.rs","connector/src/bybit/private_stream.rs","connector/src/bybit/public_stream.rs","connector/src/bybit/rest.rs","connector/src/bybit/trade_stream.rs","connector/src/connector.rs","connector/src/main.rs","connector/src/utils.rs","docs/Makefile","docs/conf.py","docs/data.rst","docs/debugging_backtesting_and_live_discrepancies.rst","docs/images/CRVUSDT_chart.png","docs/images/CRVUSDT_depth.png","docs/images/arch.png","docs/images/latencies.png","docs/images/latency-comparison.png","docs/images/liquidity-and-trade-activities.png","docs/images/probqueuemodel.png","docs/images/probqueuemodel2.png","docs/images/probqueuemodel3.png","docs/images/probqueuemodel_log.png","docs/index.rst","docs/jit_compilation_overhead.rst","docs/latency_models.rst","docs/market_maker_program.rst","docs/migration2.rst","docs/order_fill.rst","docs/reference/backtester.rst","docs/reference/constants.rst","docs/reference/data_utilities.rst","docs/reference/data_validation.rst","docs/reference/hftbacktest.data.utils.binancefutures.rst","docs/reference/hftbacktest.data.utils.binancehistmktdata.rst","docs/reference/hftbacktest.data.utils.databento.rst","docs/reference/hftbacktest.data.utils.difforderbooksnapshot.rst","docs/reference/hftbacktest.data.utils.migration2.rst","docs/reference/hftbacktest.data.utils.snapshot.rst","docs/reference/hftbacktest.data.utils.tardis.rst","docs/reference/initialization.rst","docs/reference/stats.rst","docs/requirements.txt","docs/tutorials/Risk Mitigation through Price Protection in Extreme Market Conditions.ipynb","docs/tutorials/examples.rst","examples/Accelerated Backtesting.ipynb","examples/Data Preparation.ipynb","examples/Fusing Depth Data.ipynb","examples/GLFT Market Making Model and Grid Trading.ipynb","examples/Getting Started.ipynb","examples/High-Frequency Grid Trading - Comparison Across Other Exchanges.ipynb","examples/High-Frequency Grid Trading - Simplified from GLFT.ipynb","examples/High-Frequency Grid Trading.ipynb","examples/Impact of Order Latency.ipynb","examples/Integrating Custom Data.ipynb","examples/Level-3 Backtesting.ipynb","examples/Making Multiple Markets - Introduction.ipynb","examples/Making Multiple Markets.ipynb","examples/Market Making with Alpha - APT.ipynb","examples/Market Making with Alpha - Basis.ipynb","examples/Market Making with Alpha - Order Book Imbalance.ipynb","examples/Order Latency Data.ipynb","examples/Pricing Framework.ipynb","examples/Probability Queue Models.ipynb","examples/Queue-Based Market Making in Large Tick Size Assets.ipynb","examples/Working with Market Depth and Trades.ipynb","examples/bybit/btcusdt_20250926.gz","examples/cm/btcusd_perp_20240808.gz","examples/cm/btcusd_perp_20240809.gz","examples/cm/ethusd_perp_20240808.gz","examples/cm/ethusd_perp_20240809.gz","examples/example.py","examples/example_bybit.py","examples/example_hyperliquid.py","examples/example_mexc.py","examples/hyperliquid/btcusd_20250126.gz","examples/mexc/btcusdt_20250126.gz","examples/spot/btcusdt_20240808.gz","examples/spot/btcusdt_20240809.gz","examples/spot/ethusdt_20240808.gz","examples/spot/ethusdt_20240809.gz","examples/usdm/btcusdt_20240808.gz","examples/usdm/btcusdt_20240809.gz","examples/usdm/ethusdt_20240808.gz","examples/usdm/ethusdt_20240809.gz","hftbacktest-derive/Cargo.toml","hftbacktest-derive/src/lib.rs","hftbacktest/Cargo.toml","hftbacktest/README.md","hftbacktest/examples/1_ticker.py","hftbacktest/examples/2_download_tardis.py","hftbacktest/examples/3_convert.py","hftbacktest/examples/4_latency.py","hftbacktest/examples/5_backtest.py","hftbacktest/examples/6_gridsearch.py","hftbacktest/examples/algo.rs","hftbacktest/examples/custom_evhandling.rs","hftbacktest/examples/gridtrading.ipynb","hftbacktest/examples/gridtrading_256assets.ipynb","hftbacktest/examples/gridtrading_backtest.rs","hftbacktest/examples/gridtrading_backtest_args.rs","hftbacktest/examples/gridtrading_live.rs","hftbacktest/examples/gridtrading_live_bybit.rs","hftbacktest/examples/live_order_error_handling.rs","hftbacktest/examples/logging_order_latency.rs","hftbacktest/examples/tickers.json","hftbacktest/examples/tickers_256.json","hftbacktest/src/backtest/assettype.rs","hftbacktest/src/backtest/data/mod.rs","hftbacktest/src/backtest/data/npy/mod.rs","hftbacktest/src/backtest/data/npy/parser.rs","hftbacktest/src/backtest/data/reader.rs","hftbacktest/src/backtest/evs.rs","hftbacktest/src/backtest/mod.rs","hftbacktest/src/backtest/models/fee.rs","hftbacktest/src/backtest/models/latency.rs","hftbacktest/src/backtest/models/mod.rs","hftbacktest/src/backtest/models/queue.rs","hftbacktest/src/backtest/order.rs","hftbacktest/src/backtest/proc/l3_local.rs","hftbacktest/src/backtest/proc/l3_nopartialfillexchange.rs","hftbacktest/src/backtest/proc/local.rs","hftbacktest/src/backtest/proc/mod.rs","hftbacktest/src/backtest/proc/nopartialfillexchange.rs","hftbacktest/src/backtest/proc/partialfillexchange.rs","hftbacktest/src/backtest/recorder.rs","hftbacktest/src/backtest/state.rs","hftbacktest/src/depth/btreemarketdepth.rs","hftbacktest/src/depth/fuse.rs","hftbacktest/src/depth/hashmapmarketdepth.rs","hftbacktest/src/depth/mod.rs","hftbacktest/src/depth/roivectormarketdepth.rs","hftbacktest/src/lib.rs","hftbacktest/src/live/bot.rs","hftbacktest/src/live/ipc/config.rs","hftbacktest/src/live/ipc/iceoryx.rs","hftbacktest/src/live/ipc/mod.rs","hftbacktest/src/live/mod.rs","hftbacktest/src/live/recorder.rs","hftbacktest/src/prelude.rs","hftbacktest/src/types.rs","hftbacktest/src/utils/aligned.rs","hftbacktest/src/utils/mod.rs","py-hftbacktest/Cargo.toml","py-hftbacktest/LICENSE","py-hftbacktest/README.md","py-hftbacktest/README.rst","py-hftbacktest/hftbacktest/__init__.py","py-hftbacktest/hftbacktest/binding.py","py-hftbacktest/hftbacktest/data/__init__.py","py-hftbacktest/hftbacktest/data/utils/__init__.py","py-hftbacktest/hftbacktest/data/utils/binancefutures.py","py-hftbacktest/hftbacktest/data/utils/binancehistmktdata.py","py-hftbacktest/hftbacktest/data/utils/bybit.py","py-hftbacktest/hftbacktest/data/utils/bybithistmktdata.py","py-hftbacktest/hftbacktest/data/utils/databento.py","py-hftbacktest/hftbacktest/data/utils/difforderbooksnapshot.py","py-hftbacktest/hftbacktest/data/utils/feed_order_latency.py","py-hftbacktest/hftbacktest/data/utils/hyperliquid.py","py-hftbacktest/hftbacktest/data/utils/mexc.py","py-hftbacktest/hftbacktest/data/utils/migration2.py","py-hftbacktest/hftbacktest/data/utils/snapshot.py","py-hftbacktest/hftbacktest/data/utils/tardis.py","py-hftbacktest/hftbacktest/data/validation.py","py-hftbacktest/hftbacktest/intrinsic.py","py-hftbacktest/hftbacktest/order.py","py-hftbacktest/hftbacktest/recorder.py","py-hftbacktest/hftbacktest/state.py","py-hftbacktest/hftbacktest/stats/__init__.py","py-hftbacktest/hftbacktest/stats/metrics.py","py-hftbacktest/hftbacktest/stats/stats.py","py-hftbacktest/hftbacktest/stats/utils.py","py-hftbacktest/hftbacktest/types.py","py-hftbacktest/pyproject.toml","py-hftbacktest/rustfmt.toml","py-hftbacktest/src/backtest.rs","py-hftbacktest/src/depth.rs","py-hftbacktest/src/fuse.rs","py-hftbacktest/src/lib.rs","py-hftbacktest/src/live.rs","py-hftbacktest/src/order.rs","py-hftbacktest/tests/test_hftbacktest.py","rustfmt.toml"],"storefront":"/r/nkaz001","claimed":false,"request_supported":{"post":"https://gitbuyer.com/r/nkaz001/hftbacktest/request-supported","requests":0},"note":"indexed from public GitHub; nothing is for sale on this page. Clone it from GitHub. Paid listings live at /search."}