how to get 1 minute price data of a specific time and date from yfinance?

Viewed 2228

I wanna know how can i get 1 minute gold price data of a specific time and date interval (such as an 1 houre interval in 18th october: 2021-10-18 09:30:00 to 2021-10-18 10:30:00) from yfinance or any other source in python?

my code is:

gold = yf.download(tickers="GC=F", period="5d", interval="1m")

it seems it`s just possible to set period while i wanna set specific date and time intervals. thanks

2 Answers

Your call to yfinance returns a Pandas DataFrame with datetime as the index. We can use this to filter the dataframe to only entries between our start and end times.

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]

Outputs

                                  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]

Edit 2021-10-25

To clear my answer. Question was:

i wanna set specific date and time intervals. thanks

All you need is in the code documentation. So start and end could be date or _datetime

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

Example code:

Note: something wrong with timezones, i've tryed to pass correcth timezone with start and end but lib didn't handle it correctly and I was finish with convert it manually)

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))

Result and you can pass whatever datetime ranges you want:

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: with start and end you do not have limitation to the last 7 days, but still have limit to the last 30 days:

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.

Original

this lib has a lack of documentation. But this is python and as result it some kind of self-documented.

Read definition of download function here https://github.com/ranaroussi/yfinance/blob/6654a41a8d5c0c9e869a9b9acb3e143786c765c7/yfinance/multi.py#L32

PS this function have start= and end= params that I hope help you

Related