Building a crypto pairs trading strategy in Python

Crypto pairs trading

Fadai Mammadov
Coinmonks
Published in
5 min readFeb 5, 2024

--

Let’s go straight to the point without any fluff.

Below is an implementation of pairs trading strategy based on Binance data. First, we import several libraries.

Then, we create a list containing multiple coins trading at Binance.

You can, of course, add or remove as many items from this array as you wish.

In the next lines we define variables pair, root_url interval which allow us to fetch historical data from Binance. Then we read data with the following line:

data = json.loads(requests.get(url).text)

When you look at the content of data variable, you’ll see something like this:

Doesn’t look nice, does it? Let’s convert it to data frame with the following:

df = pd.DataFrame(data)

Now our df looks like this:

Much better now, but we don’t know those column names mean. This is where Binance API docs can be useful.

We’ll rename our columns with the following code:

df.columns = ['open_time',
'o', 'h', 'l', 'c', 'v',
'close_time', 'qav', 'num_trades',
'taker_base_vol'…

--

--