XEMM HIP: latency optimization

Author: Jelle {Cryptobot} 0xe8f

Original Post Date: 4 Jul 2022

General Description:

This is part of a 4 part HIP consisting of:

  • XEMM triangular arbitrage
  • XEMM automatic rebalancer
  • XEMM basic changes
  • XEMM latency optimization

In the past two months, I have significantly improved the XEMM strategy, which allows me to better manage slippage, run XEMM with triangular arbitrage, significantly scale up campaign exposure, and spend 60% less time on managing my XEMM strategies.

Unfortunately, as I am not a developer, my code is written unprofessionally and is for personal use only. However, as I think all users will benefit from these improvements, I think these ideas should be further developed by a professional programmer. My code can be used as a guide, and I will further assist (if needed) any developer who wants to further develop these ideas.

This proposal will first be published on the commonwealth forum so users can add comments, give their opinions and share ideas. Later, the proposals will be put up for a vote as a HIP.

Latency optimization:

The most important parts of the XEMM strategy are:

  • Calculating the effective hedging price, to prevent adverse selection

  • Executing the taker order to lock in profits

In order to reduce adverse selection updating the effective hedging price frequently is essential. For some markets, a 1-second tick size is not enough. In order to more easily adjust the frequency at which the effective hedging price is updated (tick_size), allowing users to define the tick size within the global configurations would be useful.

Secondly and most importantly, the ‘order fill event’ function in the strategy is not written efficiently and could be optimized in order to reduce latency. The most important task of the strategy is to recognize a maker order fill event and place an order on the taker exchange as quickly as possible. These two components should be designed such that they have priority over any other process within the strategy and thereby reduce latency. Small decreases in latency could mean the difference between a profitable trade and an unprofitable one. HFT pros even take into account the length of the function name as a computer can read ‘order ’ quicker than ‘cplace orderat specificprice andspecific size’.

See below two interesting videos on how quants speed up their code:

One way of reducing latency is by prioritizing the taker order placement over any other tasks within the code. A simple example, the taker order placements is more important than logging output on the terminal. Order placement should therefore be placed in front of the logging component. See code below (line 671) on how this could be optimized in the XEMM strategy:

By looking at the code in general, here are some changes I would make to the strategy to reduce latency.

  • Place c_check_and_hedge_order function at top of the c_did_fill_order function (around line 471 in cross_exchange_market_making.pyx)
  • Skip available_balance check in c_check_and_hedge_order (line 652) and simply use buy_fill_quantity as quantity to hedge. Available balance is already monitored every tick, extra check could be redundant.
  • Make taker order price dependent on avg_fill_price + min_profitability + slippage_buffer instead of the price_for_quote_volume + slippage buffer. This first seems like a less expensive computation than requesting the price_for_quote_volume and could therefore reduce latency. In addition, it allows for better management of taker order placemen.

I bet there are other ways to improve latency, potentially within the order_fill_event part of the code. However these are simple, but I think essential improvements to the strategy.

I would like to get your opinion on this idea before submitting this as a HIP. And would like to start a general discussion on how trading latency can be further reduced within Hummingbot’s code.

Feel free to DM me on discord: @Jelle {Cryptobot}

1 Like