Unable to subscribe to stream - loop error - code included for review

Hello,

I am attempting to subscribe to a stream, but unable to get this to work. Does anyone know suggested fixes? I have 2 different types of errors:

  1. When I run it without the “trade_info” function, I get the following repetitive error:
    ERROR:root:error while consuming ws messages: As of 3.10, the loop parameter was removed from Lock() since it is no longer necessary

  2. When I run it with the “trade_info” function, I get the following repetitive error:
    ERROR:root:error while consuming ws messages: unknown channel trade_info (you may need to specify the right data_stream)

import alpaca_trade_api as tradeapi
import config as cfg
import threading

#instantiate REST API
api = tradeapi.REST(cfg.API_KEY, cfg.SECRET_KEY, base_url=cfg.BASE_URL, api_version=‘v2’)

#obtain account information
account = api.get_account()
print(account)

ws_url = ‘wss://data.alpaca.markets’
conn = tradeapi.stream2.StreamConn(cfg.API_KEY, cfg.SECRET_KEY, base_url=cfg.BASE_URL,
data_url=ws_url, data_stream=‘alpacadatav1’)

@conn.on(r’^account_updates$')
async def on_account_updates(conn, channel, account):
print(‘account’, account)

@conn.on(r’^trade_updates$')
async def on_trade_updates(conn, channel, trade):
print(‘trade’, trade)

@conn.on(r’^T.AAPL$')
async def trade_info(conn, channel, bar):
print(‘bars’, bar)
#print(bar._raw)
print(bar.price)

def ws_start():
conn.run([‘account_updates’, ‘trade_updates’])

#start WebSocket in a thread
ws_thread = threading.Thread(target=ws_start, daemon=True)
ws_thread.start()

Can you try upgrading the websockets library to 10.1? Or maybe try with python3.9?

I am still rather new working with all of this and on PyCharm. I see that my Python Interpreter is set to Python 3.10 but I don’t have any other versions I can select from. Is there an easy way to add another?

when I installed the alpaca trade api, wouldn’t the websockets library be specified to the latest?

I did upgrade to 10.1 on the websockets by installing the package, but upon re-running, same issue.

Hey, I put up a PR that supports python 3.10. It’s not merged in yet, but you can still test it if you’d like.

1 Like

This is so cool of you! Thank you!