barbotine-logo

Presentation

1/ What is it?

Barbotine's arbitrage bot is a cross-exchange crypto arbitrage system that you can run on any machine running Python 3. It's built on top of an open-source library, CCXT, which manages the connectors to exchanges' APIs and offer a centralized way to connect to more than 50 crypto exchanges.

The demo mode is available for free on Github, it works with paper money, fake money, so you can test and see how it works. There is a full version that works live with real money on real crypto markets. Note that the demo mode might show different results than live mode due to market factors like filling of orders that can't be simulated with fake money.

The bot has an embedded strategy featuring a no-transfer rebalancing system, meaning that it's able to take cross-exchange arbitrage opportunities without transferring the coins from one exchange to another.

The bot can work simultaneousely with an infinite number of exchanges (even if opportunities only involve two exchanges), but the rebalancing system means that it can only handle one pair at a time. You can run multiple instances of the bot, but you'd have to allocate proportionally less money on each individual instance.

The infrastructure of the bot is pretty simple: a file named main.py is like the orchestra conductor, it launchs the bot script (either bot.py for the full version or bot-fake-money.py for the demo version) with the subprocess Python module. When you want to run the bot, you use the file named run.py, which calls the main.py script.

2/ More technical details

Barbotine's arbitrage bot uses an asynchronous loop to fetch orderbooks faster on all exchanges simultaneousely (using websockets).

It logs everything in a logs folder, all taken opportunities and information/errors in separate .txt files, using simple Python functions to write in .txt files.

The bot works with what I call 'sessions'. At the end of each session, a rebalancing happens (See more details about rebalancing). You have to input a session renewal delay, after this delay, it will end the current session and start a new one. This cycle is repeated indefinitely unless you stop the bot. You can also disable automatic session renewal in the config file (exchange_config.py). You can also trigger a manual session renewal by typing 'Enter' on your keyboard when in a session.

3/ Presentation by video

Installation and usage

1/ Installation guide

1. Clone the Github repository (or download the zip file from Github website)

git clone https://github.com/nelso0/barbotine-arbitrage-bot

2. Go to the folder in your terminal/cmd

cd barbotine-arbitrage-bot

3. Install the required Python modules

pip install -r requirements.txt

4. Set your configuration details in exchange_config.py

5. Run with:

python run.py

2/ Usage

You can run it directly with python run.py, and the bot will ask you the parameters, but you can be quicker and run it direclty with the parameters as arguments, just like this:

python run.py <mode> [renew-time-minutes] <balance> <pair> <exchanges list separated by commas (no space!)>

Some details about the parameters:

mode --> The mode you wanna use between 'fake-money' or 'real'

renew-time-minutes --> The time in minutes that sessions should last. Only applicable if renewal enabled.

balance --> The total balance in quote currency (most of the time in USDT) you want the bot to run with.

pair --> The pair you want the bot to run on. Should be the exact same on all exchanges.

exchanges-list --> The list of exchanges you want the bot to run on. Each should be configured in the config.

Examples of usage:

python run.py fake-money 60 1000 SOL/USDT binance,okx,kucoin # run the bot with paper money with renewal enabled happening every 60 min. on Binance, OKX and Kucoin, on the SOL/USDT pair.python run.py real 3500 ETH/BTC poloniex,bitmex,bingx # run the bot with real money on Bitmex, Poloniex and BingX, on the ETH/BTC pair.

3/ Configuration

In this part, we're gonna breakdown every setting of exchange_config.py.

* = Mandatory for demo and real modes

* = Mandatory only for real mode

renewal*

(boolean) You can enable or disable renewal. If you enable it, you need to specify a renewal delay when running the bot (see Usage). Default: False

delta_neutral*

(boolean) Enable if you want to hedge the purchase of cryptos for rebalancing with a short order, to remain in a delta-neutral situation. If enabled, you need to fill in the necessary configuration in the file delta_neutral_config.py. More details in Rebalancing & hedging.

python_command*

(string) The command you put in the terminal/cmd to launch python. Usually: python, python3, py...

exchanges*

(dict) It's a dictionnary in a dictionnary. The key is exchange's ID (like here), and value is a dictionnary containing necessary API details of that exchange for CCXT. Most of the time, it's this: {'apiKey':'here','secret':'here'} and replace 'here' with your API credentials. Some exchanges need other parameters, just include them in this dictionnary.

apiToken

(string) Telegram API details to send everything to you, don't fill unless you want telegram notifications. If so, this is the apiToken of your telegram bot.

criteria_pct

(float) Minimum of price difference in % to take the opportunity. Default: 0

criteria_usd

(float) Minimum of profit in quote currency (= the second asset of the pair, most of the time USDT). Default: 0

first_orders_fill_timeout

(float) Put a value for the timeout in minutes of the rebalancing orders filling. 0 means deactivated, it means it waits indefinitely for the orders to get filled (default).

demo_fake_delay

(boolean) Enable or disable a fake ping delay to better simulate the order filling (see Strategy & FAQ for more details). Default: False

demo_fake_delay_ms

(int) Necessary if demo_fake_delay is enabled. The fake ping delay in milliseconds. Note that if enabled, the bot will have to fetch the orderbooks again, which will take time anyway, even if you put this value at 0. demo_fake_delay_ms is additional delay. For example, if you enable demo_fake_delay, put demo_fake_delay_ms at 0, and fetching an orderbook on your machine takes 1 second, the bot will technically add a 1 second fake delay, because fetching orderbooks again isn't instantaneous.

Strategy & FAQ

1/ FAQ

What is the difference between demo/fake-money mode and real mode?

As its title says, the fake-money mode works with a fake balance, and the real mode actually sends orders with your real account balance on the real markets. The fake-money/demo mode simulates everything including trading fees (for both rebalancing orders and opportunity orders) and works with the same prices as the real mode (best bid/best ask).

The bot currently uses market orders to take opportunities (more details in the next section "Strategy"), so the fake-money/demo mode assumes you have a 0ms ping delay and that you get filled at the best bid/ask price fetched when the order is sent. You can enable a setting to simulate a custom ping-delay between the time at which the price is fetched and the time at which the fake order is received and executed by the exchange (see demo_fake_delay in Configuration).

How much profit can I except?

It depends on you, you have to ask this question to yourself and your strategy. The point of this open-source bot is to make it yours. With this bot, I tried to make your life as easier as I can, but I can't do the job for you (and nobody can). When to run it, where to run it, how to run it, with what settings, and why... These are questions you need to answer in order to be profitable. You can see it as a hard task, but in my opinion, it's the beauty of algo-trading. Anyone can create their own strategy and it has to be unique. You are rewarded for creating your own path, isn't it beautiful?

I have errors in the terminal/cmd but the bot doesn't stop, is it a bug?

No, as the bot works with asynchronous websockets to fetch the orderbooks at a very fast pace, some exchanges can close the connection unexpectedly. That's why Barbotine's arbitrage bot has a built-in automatic reconnecting system that handles the reconnection for you. The errors like ConnectionResetError are simply messages telling you that the connection was closed 'brutally', unexpectedly. It's exactly the same as when you pull out an SD card without ejecting it first, it's harmless.

2/ Rebalancing & hedging

As said in Technical details, the bot rebalances automatically each X seconds (if you enable renewal). When you start the bot, you only need the quote currency of the pair you choosed. For example, if you run the bot on BTC/USDT, you only need USDT in all exchanges. Before starting the bot, each exchange should have:

balanceper exchange=totalbalancenumberofexchanges(inquotecurrency)

For example, if you run the bot with 1000 as total balance, on the BTC/USDT pair with 4 exchanges, you need to put 1000/4 = 250 USDT in each exchange (spot wallet).

To take opportunities, the bot doesn't transfer the coins. It means it has to buy & sell on both exchanges. This way, we can avoid transfer fees and delay. But in order to do that, we need to have quote-currency AND base-currency. For example, with the BTC/USDT pair, we have to own USDT and BTC in each exchange, to be able to buy BTC on exchange 1, and sell BTC on exchange 2. So, when you start the bot, it will automatically buy base-currency (BTC) on each exchange, with the half of quote-currency (USDT) that you put in it before. At each rebalancing/start of the bot, the balances will look like this:

rebalancing

This is what it will look like with 3 exchanges, 750 total balance, on the BTC/USDT pair. When an opportunity arises, it will calculate all possible opportunities, and the impact on the balances (including all fees). If it's profitable and match criteria_usd and criteria_pct, it immediately starts sending the orders. Here's a schema of what happens to the balances of the 2 exchanges:

opportunity

Assuming you want profits in quote-currency (most of the time USDT) and not in base-currency, at the each rebalancing, the bot sells everything back to quote-currency, and buys the appropriate quantity of base-currency to end up having the necessary balances to take opportunities (first schema). This would be perfect for a pair like USDC/USDT, because the ratio always stays equal to 1. But for any other volatile pair like BTC/USDT, if BTC goes down, you'll money when rebalancing happens. That's why Barbotine's arbitrage bot integrates a "delta-neutral" feature. This allows you to configure a futures/perpetual exchange, to automatically place a short order with the appropriate quantity, close it when rebalancing happens, and repeat. Here's a schema describing how the hedging works:

hedgine

3/ Things to do next

- Rewrite it in other languages such as JS

- Add an UI