# Slippage Analysis - Part 3

Posted December 11, 2020

I ran some backtests overnight to try to work out if latency is likely an issue. If the slippage issue is due to latency in production and the simulation is accurate then we would expect to see the same negative effect of latency on pnl in the simulations.

Results from the simulation over the last 30 days.

Latency has an effect on pnl, but it is not that significant and doesn’t explain the amount of slippage we are seeing in live trading. My deductions from this are the problem is possibly one few things

• An issue with the historical market data used for simulation
• I have gone to great lengths to make sure it is of the best quality possible. The timestamps between exchanges are synchronised and the data is clean and without gaps. I record data myself in multiple locations and have automated mechanisms for comparing the data sets and finding data issues.
• A problem with the simulation environment
• This is more likely to be a problem but for performance reasons, it is quite simple. This simplicity has the added benefit of making the code easy to unit test and check for correctness.
• Some other problem with the production execution strategy
• It would thus seem from what I know about the code, it is the most likely issue is related to the production execution code.

## Issue

Order Management Code for Tracking Touch

TLDR; Check if there are no orders and if not place one. If there is an order in the market but the price is different to touch cancel it.

The issue seemed to lie here on line 32

The code was repurposed from a different strategy and has some odd issues with the order book cleaning code. Liquidity is cleaned from the limit order book when there is a trade at the touch price. If the current best bid is 200 contracts @ 18000.05 and there is a sell trade on the public feed of 200 lots for 18000.05. It is faster to then update the order book and remove the level @ 18000.05 compared with simply waiting for the change to be affirmed by the order book feed.

In exchanges like CME, you would expect in these cases the order was filled. How can more volume trade on the bid than the volume available? Either there is hidden size in the book which seems unlikely given there is not an order type for icebergs or hidden size. There is the ability to set take profit and stop losses on orders in BYBIT so likely conditional orders are being executed that are not shown in the order book. It might also just be BYBIT been doesn’t have synchronisation between the REST order book feed and the trade feed. In the Chicago mercantile exchange (CME) for instance market data, messages have monotonically increasing sequence ids and unless there is packet loss you would expect a trade update and then a book update with the same sequence ids. The code was pulling the order thinking the order book had moved but somehow the order was pulled before we were filled. I will at some point have to do some more work on the book building but this is something that I have seen before and is a useful microstructure signal especially in times when the market spreads are wide.

The fix was to change line 32 with this.

The main differences are

• Only move bid orders up and ask orders down
• Use order modify to improve the amount of time we are in the market and limits the risk of overfill.

## Limit Order Book Plots

Limit order book plots are a useful tool. Given a specific point in time, the chart shows the limit order books, strategy orders, exchange trades, fills, etc. If any fills look problematic or times the strategy lost money it’s easy to isolate the information there. The limit order books contain a lot of information and when that is extended over time it is almost impossible to go through the data line by line. The LOB chart is a powerful way to collate this data and to make it easier to understand what is going on.

Charts are normally just the start of the investigation, once some hypothesis have been defined they can either be tested in simulation or experiments be designed to test them in production via A/B testing.

The limit order book charts allow us to drill down on fills that have bad slippage to see what happened.

A buy order with 30 USD of slippage.

The live execution makes sense because the market never ticked down until around the time order was filled. It however looks like the paper buy order was placed at an off-market price which is why it was filled without the market moving. The limit order book plot is really useful to understand the actions of the strategy and to investigate any executions of interest.

## Next Steps

• Understand what is going on with the paper fills
• Collect more live trade data to confirm the effect of the code fix.