How to retrieve recent/current stock data?

This is in python

I am trying to calculate the stocks RSI

I have tried:

current_datetime = datetime.now()
print(current_datetime)

#Get the last 7 days of historical data
start_datetime = current_datetime - timedelta(days=7)

data = api.get_bars(“NVDA”, tradeapi.rest.TimeFrame.Minute, start_datetime, current_datetime, limit=100000, adjustment=‘raw’).df

yet I get the error:

400 Client Error: Bad Request for url: https://data.alpaca.markets/v2/stocks/NVDA/bars?timeframe=1Min&adjustment=raw&start=2023-12-18+19%3A01%3A36.991984&end=2023-12-25+19%3A01%3A36.991984&limit=10000

@Bunkiyiester The main issue is that the APIs expect a timezone. The current_datetime is timezone naive. In general, always ensure datetime objects are timezone aware and ideally (for consistancy) ensure the timezone is set to market time. In this instance however UTC is ok.

So change your code to this and it will work


from datetime import datetime, timedelta, timezone

current_datetime = datetime.now(tz=timezone.utc)
start_datetime = current_datetime - timedelta(days=7)

data = api.get_bars(“NVDA”, 
                    tradeapi.rest.TimeFrame.Minute, 
                    start_datetime, 
                    current_datetime, 
                    limit=100000, 
                    adjustment=‘raw’).df

ah I see that makes sense, thanks, but is there any way to specify the interval for data?