hftbacktest - High-Frequency Trading and Market-Making Backtesting Tool
Quant Finance Tool Monday - High-Frequency Trading and Market-Making Backtesting Tool
Happy Monday, Friends!
A 5-minute introduction to a Quantitative Finance tool.
This week’s Monday’s tool:
hftbacktest - High-Frequency Trading and Market-Making Backtesting Tool.1
Backtesting is useful
Backtesting aims to experiment with your trading strategies by applying them to historical market data. This process allows you to evaluate the potential effectiveness of your strategies by analyzing their performance in past market conditions. Essentially, it serves as a predictive tool, offering valuable insights into the profitability and robustness of your approaches. By doing so, you can refine your strategies, identify any vulnerabilities, and optimize your decision-making processes without the immediate financial risks of real-time trading.
Backtesting is usually behind a paywall
Given the importance of backtesting tools, prop desks, banks, and hedge funds will all have proprietary backtesting tools that focus on their specific markets. So what’s interesting about this tool is that it’s a) open source, b) MIT license, and c) written in Python so it’s relatively straightforward to understand and build on top of it.
What this tool offers
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 of the Tool
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.
Example from the webpage
Some Python code2
@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)
That’s all for today :) Have a great one!
For more Quant Finance treats, check out our archives.
Stay quanty!
All the best,
Sebastian
https://github.com/nkaz001/hftbacktest
https://hftbacktest.readthedocs.io/en/latest/index.html