Skip to content

Commit

Permalink
fix: adjust auth request for TradingStream
Browse files Browse the repository at this point in the history
  • Loading branch information
hiohiohio committed Jan 20, 2025
1 parent 18e2403 commit 6ca95df
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions alpaca/trading/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TradingStream:
"""
This is a WebSocket client which allows you to streaming data from your trading account.
Learn more here: https://alpaca.markets/docs/api-references/trading-api/streaming/
Learn more here: https://docs.alpaca.markets/docs/websocket-streaming
If using paper keys, make sure to set ``paper`` to True when instantiating the client.
"""
Expand All @@ -30,12 +30,12 @@ def __init__(
secret_key: str,
paper: bool = True,
raw_data: bool = False,
url_override: str = None,
url_override: Optional[str] = None,
websocket_params: Optional[Dict] = None,
):
self._api_key = api_key
self._secret_key = secret_key
self._trade_updates_handler = None
self._trade_updates_handler: Optional[Callable] = None
self._endpoint = (
url_override
if url_override
Expand All @@ -45,7 +45,7 @@ def __init__(
self._running = False
self._loop = None
self._raw_data = raw_data
self._stop_stream_queue = queue.Queue()
self._stop_stream_queue: queue.Queue = queue.Queue()
self._should_run = True
self._websocket_params = {
"ping_interval": 10,
Expand All @@ -65,11 +65,9 @@ async def _auth(self):
await self._ws.send(
json.dumps(
{
"action": "authenticate",
"data": {
"key_id": self._api_key,
"secret_key": self._secret_key,
},
"action": "auth",
"key": self._api_key,
"secret": self._secret_key,
}
)
)
Expand Down Expand Up @@ -201,7 +199,7 @@ async def stop_ws(self) -> None:

def stop(self) -> None:
"""Stops the websocket connection."""
if self._loop.is_running():
if self._loop is not None and self._loop.is_running():
asyncio.run_coroutine_threadsafe(self.stop_ws(), self._loop).result()

def run(self) -> None:
Expand Down

0 comments on commit 6ca95df

Please sign in to comment.