Live code: basic spreader (handling multiple orderbooks in ccxt async)
Example of combining async data with order management, by spreading two perps in binance
Code here: https://github.com/samchaaa/ccxt_async_example/
Intro
Noticed a few new followers, so doing a quick share. This is some code I wish I had much earlier. It handles two issues: getting better execution, and using async operations.
This is for people struggling to learn async, and keep their code organized.
One time I came up with a strategy that looked great on paper, but did not perform live. This was due to assuming fills at last traded price. In real life, to get filled instantly, you need to market in. However this kills a lot of strategies.
Another issue is graduating from using REST APIs to async. This is my MVP for handling continuous data, orders, and execution. If you are new to async (like I was), it may be big leap to wrap all these operations together!
This code is an example of continuously moving your order to best bid/ask, in order to maximize your fill price but also get a fill ASAP (across multiple instruments).
What it does
buy one perpetual, sell another on binance, always staying on the best bid/ask
How to run
python3 main.py binance:BTCUSDT binance:ETHUSDT 1000exchange1:pair1 exchange2:pair2 notional
How it works
Wait for orderbook updates with watch_order_book_for_symbols(), put best bid/ask in a data object (dict of dicts)
Keep track of orders in self.oms (order management system)
Handle order logic in oms.py (if our limit px != best bid/ask, move to best bid/ask)
Here’s the magic line that ties it all together:
Disclaimer and quirks
wait_time: Does not actually do anything, you can update to wait a bit before moving your orders though.
Data.watch_multiple_ob() changes the symbols from “/USDT:USDT” to “USDT” (you may want to modify if integrating spot instruments)
Note these are postonly orders, so if the order book moves quickly (between the time you request bid/ask and submit the order) and the code tries to post at a price that would be taking liquidity, and would break
in this case, there’s a try/except in the OMS logic that will wait half a second, then replace the order again
Currently exchange arguments (in `exchange1:pair1 exchange2:pair2`) don’t do anything, it’s just there to handle multiple exchanges later.
What you can do with it
You can use this code to execute discretionary trades algorithmically (plot out a bunch of correlated pairs, trade divergences long/short).
With addition of spot instruments, you could use it to put on basis trades.
This code can be easily updated to handle:
multiple exchanges
spot as well as perps
vary order size according to how fast the other leg is filling
instead of sizing to notional, size to volatility
I hope this helps someone’s learning curve! Hit me up with input and ideas anytime.

