I'm trying to get market data concerning the closing time value of the last 20 hours using the alpaca API. My code works when the stock exchange is open and only provides the value of that particular day (so not the day before). I want the code to be running continuesly, so just specifying the date will not work.
from alpaca_trade_api.rest import REST, TimeFrame, TimeFrameUnit
import alpaca_trade_api as tradeapi
from alpaca_trade_api.rest import TimeFrame
#setup API
SEC_KEY = '<secret paper key>'
PUB_KEY = '<Public paper key>'
BASE_URL = 'https://paper-api.alpaca.markets'
api = tradeapi.REST(key_id= PUB_KEY, secret_key=SEC_KEY, base_url=BASE_URL)
# set time frame
data_df= api.get_bars("AAPL", TimeFrame(20, TimeFrameUnit.Hour), adjustment='raw').df
# Convert to market time to make it easier to read
data_df.index = data_df.index.tz_convert('America/New_York')
print(data_df)