Intra day 5 min bar

I am testing a strategy on alpaca paper account which requires making buy/sell decisions on 5 min bar data. I am currently on free tier with plans to upgrade later. I need real time 5 min bar data as soon as they are formed. For E.G. at 9:35 AM EST the first 5 min bar for the day will be available for a symbol. I am using a websocket and subscribing to the symbol using IEX
DATA_URL = “wss://stream.data.alpaca.markets/v2/iex”

1.) I am getting one min bar data but its delayed by a minute. Is that normal?
2.) I am aggregating the 1 min bar data and resampling it to generate 5 min bar data. Is there an option to directly get the 5 min bar data rather than me doing the aggregation and resampling?
3.) If I subscribe to Algo trader plus I can query sip feed rather than iex. Will I get real time 5 min bar at the precise intervals of 9:35, 9:40 and so on without me doing resampling?

Sample code below

async def stream_data():
    """Stream live bar data for all symbols."""
    import websocket

    def on_open(ws):
        # Authenticate
        ws.send(json.dumps({"action": "auth", "key": API_KEY, "secret": API_SECRET}))
        # Subscribe to bar updates
        ws.send(json.dumps({"action": "subscribe", "bars": SYMBOLS}))

    def on_message(ws, message):
        logging.info(f"Received message: {message}")

    def on_close(ws, close_status_code, close_msg):
        logging.info(f"WebSocket closed: {close_status_code} - {close_msg}")
        # Trigger an explicit exit
        ws.keep_running = False  # Exit `run_forever()`

    def on_error(ws, error):
        logging.error(f"WebSocket error: {error}")

    ws = websocket.WebSocketApp(DATA_URL, on_open=on_open, on_message=on_message, on_close=on_close, on_error=on_error)

    while True:  # Auto-reconnect logic
        ws.run_forever()
        logging.info("WebSocket disconnected. Retrying in 10 seconds...")
        await asyncio.sleep(10)  # Wait before reconnecting

Thanks.

Update: The data may not be delayed after all. I am getting the below one min bar data at exactly 2024-12-17T15:20:00Z. The timestamp in the bar indicates data from 15:19:00 to 15:19:59 (inclusive) so it is current

[{‘T’: ‘b’, ‘S’: ‘ABC’, ‘o’: 20.64, ‘h’: 20.65, ‘l’: 20.64, ‘c’: 20.65, ‘v’: 1570, ‘t’: ‘2024-12-17T15:19:00Z’, ‘n’: 2, ‘vw’: 20.643631}]

Hi! :wave:

1.) I am getting one min bar data but its delayed by a minute. Is that normal?

As you realised in your update, the data is not delayed. The bar’s timestamp simply indicates the start of the bar.

2.) I am aggregating the 1 min bar data and resampling it to generate 5 min bar data. Is there an option to directly get the 5 min bar data rather than me doing the aggregation and resampling?

No, there is no such option on the stream.

3.) If I subscribe to Algo trader plus I can query sip feed rather than iex. Will I get real time 5 min bar at the precise intervals of 9:35, 9:40 and so on without me doing resampling?

No, there is no such option on the SIP stream either. The main benefit you get from using SIP instead of IEX is that the quality of the minute bars will drastically improve, because they will contain all the trades from all US Exchanges (instead of IEX only).

Thanks for the response. For IEX, there are lots of missing one min bars which messes up my 5 min bar aggregation. I believe that won’t happen with SIP right?

There will be less missing on minute bars with SIP. But there still can be some for less frequently traded stocks. It shouldn’t mess up your 5-min bar aggregation though.