yfinance - 将我的结束日期设置为 2021-1-1,但数据的结束日期变为今天日期
Posted
技术标签:
【中文标题】yfinance - 将我的结束日期设置为 2021-1-1,但数据的结束日期变为今天日期【英文标题】:yfinance - Setting my end date to 2021-1-1 but the end date for the data becomes today date 【发布时间】:2021-09-23 04:16:39 【问题描述】:对于 yfinance python 模块,我将结束日期设置为 2021-1-1,但结果却是今天。如何解决?
yfinance.download(tickers="AAPL",end="2021-1-1",period="5y",interval="1d")
结果:
【问题讨论】:
查看节目源,如果没有给出开始日期,则给出默认值1970-01-01,如果没有给出开始日期,则按获取周期取,我猜测。来源可以找到here。 【参考方案1】:我发现当您使用period="5y"
时,无论提到的开始/结束日期都不起作用
去掉 period 参数,你可以尝试 datetime format 设置 start 和 end 日期为 5 年如下
import yfinance
import datetime as dt
enddate = dt.datetime.strptime("2021-01-01", "%Y-%m-%d").date()
startdate = enddate - dt.timedelta(days=365*5) # for 5 years
data = yfinance.download(tickers="AAPL", start=startdate, end=enddate, interval="1d")
print(data)
输出:
[*********************100%***********************] 1 of 1 completed
Open High Low Close Adj Close Volume
Date
2016-01-04 25.652500 26.342501 25.500000 26.337500 24.323219 270597600
2016-01-05 26.437500 26.462500 25.602501 25.677500 23.713696 223164000
2016-01-06 25.139999 25.592501 24.967501 25.174999 23.249626 273829600
2016-01-07 24.670000 25.032499 24.107500 24.112499 22.268383 324377600
2016-01-08 24.637501 24.777500 24.190001 24.240000 22.386133 283192000
... ... ... ... ... ... ...
2020-12-24 131.320007 133.460007 131.100006 131.970001 131.549637 54930100
2020-12-28 133.990005 137.339996 133.509995 136.690002 136.254608 124486200
2020-12-29 138.050003 138.789993 134.339996 134.869995 134.440399 121047300
2020-12-30 135.580002 135.990005 133.399994 133.720001 133.294067 96452100
2020-12-31 134.080002 134.740005 131.720001 132.690002 132.267349 99116600
[1259 rows x 6 columns]
或
使用 str date 提及开始和结束日期,如下所示
import yfinance
data = yfinance.download(tickers="AAPL", start="2016-01-03", end="2021-01-01", interval="1d")
print(data)
输出:
[*********************100%***********************] 1 of 1 completed
Open High Low Close Adj Close Volume
Date
2016-01-04 25.652500 26.342501 25.500000 26.337500 24.323219 270597600
2016-01-05 26.437500 26.462500 25.602501 25.677500 23.713696 223164000
2016-01-06 25.139999 25.592501 24.967501 25.174999 23.249626 273829600
2016-01-07 24.670000 25.032499 24.107500 24.112499 22.268383 324377600
2016-01-08 24.637501 24.777500 24.190001 24.240000 22.386133 283192000
... ... ... ... ... ... ...
2020-12-24 131.320007 133.460007 131.100006 131.970001 131.549637 54930100
2020-12-28 133.990005 137.339996 133.509995 136.690002 136.254608 124486200
2020-12-29 138.050003 138.789993 134.339996 134.869995 134.440399 121047300
2020-12-30 135.580002 135.990005 133.399994 133.720001 133.294067 96452100
2020-12-31 134.080002 134.740005 131.720001 132.690002 132.267349 99116600
[1259 rows x 6 columns]
【讨论】:
以上是关于yfinance - 将我的结束日期设置为 2021-1-1,但数据的结束日期变为今天日期的主要内容,如果未能解决你的问题,请参考以下文章
如何从 yfinance 获取特定时间和日期的 1 分钟价格数据?
将我的 javascript 日期时间从 PST 转换为 UTC