如何从yfinance获取特定时间和日期的1分钟价格数据?



我想知道我怎么才能得到1分钟特定时间和日期间隔的金价数据(例如10月18日的1小时间隔:20121-10-18 09:30:00到20121-10-18 10:30:00)从yfinance或任何其他python来源?

我的代码是:
gold = yf.download(tickers="GC=F", period="5d", interval="1m")

似乎可以设置period而我想设置特定的日期和时间间隔.由于

yfinance的调用返回一个以datetime为索引的PandasDataFrame。我们可以使用它来过滤数据帧,只保留startend之间的条目。

import yfinance as yf
from datetime import datetime
gold = yf.download(tickers="GC=F", period="5d", interval="1m")
start = datetime(2021, 10, 18, 9, 30, 0)
end = datetime(2021, 10, 18, 10, 30, 0)
filtered = gold[start: end]

输出
Open         High  ...    Adj Close  Volume
Datetime                                             ...                     
2021-10-18 09:30:00-04:00  1770.099976  1770.099976  ...  1767.599976    1035
2021-10-18 09:31:00-04:00  1767.900024  1769.099976  ...  1768.500000     467
2021-10-18 09:32:00-04:00  1768.599976  1769.300049  ...  1769.199951     428
2021-10-18 09:33:00-04:00  1769.300049  1770.199951  ...  1769.099976     750
2021-10-18 09:34:00-04:00  1769.199951  1769.300049  ...  1767.800049     549
...                                ...          ...  ...          ...     ...
2021-10-18 10:26:00-04:00  1770.300049  1770.500000  ...  1769.900024     147
2021-10-18 10:27:00-04:00  1769.800049  1769.800049  ...  1769.400024     349
2021-10-18 10:28:00-04:00  1769.400024  1770.400024  ...  1770.199951     258
2021-10-18 10:29:00-04:00  1770.300049  1771.000000  ...  1770.099976     382
2021-10-18 10:30:00-04:00  1770.300049  1771.000000  ...  1770.900024     180
[61 rows x 6 columns]

编辑20121-10-25

澄清我的答案。问题是:

我想设置特定的日期和时间间隔。由于

您所需要的只是代码文档。所以startend可以是date或者_datetime

start: str
Download start date string (YYYY-MM-DD) or _datetime.
Default is 1900-01-01

示例代码:

注意:时区有问题,我试图通过正确的时区开始和结束,但lib没有正确处理它,我完成了手动转换)

import pandas as pd
import yfinance as yf
import pendulum
pd.options.display.max_rows=10  # To decrease printouts
start = pendulum.parse('2021-10-18 09:30').add(hours=7)  # My tz is UTC+03:00, original TZ UTC-04:00. So adds to my local time 7 hours
end = pendulum.parse('2021-10-18 10:30').add(hours=7)  # Same
print(start)
print(yf.download(tickers="GC=F", interval="1m", start=start, end=end))

结果,你可以传递任何你想要的日期时间范围:

2021-10-18T16:30:00+00:00
[*********************100%***********************]  1 of 1 completed
Open         High          Low        Close  
Datetime                                                                        
2021-10-18 09:30:00-04:00  1770.099976  1770.099976  1767.400024  1767.800049   
2021-10-18 09:31:00-04:00  1767.900024  1769.099976  1767.800049  1768.500000   
2021-10-18 09:32:00-04:00  1768.599976  1769.300049  1768.199951  1769.199951   
2021-10-18 09:33:00-04:00  1769.300049  1770.199951  1768.900024  1769.099976   
2021-10-18 09:34:00-04:00  1769.199951  1769.300049  1767.599976  1767.800049   
...                                ...          ...          ...          ...   
2021-10-18 10:25:00-04:00  1769.900024  1770.400024  1769.800049  1770.300049   
2021-10-18 10:26:00-04:00  1770.300049  1770.500000  1769.900024  1769.900024   
2021-10-18 10:27:00-04:00  1769.800049  1769.800049  1769.099976  1769.400024   
2021-10-18 10:28:00-04:00  1769.400024  1770.400024  1769.400024  1770.199951   
2021-10-18 10:29:00-04:00  1770.300049  1771.000000  1769.900024  1770.099976   
Adj Close  Volume  
Datetime                                        
2021-10-18 09:30:00-04:00  1767.800049       0  
2021-10-18 09:31:00-04:00  1768.500000     459  
2021-10-18 09:32:00-04:00  1769.199951     428  
2021-10-18 09:33:00-04:00  1769.099976     750  
2021-10-18 09:34:00-04:00  1767.800049     549  
...                                ...     ...  
2021-10-18 10:25:00-04:00  1770.300049     134  
2021-10-18 10:26:00-04:00  1769.900024     147  
2021-10-18 10:27:00-04:00  1769.400024     349  
2021-10-18 10:28:00-04:00  1770.199951     258  
2021-10-18 10:29:00-04:00  1770.099976     382  
[60 rows x 6 columns]

PS:使用startend,您不受过去7天的限制,但仍有过去30天的限制:

1 Failed download:
- GC=F: 1m data not available for startTime=1631980800 and endTime=1631998800. The requested range must be within the last 30 days.
<标题>原始h1> 个库缺少文档。但这是python,因此它是某种自我文档。

在这里阅读下载函数的定义https://github.com/ranaroussi/yfinance/blob/6654a41a8d5c0c9e869a9b9acb3e143786c765c7/yfinance/multi.py L32

PS这个函数有start=end=参数,希望对你有所帮助

相关内容

  • 没有找到相关文章

最新更新