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.