Code Pack - bybitstatarb

ByBit Pairs Trading Cointegrated Currency Scanning Tool

Find crypto pairs on the ByBit exchange that have an objective statistical edge for potential mean reversion

Technologies
python
Themes
arbitrage
crypto
trading
Skill Level
intermediate
Last updated
2023-08-22

What's Inside

We can detect promising pairs to assess for trading based on mathematical relationships. In this code package, we use the ByBit Python API to extract price history for a large array of Crypto pairs and assess their mathematical relationship using a statistical tool known as cointegration.

With this knowledge, we can detect pairs that are strongly bound together in the timeframe assessed. Meaning, we can trade the difference between the two pairs, rather than speculating on price. This difference is known as the spread.

Uses

-
Find strongly cointegrated pairs for trading on a well-known Crypto exchange
-
Develop an understanding of using the statsmodels Python library
-
Save advanced statistical information for use in trading bots (as seen in Udemy course)

Requirements

-
You need experience in creating python virtual environments and downloading python packages. That's pretty much the only programming experience needed.
-
However, you must also understand the nature of the strategy. If you are not familiar with statistical arbitrage, the Crypto Wizards YT channel and platform has much information on the topic.

START

What you need to get running

You will need Python 3.7 or higher to work with this code. The code was developed with Python version 3.10.8 and pip version 22.2.2.

Step 1

Download package

Click the download button at the top of this page. Then save the project on your desktop of project folder. Going forward, we will assume a project name as 'myproject'.
However, you may of course name the project folder whatever makes sense for your purpose.

Step 2

Install Python packages

Change directory into your project folder where the code package is saved:

$(root/)

cd myproject
Create a virtual environment:

$(root/myproject)

python3 -m venv venv
Activate your venv (MAC):

$(root/myproject)

source venv/bin/activate
Activate your venv (WINDOWS):

$(root/myproject)

source venv/Scripts/activate
Install packages

$(venv)(root/myproject)

pip3 install statsmodels==0.13.5
pip3 install numpy==1.24.2
pip3 install pandas==1.5.3
pip3 install matplotlib==3.6.3
pip3 install pybit==2.4.1

Step 3

Run the code

Run your strategy

$(venv)(root/myproject)

python3 main_strategy.py

This code can take quite some time to run and find pairs. So let it run.

The reason is, it is hunting through a vast amount of prices on the exchange and comparing price histories for every single possible combination.

Once the code has run, you will be presented a comparison chart of a selected pair. You can of course change this as shown in the video.

CONGRATULATIONS

Statistical arbitrage analysis complete

You can now start exploring the code and testing things out. The point is you now have objective code to analyse the markets as taught in the Udemy course. In this course, we even build a bot to trade the signals automatically. Regardless, this code in scanning for opportunities and cointegrated pairs is where the true value is at.