I'm having an hard time understanding how cointegration works. Basically i'm trying to find cointegrated pairs in the crypto market, so i do the following:
- Get OHLC data for the two markets (i'm getting 5k candles on the 5m timeframe)
- Get the log returns for the two markets
- Check the p-value when i look for cointegration between the log returns
The problem with my code is that i always get very low p-values, no matter what market i try, and on some markets the p-value is 0.
Here is my code:
import pandas as pd
import json
import numpy as np
import requests
import time
import json
import mplfinance as mpf
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import statsmodels.tsa.stattools as ts
import threading
import scipy
import pandas_ta as ta
import ccxt
from pykalman import KalmanFilter
ftx = ccxt.ftx()
def get_ohlc_ccxt(market, timeframe):
data = ftx.fetch_ohlcv(market, timeframe, limit=5000)
ohlcv = pd.DataFrame(data, columns=['time', 'open', 'high', 'low', 'close', 'volume'])
ohlcv = ohlcv.drop_duplicates(subset=['time', 'open', 'high', 'low', 'close', 'volume'], keep='first')
ohlcv['time'] = ohlcv['time'].astype('int64')
ohlcv['time'] = ohlcv['time']/1000
ohlcv['date'] = pd.to_datetime(ohlcv['time'], unit='s')
ohlcv = ohlcv.set_index(pd.DatetimeIndex(ohlcv['date']))
return ohlcv
def check_pair(first_market, second_market, timeframe):
first = get_ohlc_ccxt(first_market, timeframe)
second = get_ohlc_ccxt(second_market, timeframe)
if len(first) != len(second):
length = min(len(first), len(second))
first = first.iloc[-length:]
second = second.iloc[-length:]
x = first['close'].to_numpy()
y = second['close'].to_numpy()
first['logret'] = first.ta.log_return()
second['logret'] = second.ta.log_return()
xr = first['logret'].fillna(0)
yr = second['logret'].fillna(0)
coint = ts.coint(xr.to_numpy(), yr.to_numpy())
p_value = coint[1]
print('Cointegration:', first_market, second_market, p_value)
spread = xr-yr
return first, second
data = check_pair('BTC-PERP', 'ETH-PERP', '5m')
In this case the p-value is 8.94059749424772e-29
. If i try other markets (like BTC-LINK, or BTC-LTC and so on) the p-value will always be incredibly low. Can anyone help me find what i'm doing wrong?