I Created a Crypto Arbitrage Trading Bot With Python

πŸ›‘ Disclaimer: NOT INVESTMENT ADVICE!

In this short project, I’ll explain a Python trading bot I used for the purpose of arbitrage trading.

I use Bitcoin BTC, but the arbitrage bot works better on illiquid and inefficiently priced coins — Bitcoin is usually far too liquid and efficiently priced for this to work. I also assume an exchange rate of 1 GBP > 1 EUR > 1 USD.

How Does It Work?

The Bitcoin arbitrage bot continuously checks the prices of Bitcoin and looks for very simple, almost trivial arbitrage opportunities.

  • If you can sell Bitcoin at a EUR price above the USD price and you assume EUR is worth more than USD, you buy Bitcoin for USD and sell it for EUR.
  • If you can sell Bitcoin at a GBP price above the USD price and you assume GBP is worth more than USD, you buy Bitcoin for USD and sell it for GBP.
  • If you can sell Bitcoin at a GBP price above the EUR price and you assume GBP is worth more than EUR, you buy Bitcoin for EUR and sell it for GBP.

When these trivial opportunities are found, the bot will execute the appropriate trade to take advantage of the price difference.

πŸ’‘ Info: Don’t expect this to work for highly liquid and efficiently priced trading pairs — but it may work for inefficiently priced trading pairs. That’s where all the arbitrage opportunities are!

I have actually replaced the execution of the concrete trade with a simple print() statement to make it more generalized — no matter which trading tool you’re actually using.

The loop is set to sleep for 60 seconds before checking for arbitrage opportunities again.

import time
import requests 
import pandas as pd
import numpy as np

def get_price_data():
    '''
    Get the latest crypto price data from various exchanges
    '''
    url = 'https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC&tsyms=USD,EUR,GBP'
    resp = requests.get(url=url)
    data = resp.json()
    return data

def calculate_arbitrage(data):
    '''
    Calculate the arbitrage opportunity between exchanges
    '''
    # Get the exchange prices
    btc_usd = data['BTC']['USD']
    btc_eur = data['BTC']['EUR']
    btc_gbp = data['BTC']['GBP']
    # Calculate the arbitrage opportunity
    usd_eur_diff = btc_eur - btc_usd
    usd_gbp_diff = btc_gbp - btc_usd
    eur_gbp_diff = btc_gbp - btc_eur
    
    return usd_eur_diff, usd_gbp_diff, eur_gbp_diff

def trade_arbitrage(usd_eur_diff, usd_gbp_diff, eur_gbp_diff):
    '''
    Execute an arbitrage trade
    '''
    if usd_eur_diff > 0:
        # Buy BTC with USD and sell it for EUR
        # Profit from USD->EUR difference
        print('Executing USD->EUR arbitrage trade')
    elif usd_gbp_diff > 0:
        # Buy BTC with USD and sell it for GBP
        # Profit from USD->GBP difference
        print('Executing USD->GBP arbitrage trade')
    elif eur_gbp_diff > 0:
        # Buy BTC with EUR and sell it for GBP
        # Profit from EUR->GBP difference
        print('Executing EUR->GBP arbitrage trade')
    else:
        print('No arbitrage opportunity.')

if __name__ == '__main__':
    while True:
        data = get_price_data()
        usd_eur_diff, usd_gbp_diff, eur_gbp_diff = calculate_arbitrage(data)
        trade_arbitrage(usd_eur_diff, usd_gbp_diff, eur_gbp_diff)
        time.sleep(60)

This code is a Python program that implements a Bitcoin arbitrage trading bot. It works by continually checking the prices of Bitcoin on various exchanges and looking for arbitrage opportunities.

Detailed Explanation

The program starts by importing the necessary libraries, such as requests, pandas, and numpy.

Next, you define the function get_price_data() that uses the CryptoCompare API to retrieve the latest prices of Bitcoin from various exchanges. The data is returned in JSON format.

print(data)
{'BTC': {'USD': 21149.49, 'EUR': 19603.81, 'GBP': 17252.73}}

You define the calculate_arbitrage() that takes in the data returned by get_price_data() and calculates the arbitrage opportunities between the different exchanges.

It returns the differences in the prices of Bitcoin in USD-EUR, USD-GBP, and EUR-GBP.

You define the trade_arbitrage() function that takes in the arbitrage opportunities calculated by the previous function and executes the appropriate trade based on the opportunity.

  • If the USD-EUR arbitrage opportunity is positive, the bot will buy Bitcoin with USD and sell it for EUR.
  • If the USD-GBP arbitrage opportunity is positive, the bot will buy Bitcoin with USD and sell it for GBP.
  • If the EUR-GBP arbitrage opportunity is positive, the bot will buy Bitcoin with EUR and sell it for GBP.

Finally, the program enters an infinite loop where it continuously checks for arbitrage opportunities and executes trades when necessary. The loop is set to sleep for 60 seconds before checking for arbitrage opportunities again.

πŸ‘‰ Recommended: How to Stop an Infinite While Loop in Python