Yahoo Finance - download specific time frames of hisotrical data

Viewed 560

I am using the below code to download stock data from yahoo finance and wanted to ask how I can modify it so that it for instance only downloads data from 1pm to 3pm?

I have tried a few things but it did not work out so far. Would appreciate any tips!

TickerSymbol = '^GDAXI'
data = yf.download(tickers=TickerSymbol, period='1d', interval='1m')
data.reset_index(inplace=True)
DataDateStrt = data.loc[0, 'Datetime'].strftime('%d.%m.%y')
DataDateStop = data.loc[len(data['Datetime'])-1, 'Datetime'].strftime('%d.%m.%y')
DataTimeSpan = data.loc[len(data['Datetime'])-1, 'Datetime'] - data.loc[0, 'Datetime']
1 Answers

You can set a parameters for time interval in yahoo finance.

import yfinance as yf

data = yf.download("AAPL", start="2022-05-01", end="2022-05-03",  interval = "1m")

Data will be like

                                 Open        High         Low       Close   Adj Close   Volume
Datetime                                                                                      
2022-05-02 09:30:00-04:00  157.339905  157.429993  157.285004  157.429993  157.429993  4015130
2022-05-02 09:31:00-04:00  157.220093  157.559998  157.010101  157.430405  157.430405   356736
2022-05-02 09:32:00-04:00  157.419998  158.024994  157.130096  157.779907  157.779907   472590
2022-05-02 09:33:00-04:00  157.779999  158.039993  157.309998  157.360001  157.360001   455743

If you do this filter you can access the time interval with minute

data[(data.index > "2022-05-02 09:30:00-04:00") & (data.index < "2022-05-02 09:35:00-04:00")]

This filter output will be

                                 Open        High         Low       Close   Adj Close  Volume
Datetime                                                                                     
2022-05-02 09:31:00-04:00  157.220093  157.559998  157.010101  157.430405  157.430405  356736
2022-05-02 09:32:00-04:00  157.419998  158.024994  157.130096  157.779907  157.779907  472590
2022-05-02 09:33:00-04:00  157.779999  158.039993  157.309998  157.360001  157.360001  455743
2022-05-02 09:34:00-04:00  157.350098  157.610001  156.630005  156.759995  156.759995  456566

[4 rows x 6 columns]
Related