您的代码可正确获取加密货币数据:
然而,当切换到加密货币符号时,请确保使用正确的股票代码格式:
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")
这应该可以解决问题。如果问题仍然存在,请确保日期范围和符号正确无误。
有关yfinance的更多详细信息,请查看
official documentation
.