繁体   English   中英

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

[英]Get historical monthly stock close price in custom format using 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,或者如果不可能只获得收盘价,那么将 output 转换为我需要的格式的最佳方法是什么。 玩过 dataframe.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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM