Daniel N
Daniel N

Reputation: 13

Can't stream incoming stock/ticker data with websocket connection?

I'm trying to create a trading algo on Alpaca markets using Polygon.io stock data. But am met with 2 problems:

  1. I'm having trouble getting a constant stream of ticker data with Polygon.io data through Alpaca markets. When I run my code, I'm only getting a message that the connection was a success and it was authenticated.

    [{"ev":"status","status":"connected","message":"Connected Successfully"}]
    [{"ev":"status","status":"auth_success","message":"authenticated"}]
    
  2. I get an "Maxmium number of connections exceeded." message. Then the connection closes. I assume this is because the first connection never actually closed so I'm overflowing the connection amount? How do I secure a complete connection cutoff with my code?

    {"ev":"status","status":"max_connections","message":"Maximum number of connections exceeded."}]

Here's my code below:

> import websocket, json 
> from config import *
>     
> def on_open(ws):
>     print("opened")
>     auth_data = {
>         "action": "auth",
>         "params": PAPER_API_KEY
>     }
> 
>     ws.send(json.dumps(auth_data))
> 
>     channel_data = {
>         "action": "subscribe",
>         "params": TICKERS
>     }
>     print(channel_data)
>     ws.send(json.dumps(channel_data))
> 
> 
> def on_message(ws, message):
>     print("received a message")
>     print(message)
> 
> def on_close(ws):
>     print("closed connection")
> 
> def on_error(ws, error):
>     print(error)
> 
> socket = "wss://socket.polygon.io/stocks"
> 
> ws = websocket.WebSocketApp(socket, on_open=on_open,
> on_message=on_message, on_close=on_close, on_error=on_error)
> ws.run_forever()

Upvotes: 1

Views: 2457

Answers (2)

Vyachez
Vyachez

Reputation: 1001

Did you try Polygon API directly?
Install it: pip install polygon-api-client.
Here is an example that worked for me:

import time

from polygon import WebSocketClient, STOCKS_CLUSTER


def my_custom_process_message(message):
    print("this is my custom message processing", message)


def my_custom_error_handler(ws, error):
    print("this is my custom error handler", error)


def my_custom_close_handler(ws):
    print("this is my custom close handler")


def main():
    key = 'your api key'
    my_client = WebSocketClient(STOCKS_CLUSTER, key, my_custom_process_message)
    my_client.run_async()

    my_client.subscribe("T.MSFT", "T.AAPL", "T.AMD", "T.NVDA")
    time.sleep(10)

    my_client.close_connection()


if __name__ == "__main__":
    main()

Upvotes: 1

roboboogie
roboboogie

Reputation: 11

you are allowed one stream, after you send out the subscriptions request you should receive a response.

this should happen after you run your " ws.send(json.dumps(channel_data))" line

[{'ev': 'status', 'message': 'subscribed to: AM.F', 'status': 'success'}]
[{'ev': 'status', 'message': 'subscribed to: AM.BAC', 'status': 'success'}]
[{'ev': 'status', 'message': 'subscribed to: AM.AAL', 'status': 'success'}]
[{'ev': 'status', 'message': 'subscribed to: AM.GE', 'status': 'success'}]

After which the data will start flowing once the data becomes available.

Upvotes: 0

Related Questions