I am trying to learn more about Python. I am trying to pull some data from Yahoo Finance. I have this code:
import yfinance as yf
from statsmodels.tsa.vector_ar.vecm import coint_johansen
symbols = ['AMZN', 'AAPL']
start = '2020-01-01'
end = '2024-01-16'
data = yf.download(symbols, start=start, end=end)['Adj Close']
specified_number = 0
coint_test_result = coint_johansen(data, specified_number, 1)
print("End")
It works as expected. I then change the symbols (line four to crypto currency pairs i.e.
symbols = ['BTCUSD', 'ETHUSD']
The code then errors on this line:
coint_test_result = coint_johansen(data, specified_number, 1)
The error is: zero-size array to reduction operation maximum which has no identity
Why does it error with cryptocurrency pairs as symbols?
I have version 3.12.3 of Python.
>Solution :
Your Code works to get cryptocurrency data correctly:
However, when switching to cryptocurrency symbols, make sure you use the correct format for the tickers:
import yfinance as yf
from statsmodels.tsa.vector_ar.vecm import coint_johansen
symbols = ['BTC-USD', 'ETH-USD']
start = '2020-01-01'
end = '2024-01-16'
data = yf.download(symbols, start=start, end=end)['Adj Close']
if data.empty:
print("No data fetched.")
else:
specified_number = 0
try:
coint_test_result = coint_johansen(data, specified_number, 1)
print("Cointegration Test Result:", coint_test_result)
except Exception as e:
print("Error performing cointegration test:", e)
print("End")
This should fix the issue. If the problem persists, make sure the date range and symbols are correct.
For more details on yfinance, check out the official documentation.