I am writing script to fetch latest 29 bars at any point of time in the day…
i gave end datetime as 3 PM CST and starttime 72 mins before 3 PM.
Expected - 29 bars between given start and end time
Results - only 2 bars are showing up…
Script is running on a weekday during trading hours so no chance of no bar…
Would appreciate any inputs.
# Set end time to 3 PM today
end = datetime.now().replace(hour=15, minute=0, second=0, microsecond=0)
# Calculate start time to be 72 minutes before the end time
start = end - timedelta(minutes=(29 * 3) - 15) # 72 minutes before 3 PM
# Debugging output to verify start and end times
print(f"Start time: {start}")
print(f"End time: {end}")
request = StockBarsRequest(
symbol_or_symbols=["SPY"],
start=start,
end=end,
timeframe=TimeFrame(3, TimeFrame.Minute)
)
# Now let's get the data.
data = stock_data_client.get_stock_bars(request_params=request)
OUTPUT →
Start time: 2024-10-28 13:48:00
End time: 2024-10-28 15:00:00
data={‘SPY’: [{ ‘close’: 582.41,
‘high’: 582.49,
‘low’: 581.24,
‘open’: 581.75,
‘symbol’: ‘SPY’,
‘timestamp’: datetime.datetime(2024, 10, 28, 13, 59, tzinfo=TzInfo(UTC)),
‘trade_count’: 67853.0,
‘volume’: 2286577.0,
‘vwap’: 581.842912}, { ‘close’: 581.45,
‘high’: 582.5,
‘low’: 581.42,
‘open’: 582.4,
‘symbol’: ‘SPY’,
‘timestamp’: datetime.datetime(2024, 10, 28, 14, 30, tzinfo=TzInfo(UTC)),
‘trade_count’: 30849.0,
‘volume’: 1511857.0,
‘vwap’: 582.084401}]}
Recent bars