如何在Python中使用yfinance获取汇总的4小时条历史股票数据?

vuktfyat  于 2022-12-15  发布在  Python
关注(0)|答案(1)|浏览(150)

yfinance似乎没有提供免费的预聚合条供下载(据我所知),而是需要我自己聚合数据,我试过下面的代码,但它在聚合数据框中产生了很多NaN值,这似乎不对!......我想知道是否有更好的方法来做到这一点。
代码:

import yfinance as yf

df = yf.download("SPY", period="2y", interval='1h')
ohlc_dict = {'Open': 'first', 'High': 'max', 'Low': 'min', 'Close': 'last', 'Adj Close': 'last', 'Volume': 'sum'}
df = df.resample('4H', label='left').agg(ohlc_dict)
print(df)

结果:

[*********************100%***********************]  1 of 1 completed
                                Open        High  ...   Adj Close    Volume
2020-11-17 12:00:00-05:00  361.36499  361.920013  ...  360.640015  22304320
2020-11-17 16:00:00-05:00        NaN         NaN  ...         NaN         0
2020-11-17 20:00:00-05:00        NaN         NaN  ...         NaN         0
2020-11-18 00:00:00-05:00        NaN         NaN  ...         NaN         0
2020-11-18 04:00:00-05:00        NaN         NaN  ...         NaN         0

[5 rows x 6 columns]

有没有一种简单的方法可以从Yahoo Finance服务器获取已经预先聚合的数据?

5hcedyr0

5hcedyr01#

这是我如何汇总数据的

import yfinance as yf
import pandas as pd

# Download data from 'Yahoo! Finance' using yfinance
df = yf.download('AAPL', period='2y', interval='1h')
# group in 4Hours chunks.
df_agg = df.groupby(pd.Grouper(freq='4H')).agg({"Open": "first", "High": "max", "Low": "min", "Close": "last",
                                                  "Adj Close": "last"})
# Remove the NaN rows
df_final = df_agg.dropna(how='all')
# Label the dataframe columns 
df_final.columns = ["open", "high", "low", "close", "Adj Close"]
# Print the dataframe
print(df_final.to_string())

结果如下所示:

open        high         low       close   Adj Close
2020-12-14 08:00:00-05:00  122.599998  123.349998  122.489998  122.914497  122.914497
2020-12-14 12:00:00-05:00  122.910004  122.964203  121.540001  121.699997  121.699997
2020-12-15 08:00:00-05:00  124.339996  126.919998  124.129997  126.370003  126.370003
2020-12-15 12:00:00-05:00  126.371696  127.900002  126.239998  127.892998  127.892998
2020-12-16 08:00:00-05:00  127.410004  128.020004  126.599998  127.555000  127.555000
2020-12-16 12:00:00-05:00  127.550003  128.369995  127.260002  127.750000  127.750000
2020-12-17 08:00:00-05:00  128.899994  129.580002  128.044998  128.413605  128.413605
2020-12-17 12:00:00-05:00  128.410004  128.830002  128.119995  128.649994  128.649994
2020-12-18 08:00:00-05:00  128.960007  129.100006  127.080002  127.649300  127.649300
2020-12-18 12:00:00-05:00  127.645203  128.000000  126.120003  126.650002  126.650002
2020-12-21 08:00:00-05:00  125.430000  126.618401  123.448997  125.695900  125.695900
2020-12-21 12:00:00-05:00  125.695000  128.309998  125.578499  128.240005  128.240005
2020-12-22 08:00:00-05:00  131.610001  134.404999  129.649994  131.580002  131.580002
2020-12-22 12:00:00-05:00  131.586105  133.149994  131.221497  131.845001  131.845001
2020-12-23 08:00:00-05:00  131.918503  132.259796  130.850006  132.068298  132.068298
2020-12-23 12:00:00-05:00  132.050003  132.259995  130.919998  130.960007  130.960007
2020-12-24 08:00:00-05:00  131.320007  133.460007  131.100006  131.880005  131.880005
2020-12-24 12:00:00-05:00  131.880005  132.050003  131.610001  131.990005  131.990005

相关问题