使用yfinance以自定义格式获取历史月度股票收盘价



我需要以以下格式获取股票的历史价格:

[
{'AMZN': [
{'Sep 2022': 113},
{'Oct 2022': 102},
{'Nov 2022': 92}
]},
{'AAPL': [
{'Sep 2022': 137},
{'Oct 2022': 153},
{'Nov 2022': 147}
]},
{'MSFT': [
{'Sep 2022': 232},
{'Oct 2022': 231},
{'Nov 2022': 241}
]}
]

但是不知道如何将适当的配置传递给yfinance,或者如果不可能只获得收盘价,那么将输出转换为我需要的格式的最佳方法是什么?使用了datafframe .to_dict("records"),但是只得到价格而没有日期。

我的代码

from datetime import date
import yfinance as yf
tickers = ['AMZN', 'AAPL', 'MSFT']
data = yf.download(
tickers = tickers,
start="2019-01-01",
end=date.today().replace(day=2),
interval = "1mo",
group_by = 'ticker'
)

让它工作:

[{ticker: [{str(x.strftime('%b %Y')): int(tickerdata[ticker]['Close'][x])} for x in tickerdata[ticker]['Close'].index]} for ticker in tickers]

最新更新