Getting bar data in a trading day

Hi

I want to get stock bar data in a trading day, like to see what happened in the morning until the noon. I am in CST time zone. The following script does not turn any results.
Could you please help?

I am using this script:

current_time = datetime.now()
today = str(date.today())
time_15_minutes_ago = current_time - timedelta(minutes=15)
time_in_ny = time_15_minutes_ago + timedelta(hours=1)
formatted_time = time_in_ny.strftime(‘%Y-%m-%d %H:%M:%S’)
start_time_string = today + ’ 09:30:00’
start_time = pd.to_datetime(start_time_string).tz_localize(‘America/New_York’)
end_time = pd.to_datetime(formatted_time).tz_localize(‘America/New_York’)

request_params = StockBarsRequest(
symbol_or_symbols=tradable_symbols,
timeframe=TimeFrame(10, TimeFrameUnit.Minute),
start=start_time,
end=end_time
)
bars_df = trading_client.get_stock_bars(request_params).df.tz_convert(‘America/New_York’, level=1)

@huseyin Your code works, but you are trying to fetch data for 2024-03-29. Markets are closed for the Good Friday Holiday and there is no market data.

I would however suggest not using string manipulation in working with times. Use pandas and it’s built in time functions. No need to convert back and forth from strings. I find the most useful pandas methods are to_datetime, normalize, tz_localize, and tz_convert. The normalize method is handy to get the datetime of midnight so one can then add Timedeltas as in the code below. Here is perhaps a more straightforward approach

import pandas as pd

current_time = pd.Timestamp(‘now’, tz=‘America/New_York’)
current_market_open = current_time.normalize() + pd.Timedelta(‘9:30:00’)

request_params = StockBarsRequest(
symbol_or_symbols = tradable_symbols,
timeframe = TimeFrame(10, TimeFrameUnit.Minute),
start = current_market_open,
end = current_time
)

bars_df = client.get_stock_bars(request_params).df.tz_convert(‘America/New_York’, level=‘timestamp’)

:slight_smile:

Thank you so much. I didn’t know market is closed today