alpacahq / alpaca-py

The Official Python SDK for Alpaca API
https://alpaca.markets/sdks/python/getting_started.html
Apache License 2.0
602 stars 147 forks source link

[Bug]: StockDataStream has no output on test websocket #487

Closed JeffStodd closed 4 months ago

JeffStodd commented 4 months ago

Is there an existing issue for this?

Current Behavior

From the docs:

We provide a test stream that is available all the time, even outside market hours, on this URL:

wss://stream.data.alpaca.markets/v2/test

However this was not returning anything when run at 7PM pacific:

from alpaca.data.live import StockDataStream

wss_client = StockDataStream('{API_KEY}', '{SECRET_KEY}', url_override='wss://stream.data.alpaca.markets/v2/test')
async def on_quote(data):
    print(data)
wss_client.subscribe_quotes(on_quote, "SPY")
wss_client.run()
INFO:alpaca.data.live.websocket:started data stream
INFO:alpaca.data.live.websocket:starting data websocket connection
INFO:alpaca.data.live.websocket:connecting to wss://stream.data.alpaca.markets/v2/test
DEBUG:websockets.client:= connection is CONNECTING
DEBUG:websockets.client:> GET /v2/test HTTP/1.1
DEBUG:websockets.client:> Host: stream.data.alpaca.markets
DEBUG:websockets.client:> Upgrade: websocket
DEBUG:websockets.client:> Connection: Upgrade
DEBUG:websockets.client:> Sec-WebSocket-Version: 13
DEBUG:websockets.client:> Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
DEBUG:websockets.client:> Content-Type: application/msgpack
DEBUG:websockets.client:> User-Agent: APCA-PY/0.28.0
DEBUG:websockets.client:< HTTP/1.1 101 Switching Protocols
DEBUG:websockets.client:< Date: Wed, 10 Jul 2024 02:24:27 GMT
DEBUG:websockets.client:< Connection: upgrade
DEBUG:websockets.client:< Upgrade: websocket
DEBUG:websockets.client:< Strict-Transport-Security: max-age=31536000; includeSubDomains
DEBUG:websockets.client:= connection is OPEN
DEBUG:websockets.client:< BINARY 91 82 a1 54 a7 73 75 63 63 65 73 73 a3 6d 73 67 ... 6f 6e 6e 65 63 74 65 64 [26 bytes]
DEBUG:websockets.client:> BINARY 83 a6 61 63 74 69 6f 6e a4 61 75 74 68 a3 6b 65 ... 63 62 4a 61 58 57 32 31 [87 bytes]
DEBUG:websockets.client:< BINARY 91 82 a1 54 a7 73 75 63 63 65 73 73 a3 6d 73 67 ... 6e 74 69 63 61 74 65 64 [30 bytes]
INFO:alpaca.data.live.websocket:connected to wss://stream.data.alpaca.markets/v2/test
DEBUG:websockets.client:> BINARY 82 a6 71 75 6f 74 65 73 91 a3 53 50 59 a6 61 63 ... 75 62 73 63 72 69 62 65 [30 bytes, continued]
DEBUG:websockets.client:> CONT '' [0 bytes]
DEBUG:websockets.client:< BINARY 91 84 a1 54 ac 73 75 62 73 63 72 69 70 74 69 6f ... 50 59 a4 62 61 72 73 90 [43 bytes]
INFO:alpaca.data.live.websocket:subscribed to quotes: ['SPY']
DEBUG:websockets.client:% sending keepalive ping
DEBUG:websockets.client:> PING d9 b3 c2 f4 [binary, 4 bytes]
DEBUG:websockets.client:< PONG d9 b3 c2 f4 [binary, 4 bytes]
...

Expected Behavior

No response

SDK Version I encountered this issue in

alpaca-py 0.28.0 Python 3.9.13

Steps To Reproduce

1. Run above code
2. Observe that there the subscribe_quotes handler is not being triggered

Filled out the Steps to Reproduce section?

Anything else?

No response

hiohiohio commented 4 months ago

@JeffStodd Thank you for the feedback. Could you please try to use FAKEPACA instead of SPY for the test stream?

https://docs.alpaca.markets/docs/streaming-market-data#subscription

For example in the test stream, you can send this message: {"action":"subscribe","trades":["FAKEPACA"]}

JeffStodd commented 4 months ago

@JeffStodd Thank you for the feedback. Could you please try to use FAKEPACA instead of SPY for the test stream?

https://docs.alpaca.markets/docs/streaming-market-data#subscription

For example in the test stream, you can send this message: {"action":"subscribe","trades":["FAKEPACA"]}

Thanks, that seems to work. By the way, is there a test websocket for paper trading?

hiohiohio commented 4 months ago

By the way, is there a test websocket for paper trading?

Yeah, in my understanding, you can use credentials (API key/secret) of a paper account.

JeffStodd commented 4 months ago

By the way, is there a test websocket for paper trading?

Yeah, in my understanding, you can use credentials (API key/secret) of a paper account.

Do you know why I am getting authentication errors? I verified my keys are correct (I am able to get account data with TradingClient) but it doesn't seem to connect to the sandbox websocket with paper account creds. Using exact same code but setting env vars differently: wss://stream.data.sandbox.alpaca.markets/v2/test or wss://stream.data.sandbox.alpaca.markets/v2/iex paper api key paper secret key

INFO:alpaca.data.live.websocket:started data stream
INFO:alpaca.data.live.websocket:starting data websocket connection
INFO:alpaca.data.live.websocket:connecting to wss://stream.data.sandbox.alpaca.markets/v2/test
DEBUG:websockets.client:= connection is CONNECTING
DEBUG:websockets.client:> GET /v2/test HTTP/1.1
DEBUG:websockets.client:> Host: stream.data.sandbox.alpaca.markets
DEBUG:websockets.client:> Upgrade: websocket
DEBUG:websockets.client:> Connection: Upgrade
DEBUG:websockets.client:> Sec-WebSocket-Version: 13
DEBUG:websockets.client:> Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
DEBUG:websockets.client:> Content-Type: application/msgpack
DEBUG:websockets.client:> User-Agent: APCA-PY/0.28.0
DEBUG:websockets.client:< HTTP/1.1 101 Switching Protocols
DEBUG:websockets.client:< Date: Wed, 10 Jul 2024 19:48:13 GMT
DEBUG:websockets.client:< Connection: upgrade
DEBUG:websockets.client:< Upgrade: websocket
DEBUG:websockets.client:< Strict-Transport-Security: max-age=31536000; includeSubDomains
DEBUG:websockets.client:= connection is OPEN
DEBUG:websockets.client:< BINARY 91 82 a1 54 a7 73 75 63 63 65 73 73 a3 6d 73 67 ... 6f 6e 6e 65 63 74 65 64 [26 bytes]
DEBUG:websockets.client:> BINARY 83 a6 61 63 74 69 6f 6e a4 61 75 74 68 a3 6b 65 ... 50 50 54 38 68 6d 42 42 [87 bytes]
DEBUG:websockets.client:< BINARY 91 83 a1 54 a5 65 72 72 6f 72 a4 63 6f 64 65 ce ... 68 20 66 61 69 6c 65 64 [36 bytes]
ERROR:alpaca.data.live.websocket:error during websocket communication: auth failed
Traceback (most recent call last):
  File "c:\Python39\lib\site-packages\alpaca\data\live\websocket.py", line 342, in _run_forever
    await self._start_ws()
  File "c:\Python39\lib\site-packages\alpaca\data\live\websocket.py", line 134, in _start_ws
    await self._auth()
  File "c:\Python39\lib\site-packages\alpaca\data\live\websocket.py", line 125, in _auth
    raise ValueError(msg[0].get("msg", "auth failed"))
ValueError: auth failed
hiohiohio commented 4 months ago

@JeffStodd Sandbox env is for Broker API keys. For paper account API key/secrets, could you please try to put your paper API key/secret to StockDataStream with sandbox=False and url_override=None? you should put your paper API key/secret with paper=True and url_override=None for the TradingClient.

JeffStodd commented 4 months ago

@JeffStodd Sandbox env is for Broker API keys. For paper account API key/secrets, could you please try to put your paper API key/secret to StockDataStream with sandbox=False and url_override=None? you should put your paper API key/secret with paper=True and url_override=None for the TradingClient.

Thanks, working now with those parameters