Forums

errno 111 with alpaca websocket

hello,

I'm using a Python script to connect to the Alpaca Market API (which is on the whitelist) from pythonanywhere via WebSocket, but unfortunately, I'm encountering error number 111, connectionRefusedError.

The code works fine when I run it from my personal computer, but when I execute it from PythonAnywhere, I get this error.

Here's the code:

########
import configPAP as config
import datetime
from alpaca_trade_api.common import URL
from alpaca_trade_api.stream import Stream

async def trade_callback(t):
    print('trade', t)
    time.sleep(50)

async def quote_callback(q):
    print('quote', q)


# Initiate Class Instance
stream = Stream(config.API_KEY,
                config.SECRET_KEY,
                base_url=URL('https://paper-api.alpaca.markets'),
                data_feed='iex',  # <- replace to 'sip' if you have PRO subscription
                    websocket_params= {'ping_interval': 50},)

# subscribing to event
stream.subscribe_trades(trade_callback, 'AAPL')
stream.subscribe_quotes(quote_callback, 'IBM')

stream.run()

" AND here's the encountered error : "

error during websocket communication:

[Errno 111] Connect call failed ('38.122.35.99', 443)
Traceback (most recent call last):
  File "/home/lco75005/.local/lib/python3.9/site-packages/alpaca_trade_api/stream.py", line 251, in _run_forever
    await self._start_ws()
  File "/home/lco75005/.local/lib/python3.9/site-packages/alpaca_trade_api/stream.py", line 104, in _start_ws
    await self._connect()
  File "/home/lco75005/.local/lib/python3.9/site-packages/alpaca_trade_api/stream.py", line 79, in _connect
    self._ws = await websockets.connect(
  File "/home/lco75005/.local/lib/python3.9/site-packages/websockets/legacy/client.py", line 659, in __await_impl_timeout__
    return await asyncio.wait_for(self.__await_impl__(), self.open_timeout)
  File "/usr/local/lib/python3.9/asyncio/tasks.py", line 481, in wait_for
    return fut.result()
  File "/home/lco75005/.local/lib/python3.9/site-packages/websockets/legacy/client.py", line 663, in __await_impl__
    _transport, _protocol = await self._create_connection()
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1056, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1041, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 955, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 502, in sock_connect
    return await fut
  File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 537, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('38.122.35.99', 443)"

So, what do you think about this error ? what's wrong with this code ?

Thank you for your responses.

[edit by admin: formatting]

Have you tried connecting via a python console? It might be easier to debug there.

One thing which seems off is that paper-api.alpaca.markets doesnt resolve to 38.122.35.99.. oddd

Maybe add some logging to make sure that the credentials are being loaded properly

hello, thank you for your response. even i dont't really get it :)

i use the PAW console. it's python console, no? If you're referring to a console like PyCharm, it works from my personal computer and therefore doesn't throw any errors

the address is on the alpaca package i don't touch the address (and the port too).

and i don't understand the last purpose. sorry, can you explain to me what do you think?

thanks,

You may need to manually tell it to use our proxy or upgrade to paid account.