Premarket data in python

I heard that u can get pre-market data from Alpaca markets.api can anyone pls show me how to get pre-market data in python pls

@python_stuff There isn’t anything special that needs to be done to fetch pre-market data. If the request is for times outside of regular market hours, and there is available data, then it will be returned.

Below would fetch all data (pre-market, market, and post-market) bars for SPY and IBM.

# import needed packages
!pip install -q alpaca-py
import pandas as pd
from alpaca.data import StockHistoricalDataClient, TimeFrame, TimeFrameUnit
from alpaca.data.requests import StockBarsRequest

# instantiate a 'client' to store data needed for API calls
ALPACA_API_KEY = 'xxxxx'
ALPACA_API_SECRET_KEY = 'xxxxx'

data_client = StockHistoricalDataClient(ALPACA_API_KEY, ALPACA_API_SECRET_KEY)

# set the start and end times and symbols
start = pd.to_datetime("2024-03-25").tz_localize('America/New_York')
end = pd.to_datetime("2024-03-26").tz_localize('America/New_York')
symbols = ["SPY", "IBM"]

# fetch 1 hour bars and convert to dataframe with the market timezone
bars = data_client.get_stock_bars(StockBarsRequest(
            symbol_or_symbols = symbols,
            timeframe=TimeFrame(amount=1, unit=TimeFrameUnit.Hour),
            start = start,
            end = end,
            feed = 'sip',
          )).df.tz_convert('America/New_York', level= 'timestamp')

Here is the result. Note that there may not be bars if there are no ‘valid’ trades. This is especially true for pre and post market where trading can be very light. Notice the first bar for IBM isn’t until 6:00 while the first bar for SPY is right at the open of pre-market hours at 4:00.

1 Like

when I change the date to today i get an error saying my supriction does not permit that

@python_stuff If one only has the Free Market Data subscription then the most recent 15 minutes are not available. A query for that time will result in an error. To avoid this, one can simply set the end datetime to not include the most recent 15 minutes. Perhaps like this.

# set the start and end times and symbols
current_time = pd.Timestamp('now', tz="America/New_York")

start = pd.to_datetime("2024-03-25").tz_localize('America/New_York')
end = current_time - pd.Timedelta('15Min')
symbols = ["SPY", "IBM"]

# fetch 1 hour bars and convert to dataframe with the market timezone
bars = data_client.get_stock_bars(StockBarsRequest(
            symbol_or_symbols = symbols,
            timeframe=TimeFrame(amount=1, unit=TimeFrameUnit.Hour),
            start = start,
            end = end,
            feed = 'sip',
          )).df.tz_convert('America/New_York', level= 'timestamp')
2 Likes

@Dan_Whitnable_Alpaca is there a way to exclude pre and post market date.

For example if I am getting 5 min bars for the last 3 days.

Thanks.

@rajpeter2020 There isn’t a direct way to exclude pre and post market data. One could of course loop through your range of days and only fetch the bars between the open and close each day. However, If using python, I typically use the pandas between_time method.

client = StockHistoricalDataClient(api_key=ALPACA_API_KEY_ID, secret_key=ALPACA_API_SECRET_KEY, raw_data=False)
request_params = StockBarsRequest(
                                  timeframe=TimeFrame(amount=5, unit=TimeFrameUnit.Minute),
                                  symbol_or_symbols = ['SPY', 'AAPL'],
                                  start = pd.to_datetime('2024-05-20 00:00').tz_localize('America/New_York'),
                                  end = pd.to_datetime('2024-05-30 20:00').tz_localize('America/New_York'),
                                  feed='sip')
bars = client.get_stock_bars(request_params=request_params).df.tz_convert('America/New_York', level='timestamp')

market_bars = bars.reset_index('symbol').between_time('9:30', '16:00')

If you want the index back as a multiindex as originally fetched, you can do this


market_bars = bars.reset_index('symbol').between_time('9:30', '16:00').set_index('symbol', append=True).swaplevel()

Note this isn’t 100% correct, but may be ok for many application. There are a few days out of the year which have shortened trading days.

For finding out specific market days and hours, I like a python package called exchange_calendars. This has a number of convenient methods including is_trading_minute. Given a minute, this will return whether it’s when markets are open. One can use this to filter the minute bars. Something like this


# install and import exchange_calendars
!pip install -q exchange_calendars
import exchange_calendars

# instantiate a calendar object
us_markets = exchange_calendars.get_calendar("XNYS")

# create columns for bar timestamp and another if that timestamp is during market hours
bars['bar_timestamp'] = bars.index.get_level_values(level='timestamp')
bars['is_market_hour_bar'] = bars.bar_timestamp.apply(us_markets.is_trading_minute)

# filter for market hour bars using the query method
market_hour_bars = bars.query('is_market_hour_bar')

Hope that gives you some ideas.

1 Like

Thanks a lot @Dan_Whitnable_Alpaca for detailed explanation.

Another related question does the 1 day level OHLC data also include pre and post market date or is it only regular session data.

Thanks
Raj

@rajpeter2020does the 1 day level OHLC data also include pre and post market date”. The answer is yes and no. The OHLC price data is only market hour data. However the volume includes all trades pre and post market.