I'm running a python script locally in the console to connect to polygon.io's stocks websocket to get aggregate second data.
when I run this code, I don't get any errors, but I also see no data streaming in the console. I am using AM.TSLA as a test. I verified with polygon support that my API key is capable of getting websocket data.
Here is my code:
import pandas as pd
import sys
import json
sys.path.insert(0, '/wamp64/www/market-data/py/client-python')
from polygon import WebSocketClient
from polygon.websocket.models import WebSocketMessage
from typing import List
def on_open(ws):
print("WebSocket connection opened.")
def handle_msg(msg: List[WebSocketMessage]):
for m in msg:
# Print the raw message data received
print("Raw message data:", m)
# If m.json is the correct attribute containing the message data
print("Parsed message data:", m.json)
def on_close(ws):
print("Connection closed")
def on_error(ws, error):
print("Error:", error)
def main():
print("Starting WebSocket client...")
# Polygon API key
api_key = '####'
# Create a WebSocket client
ws_client = WebSocketClient(api_key=api_key, subscriptions=["AM.TSLA"])
print("Getting AM.TSLA data...")
# Assign the callbacks
ws_client.on_open = on_open
ws_client.on_message = handle_msg
ws_client.on_close = on_close
ws_client.on_error = on_error
ws_client.run(handle_msg)
# Keep the script running
print("WebSocket client is running... Press Enter to stop listening.")
try:
# Wait for user input to close the connection
input()
finally:
# Ensure the WebSocket connection is closed properly
ws_client.close()
print("WebSocket connection closed.")
if __name__ == "__main__":
main()
The issue was related to the subscription level I had with polygon.io. I need to subscribe to their delayed data endpoint. Here is my updated code: