Skip to content

[Bug]: RuntimeError: asyncio.run() cannot be called from a running event loop with Alpaca-py data/trading streams #618

@johnjul1951

Description

@johnjul1951

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

I am consistently encountering a RuntimeError: asyncio.run() cannot be called from a running event loop when attempting to initialize and run the live StockDataStream and TradingStream in my Python trading bot.

Problem Description:
My bot initiates an asyncio event loop using asyncio.run(main()). Within this main loop, I attempt to start the data_stream.run() and trading_stream.run() methods from the alpaca-py library as separate asyncio.create_task() operations, in order to receive real-time market data and order updates.

The error occurs as soon as the data_stream.run() method (and presumably the trading_stream.run() as well) tries to initialize. The traceback indicates an internal call to asyncio.run() within the alpaca-py library itself, resulting in a nesting error because an asyncio loop is already active.

Relevant Traceback (core of the error):

Traceback (most recent call last):
File "C:\Users\Gebruiker\miniconda3\envs\trading_bot_env_v4\lib\site-packages\alpaca\data\live\websocket.py", line 366, in run
asyncio.run(self._run_forever())
File "C:\Users\Gebruiker\miniconda3\envs\trading_bot_env_v4\lib\asyncio\runners.py", line 33, in run
raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop

My Environment and Troubleshooting Attempts:

Python Version: I am using Python 3.10 (installed via Miniconda).

alpaca-py Versions Tested:

    0.42.0 (originally) - produced the error.

    0.41.0 (after attempting a downgrade) - produced the exact same error.

Steps Taken: I attempted to upgrade to the latest available version via pip install --upgrade alpaca-py (which resulted in 0.42.0) and subsequently tried a downgrade to 0.41.0. Both versions exhibit the identical behavior.

Context: Earlier versions of my script that only fetched historical data (via data_client.get_stock_bars()) or just account information did not encounter this issue, as they did not call the run() methods of the live streams.

Expected Behavior:
The expected behavior is that the data_stream.run() and trading_stream.run() methods of alpaca-py should integrate seamlessly into an already active asyncio event loop when started as tasks (asyncio.create_task()), without attempting to call asyncio.run() internally themselves.

Request for Assistance:
I would appreciate guidance on the following:

Is this a known issue/bug in alpaca-py versions 0.41.0 and 0.42.0?

What is the recommended stable version of alpaca-py that correctly works with asyncio.create_task() for live streams within an already running event loop?

Is there a specific method or best practice for starting the StockDataStream and TradingStream that avoids this asyncio.run() nesting problem?

Thank you in advance for your help!

Expected Behavior

No response

SDK Version I encountered this issue in

"I encountered this issue in alpaca-py versions 0.42.0 (which was the latest when I first checked and also after attempting an upgrade) and 0.41.0 (after a downgrade attempt). Both versions exhibit the same RuntimeError: asyncio.run() cannot be called from a running event loop when attempting to start the live streams."

Steps To Reproduce

Set up a Python 3.10 environment (e.g., via Miniconda).

Install alpaca-py version 0.41.0 or 0.42.0 (pip install alpaca-py==0.41.0).

Ensure Alpaca API Key ID, Secret Key, and Base URL (https://paper-api.alpaca.markets) are set as environment variables.

Filled out the Steps to Reproduce section?

  • I have entered valid steps to reproduce my issue or have attached a minimally reproducible case in code that shows my issue happening; and understand that without this my issue will be flagged as invalid and closed after 30 days.

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions