我正在尝试更多地了解Python。 我正在尝试从雅虎财经获取一些数据。 我有这个代码:
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")
它按预期工作。 然后我更改符号(第四行更改为加密货币对,即
symbols = ['BTCUSD', 'ETHUSD']
代码在这一行出错:
coint_test_result = coint_johansen(data, specified_number, 1)
错误是:
zero-size array to reduction operation maximum which has no identity
为什么将加密货币对作为符号会出错?
我的Python版本是3.12.3。
问题似乎在于雅虎财经期望加密货币符号采用不同的格式。尝试使用“BTC-USD”和“ETH-USD”代替:
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")
这应该可以解决问题。如果问题仍然存在,请确保日期范围和符号正确。