XEMM HIP: Auto Rebalancer

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 Minor Improvements
  • 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.

XEMM Auto rebalancer:

The goal of a XEMM trader is to trade delta neutral and profit from a (forced) arbitrage opportunity. However, profit is not guaranteed as we encounter execution errors. This results in an unintended increased/decreased exposure to an asset. Whenever these execution errors occur, traders (in general) manually rebalance their positions by buying or selling on the maker or taker market to get their total exposure back to the preferred target balance. This is time consuming and can cause serious losses as you could notice it hours after it happened, while the price has moved in the opposite direction.

A simple example of execution error and it’s impact:

A maker sell order is filled, subsequently, the strategy places a taker buy order aiming to capture a profit from the arbitrage opportunity. However, the taker order is not filled as the price has already moved and did not come back. Traders might have hedged their position by shorting the asset (e.g. AVAX) on the perpetual market. Because the market moved up and you decreased your exposure to AVAX you lose money on the trade. This also works the other way around, a maker buy fill without a successful taker sell fill.

Because this happens regularly, you have to keep monitoring the bot throughout the day, which is a time-consuming process. You will have to manually buy back or sell assets to rebalance the total exposure to an asset to your prefered target balance. This can result in serious losses as you will only find out about this problem once you check the bots.

The rebalancing process can be automated, which will result in quicker rebalancing, which can result in a smaller loss and result in less time that you have to spend managing the strategy. This small loss might be preferred over a potential big loss if the market has moved significantly in the other direction.

The improvement/idea:

An automatic balance fixer in which you specify a specific target balance, which is the target base balance you would like ot hold. Once the combined base balance of the taker and maker exchange deviates by a specific min order amount from this target balance, the bot will wait a specified amount of seconds (waiting time) and check whether there is still an imbalance between the target balance and the combined base balance. If there is still an imbalance after the waiting time, the bot will automatically buy or sell assets to get the total balance on both exchanges back to the target_balance.

Simply said: if your total base balance on the maker and taker combined is more or less than the specified target balance for longer than ‘waitingtime’ seconds, the bot will restore the balance by market buying or selling on a specific market.

For now, I have designed it in a way that the rebalancing order is a market order which prioritizes placing the order on the taker exchange (as this one is generally a more liquid exchange). But it might be better to have the restoring order be a maker order on the maker exchange (to earn LM rewards). This would be done by maintaining the top position until the order is filled and the balance is restored.

This solution has allowed me to minimize the cost of execution risk and allowed me to spend around 60% less time on managing bots.

I would like to get your opinion on this idea before submitting this as a HIP.

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

Github link: GitHub - jellebuth/Jelle-V2.2


Jelle, it is a great idea. I have a similar rebalancer that is running from the external monitor and thanks to your idea I am going to rewrite it to be inside the hb strategy.
I also send a notification about the rebalancing, so I can track the issue, check if everything is ok and to explore why this rebalance happened.


Thank you very much for your service
Looking forward to your strategy