Is there a way to stream order updates with python?

I am trying able to use api requests to buy place orders to buy equity, but I have to call the api every time to check if the order has been filled.

Is there a way to stream updates with the orders to show when they have been been filled?

i try this:

import alpaca_trade_api as tradeapi
import time
import datetime

api = tradeapi.REST('xxx','https://paper-api.alpaca.markets')
conn = tradeapi.StreamConn('xxx','xxx','https://paper-api.alpaca.markets')
account = api.get_account()

def run():
    @conn.on(r'trade_updates')
    async def on_msg(conn, channel, data):
        datasymbol = data.order['symbol']
        event = data.event
        print('Order executed for',datasymbol, data.order['side'], event, data.order['filled_qty'])
        
conn.run(['trade_updates'])

but it get the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
D:\Users\user\Anaconda3\envs\ml\lib\site-packages\alpaca_trade_api\stream2.py in run(self, initial_channels)
    158         try:
--> 159             loop.run_until_complete(self.subscribe(initial_channels))
    160             loop.run_forever()

D:\Users\user\Anaconda3\envs\ml\lib\asyncio\base_events.py in run_until_complete(self, future)
    565         try:
--> 566             self.run_forever()
    567         except:

D:\Users\user\Anaconda3\envs\ml\lib\asyncio\base_events.py in run_forever(self)
    520         if self.is_running():
--> 521             raise RuntimeError('This event loop is already running')
    522         if events._get_running_loop() is not None:

RuntimeError: This event loop is already running

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
<ipython-input-2-8a009c05ab30> in <module>
      6         print('Order executed for',datasymbol, data.order['side'], event, data.order['filled_qty'])
      7 
----> 8 conn.run(['trade_updates'])

D:\Users\user\Anaconda3\envs\ml\lib\site-packages\alpaca_trade_api\stream2.py in run(self, initial_channels)
    162             logging.info("Exiting on Interrupt")
    163         finally:
--> 164             loop.run_until_complete(self.close())
    165             loop.close()
    166 

D:\Users\user\Anaconda3\envs\ml\lib\asyncio\base_events.py in run_until_complete(self, future)
    564         future.add_done_callback(_run_until_complete_cb)
    565         try:
--> 566             self.run_forever()
    567         except:
    568             if new_task and future.done() and not future.cancelled():

D:\Users\user\Anaconda3\envs\ml\lib\asyncio\base_events.py in run_forever(self)
    519         self._check_closed()
    520         if self.is_running():
--> 521             raise RuntimeError('This event loop is already running')
    522         if events._get_running_loop() is not None:
    523             raise RuntimeError(

RuntimeError: This event loop is already running

can anyone tell me why this is happening? did i code it wrong?

I’m not really sure about the Python API, but I think you need to be listening for the “order_updates” event. When an order is submitted, you should immediately have an event fire with an event type of “new”. Once the order fills, you’ll have another event fire with the type of “fill”. With Websockets, you shouldn’t need to manually hit the API… data should just automatically flow into your app.