no data from polygon.io websocket

110 Views Asked by At

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()
1

There are 1 best solutions below

0
Alexnl On

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:

import pandas as pd
import sys
sys.path.insert(0, '/wamp64/www/market-data/py/client-python')
import logging
from polygon import WebSocketClient
from polygon.websocket.models import WebSocketMessage, Market
from typing import List, Union

# logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# logging.info("Starting WebSocket client...")
# logging.error("This is an error message")

# Settings
apikey = '#####'

c = WebSocketClient(api_key=apikey, feed='delayed.polygon.io', market='stocks', subscriptions=["AM.TSLA"])

def handle_msg(msgs: List[WebSocketMessage]):
    for m in msgs: print(m)
def main(): c.run(handle_msg)

main()