For the index reversal strategy, we need the data one minute and two minutes before the market close. For illustration purpose, the SPY minute level data is available in two files:
Price data for one minute before market close is stored in the file named SPY_data_15_59.csv
Price data for two minutes before market close is stored in the file named SPY_data_15_58.csv
how can i get data like this
# Read the price data for one minute before the close
price_one_minute_before_close = pd.read_csv(
'../data_modules/SPY_data_15_59.csv', index_col=0, parse_dates=True)
# Read the price data for two minutes before the close
price_two_minute_before_close = pd.read_csv(
'../data_modules/SPY_data_15_58.csv', index_col=0, parse_dates=True)
price_one_minute_before_close.head()
Open High Low Close Volume
DateTime
2004-01-02 15:59:00 110.97 111.04 110.92 111.00 156600.0
2004-01-05 15:59:00 112.38 112.41 112.35 112.37 130500.0
2004-01-06 15:59:00 112.60 112.63 112.58 112.63 302300.0
2004-01-07 15:59:00 112.83 112.92 112.83 112.89 174300.0
2004-01-08 15:59:00 113.34 113.37 113.29 113.32 253900.0
Hi there,
To store the OHLC data for the stock one minute and two minutes before the market close on multiple days, you can use the following code:
import yfinance as yf
import pandas as pd
data = yf.download('^GSPC', start='2023-05-15', end='2023-05-20', interval='1m', auto_adjust=True)
# Create empty DataFrames to store the last & second last minute data
last_minute_data_df = pd.DataFrame(columns=data.columns)
second_last_minute_data_df = pd.DataFrame(columns=data.columns)
for date in pd.unique(data.index.date):
# Filter the data for the current date
date_data = data[data.index.date == date]
# Get the last & second last minute's data for the closing time
last_minute_data = date_data.tail(1)
second_last_minute_data = date_data.tail(2).head(1)
# Concatenate the last & second last minute data to the corresponding DataFrames
last_minute_data_df = pd.concat([last_minute_data_df, last_minute_data])
second_last_minute_data_df = pd.concat([second_last_minute_data_df, second_last_minute_data])
# Save the DataFrames to CSV files
last_minute_data_df.to_csv('spy_data_15_59.csv')
print("Data saved to spy_data_15_59.csv successfully.")
second_last_minute_data_df.to_csv('spy_data_15_58.csv')
print("Data saved to spy_data_15_58.csv successfully.")
Hope this helps. Please feel free to ask if you need any further clarifications.
Thanks.