How to Profit from Statistical Arbitrage in Cryptocurrency Trading Using CCXT Library

Algotron
4 min readMar 24, 2023

--

How to Find Highly Correlated Assets and Trade Them Using CCXT Library

In the world of cryptocurrency trading, finding highly correlated assets and trading them against each other can be a profitable strategy. In this blog post, we will discuss how to find highly correlated assets, how to calculate trading fees, and how to trade them using the CCXT library.

Finding Highly Correlated Assets

The first step in implementing a statistical arbitrage trading strategy is to find highly correlated assets. To do this, you can use historical price data to calculate the correlation coefficient between two or more crypto assets. The correlation coefficient is a statistical measure that indicates the strength of the relationship between two variables. In this case, we are interested in finding crypto assets that move in the same direction most of the time.

To calculate the correlation coefficient between two crypto assets, you can use a spreadsheet program like Microsoft Excel or Google Sheets. Simply input the price data for each asset into separate columns, and then use the CORREL function to calculate the correlation coefficient.

Alternatively, you can use Python and the Pandas library to calculate the correlation coefficient. Here’s an example code snippet:

import ccxt
import pandas as pd

exchange = ccxt.binance()
symbol_1 = 'BTC/USDT'
symbol_2 = 'ETH/USDT'
ohlcv_1 = exchange.fetch_ohlcv(symbol_1, '1d')
ohlcv_2 = exchange.fetch_ohlcv(symbol_2, '1d')
df_1 = pd.DataFrame(ohlcv_1, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df_2 = pd.DataFrame(ohlcv_2, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df_1.set_index('timestamp', inplace=True)
df_2.set_index('timestamp', inplace=True)
df = pd.concat([df_1['close'], df_2['close']], axis=1)
df.columns = ['BTC', 'ETH']
correlation_coefficient = df.corr().iloc[0][1]

In this code snippet, we are using the CCXT library to fetch historical price data for two crypto assets (BTC and ETH) from the Binance exchange. We then use the Pandas library to calculate the correlation coefficient between the two assets.

Calculating Trading Fees

When implementing a trading strategy, it is important to factor in the trading fees into your calculations. Most cryptocurrency exchanges charge fees for each trade that you make. These fees can vary depending on the exchange and the trading pair that you are using. Some exchanges offer reduced fees for high volume traders or for users who hold a certain amount of the exchange’s native token.

To calculate the fees associated with a trade on the Binance exchange using the CCXT library, you can use the following code snippet:

import ccxt

exchange = ccxt.binance()
symbol = 'BTC/USDT'
amount = 0.1
buy_price = exchange.fetch_ticker(symbol)['ask']
sell_price = exchange.fetch_ticker(symbol)['bid']
buy_fee = exchange.calculate_fee(symbol, 'limit', 'buy', amount, buy_price, {})
sell_fee = exchange.calculate_fee(symbol, 'limit', 'sell', amount, sell_price, {})
# Calculate net profit
profit = (sell_price - buy_price) * amount - buy_fee['cost'] - sell_fee['cost']pyt

In this code snippet, we are using the calculate_fee method provided by the CCXT library to calculate the fees associated with a buy and sell order for a given trading pair. We then subtract the fees from the expected profit to calculate the net profit.

Trading Highly Correlated Assets

Once you have identified a pair of highly correlated crypto assets, you need to come up with a profitable trading strategy to trade them against each other. Here are some common strategies:

Mean Reversion

In a mean reversion strategy, you would buy the asset that is trading at a lower price than its historical average and sell the asset that is trading at a higher price than its historical average. This strategy assumes that the prices of the two assets will eventually converge back to their historical averages.

Momentum

In a momentum strategy, you would buy the asset that is showing strong positive momentum and sell the asset that is showing strong negative momentum. This strategy assumes that the prices of the two assets will continue to move in the same direction in the short term.

Pairs Trading

In a pairs trading strategy, you would buy the underperforming asset and sell the outperforming asset. This strategy assumes that the prices of the two assets will eventually converge back to their historical relationship.

To execute these strategies using CCXT library, you can use the following code snippet:

import ccxt

exchange = ccxt.binance()
symbol_1 = 'BTC/USDT'
symbol_2 = 'ETH/USDT'
amount = 0.1
buy_price = exchange.fetch_ticker(symbol_1)['ask']
sell_price = exchange.fetch_ticker(symbol_2)['bid']
buy_fee = exchange.calculate_fee(symbol_1, 'limit', 'buy', amount, buy_price, {})
sell_fee = exchange.calculate_fee(symbol_2, 'limit', 'sell', amount, sell_price, {})
spread = (sell_price - buy_price) * amount
net_profit = spread - buy_fee['cost'] - sell_fee['cost']
# Mean Reversion
if spread > mean_spread:
order_1 = exchange.create_limit_buy_order(symbol_2, amount, sell_price)
order_2 = exchange.create_limit_sell_order(symbol_1, amount, buy_price)
elif spread < -mean_spread:
order_1 = exchange.create_limit_buy_order(symbol_1, amount, buy_price)
order_2 = exchange.create_limit_sell_order(symbol_2, amount, sell_price)
# Momentum
if momentum_1 > momentum_2:
order_1 = exchange.create_limit_buy_order(symbol_1, amount, buy_price)
order_2 = exchange.create_limit_sell_order(symbol_2, amount, sell_price)
else:
order_1 = exchange.create_limit_buy_order(symbol_2, amount, sell_price)
order_2 = exchange.create_limit_sell_order(symbol_1, amount, buy_price)
# Pairs Trading
if spread > std_spread:
order_1 = exchange.create_limit_buy_order(symbol_2, amount, sell_price)
order_2 = exchange.create_limit_sell_order(symbol_1, amount, buy_price)
elif spread < -std_spread:
order_1 = exchange.create_limit_buy_order(symbol_1, amount, buy_price)
order_2 = exchange.create_limit_sell_order(symbol_2, amount, sell_price)

In this code snippet, we are using the CCXT library to execute trades on the Binance exchange. We first fetch the current ask price for asset 1 (BTC/USDT) and the current bid price for asset 2 (ETH/USDT). We then use the spread between the two assets to determine which trading strategy to use. The mean_spread and std_spread variables are calculated based on historical data. The trading fees are calculated using the calculate_fee method and are subtracted from the spread to calculate the net profit.

Conclusion

In this blog post, we have discussed how to find highly correlated crypto assets, how to calculate trading fees, and how to trade them against each other using the CCXT library. By following these steps and implementing a profitable trading strategy that takes trading fees into account, you can take advantage of statistical arbitrage opportunities in the world of cryptocurrency trading.

--

--

Algotron
Algotron

Written by Algotron

Follow me for algorithmic trading insights.

No responses yet