为什么我的 WebSocket 协程在以下代码中未被调用?

huangapple go评论65阅读模式
英文:

Why is my websocket coroutine not being called in the following code?

问题

I am using alpaca-py, Alpaca's new python package, to create a basic tradebot. My goal is to have the bot make a trade (buy), get data back from Alpaca's webhook regarding whether the order was filled (and some other information), and then make another trade with the same shares (sell). Before attempting to integrate the webhook, the bot was buying and selling fine. I cannot seem to get a coroutine up and running, however.

I have tried the following:

  1. Move await statements to different areas in the coroutines.
  2. Change the placement of the method and removed async from various methods.
  3. Check Alpaca's documentation. (Unfortunately, alpaca-py launched in 2023 and a lot of their documentation is outdated)
  4. Read the TradingStream code to ensure I am doing everything correctly. All looks good.
  5. Change the asyncio.gather call and run them both as routines. I get the same result.
  6. Add logger statements to the code. This clued me in that my method 'trade_update_handler' isn't being called, as nothing gets printed to the console.
  7. Used 'run()' instead of '_run_forever()' however this causes an error on the webhook side.

I am using Django to run the bot, as I like its BaseCommand class. I don't think django has anything to do with the issue. Here is my code:

class TradeMaker():
    # ... (The rest of your Python code)

When I run the command, I get the following output in my terminal (information censored for security):

python manage.py trade_maker_v5
2023-03-30 11:51:48,342 - INFO - Subscribing to trade updates
2023-03-30 11:51:48,342 - INFO - Preparing stream
Bought AAPL: id=UUID('foo') client_order_id='bar' created_at=datetime.datetime(2023, 3, 30, 18, 51, 49, 995853, tzinfo=datetime.timezone.utc) updated_at=datetime.datetime(2023, 3, 30, 18, 51, 49, 995921, tzinfo=datetime.timezone.utc) submitted_at=datetime.datetime(2023, 3, 30, 18, 51, 49, 994623, tzinfo=datetime.timezone.utc) filled_at=None expired_at=None canceled_at=None failed_at=None replaced_at=None replaced_by=None replaces=None asset_id=UUID('foo') symbol='AAPL' asset_class=<AssetClass.US_EQUITY: 'us_equity'> notional=None qty='1' filled_qty='0' filled_avg_price=None order_class=<OrderClass.SIMPLE: 'simple'> order_type=<OrderType.MARKET: 'market'> type=<OrderType.MARKET: 'market'> side=<OrderSide.BUY: 'buy'> time_in_force=<TimeInForce.DAY: 'day'> limit_price=None stop_price=None status=<OrderStatus.PENDING_NEW: 'pending_new'> extended_hours=False legs=None trail_percent=None trail_price=None hwm=None
2023-03-30 11:51:48,480 - INFO - Waiting for order status update
2023-03-30 11:51:49,493 - INFO - Waiting for order status update
2023-03-30 11:51:50,500 - INFO - Waiting for order status update

If I run the webhook separately in another terminal while my bot runs, it works. I can run the following code:

from alpaca.trading.stream import TradingStream

trading_stream = TradingStream(ALPACA_ID, ALPACA_KEY, paper=True)

async def update_handler(data):
    print(data)

trading_stream.subscribe_trade_updates(update_handler)
trading_stream.run()

It will print out all the data as my bot runs. Why would it work separately but not in a coroutine?

英文:

I am using alpaca-py, Alpaca's new python package, to create a basic tradebot. My goal is to have the bot make a trade (buy), get data back from Alpaca's webhook regarding whether the order was filled (and some other information), and then make another trade with the same shares (sell). Before attempting to intergrate the webhook, the bot was buying and selling fine. I cannot seem to get a coroutine up and running, however.

I have tried the following:

  1. Move await statements to different areas in the coroutines.
  2. Change the placement of the method and removed async from various methods.
  3. Check Alpaca's documentation. (Unfortunately, alpaca-py launched in 2023 and a lot of their documentation is outdated)
  4. Read the TradingStream code to ensure I am doing everything correctly. All looks good.
  5. Change the asyncio.gather call and run them both as routines. I get the same result.
  6. Add logger statements to the code. This clued me in that my method 'trade_update_handler' isn't being called, as nothing gets printed to the console.
  7. Used 'run()' instead of '_run_forever()' however this causes an error on the webhook side.

I am using Django to run the bot, as I like it's BaseCommand class. I don't think django has anything to do with the issue. Here is my code:

class TradeMaker():
    def __init__(self, **kwargs):
        self.paper_bool = kwargs.get(&#39;paper_bool&#39;, True)
        self.random_bool = kwargs.get(&#39;random&#39;, True)
        self.symbol_or_symbols = kwargs.get(&#39;symbol_or_symbols&#39;, &#39;AAPL&#39;)
        self.amount = kwargs.get(&#39;amount&#39;, 40000)
        self.seconds_between = kwargs.get(&#39;seconds_between&#39;, 4)
        self.log = kwargs.get(&#39;log&#39;)
        self.trading_client, self.trading_stream, self.account = self.open_client()
        self.trade_update_info = None
        self.order_filled = False
        self.shares_bought = 0
        self.current_symbol = None
    
    def open_client(self):
        trading_client = TradingClient(ALPACA_ID, ALPACA_KEY, paper=self.paper_bool)
        trading_stream = TradingStream(ALPACA_ID, ALPACA_KEY, paper=self.paper_bool)
        try:
            account = trading_client.get_account()
        except Exception as e:
            logger.error(f&quot;Exception in login: {e}&quot;)
        return trading_client, trading_stream, account
    
    async def trade_update_handler(self, data):
        logger.info(&#39;Trade Update called&#39;)
        print(&quot;Trade Update:&quot;, data)
        if data.event == TradeEvent.FILL:
            if data.order.side == OrderSide.BUY:
                self.order_filled = True
                self.shares_bought = data.order.filled_qty
                self.current_symbol = data.order.symbol

    async def run_stream(self):
        logger.info(&#39;Subscribing to trade updates&#39;)
        self.trading_stream.subscribe_trade_updates(self.trade_update_handler)
        logger.info(&#39;Preparing stream&#39;)
        await self.trading_stream._run_forever()

    async def stop_stream(self):
        logger.info(&#39;Stopping stream&#39;)
        trading_stream = TradingStream(ALPACA_ID, ALPACA_KEY, paper=self.paper_bool)
        await trading_stream.stop()

    def get_symbol(self):
        if self.random_bool:
            symbol = random.choice(self.symbol_or_symbols)
            return symbol
        else:
            symbol = self.symbol_or_symbols
            return symbol

    def buy(self):
        symbol = self.get_symbol()
        market_order_data = MarketOrderRequest(
            symbol=symbol,
            qty=1,
            side=OrderSide.BUY,
            time_in_force=TimeInForce.DAY
        )
        try:
            market_order_buy = self.trading_client.submit_order(
                    order_data=market_order_data
            )
        except Exception as e:
            logger.error(f&quot;Failed to buy {symbol}: {e}&quot;)
            return None
        return symbol, market_order_buy

    def sell(self, symbol):
        symbol = symbol
        shares = self.shares_bought
        
        market_order_data = MarketOrderRequest(
            symbol=symbol,
            qty=250,
            side=OrderSide.SELL,
            time_in_force=TimeInForce.DAY
        )
        try:
            market_order_sell = self.trading_client.submit_order(
                    order_data=market_order_data
            )
        except Exception as e:
            logger.error(f&quot;Failed to sell {symbol}: {e}&quot;)
            return None
        return market_order_sell

    async def make_trades(self):
        market_close = datetime.datetime.now().replace(hour=14, minute=0, second=0, microsecond=0)
        while datetime.datetime.now() &lt; market_close:
            seconds = self.seconds_between
            try:
                symbol, market_order_buy = self.buy()
                print(f&quot;Bought {symbol}: {market_order_buy}&quot;)
            except Exception as e:
                logger.error(f&quot;Failed to buy during trade: {e}&quot;)
                return None
            while not self.order_filled:
                logger.info(&#39;Waiting for order status update&#39;)
                await asyncio.sleep(1)
            sleep(seconds)
            try:
                market_order_sell = self.sell(symbol=symbol)
                print(f&quot;Sold {self.current_symbol}: {market_order_sell}&quot;)
            except Exception as e:
                logger.error(f&quot;Failed to sell during trade: {e}&quot;)
                return None
            self.order_filled = False
            self.shares_bought = 0
            sleep(seconds)
        print(&#39;Market closed, shutting down.&#39;)

class Command(BaseCommand):
    help = &quot;&quot;&quot;This bot trades the target stock. If you want it to choose randomly, pass it a list and set the variable random=True
    &quot;&quot;&quot;
    model = None

    def add_arguments(self, parser):
        parser.add_argument(
            &#39;--paper&#39;,
            type=bool,
            help=&#39;Set false to live trade.&#39;,
            default=True
        )
        parser.add_argument(
            &#39;--folder&#39;,
            type=str,
            help=&#39;source folder for files&#39;,
            default=&#39;&#39;
        )
        parser.add_argument(
            &#39;--symbol&#39;,
            type=str,
            help=&#39;target symbol, or list of symbols&#39;,
            default=&#39;AAPL&#39;
        )
        parser.add_argument(
            &#39;--random&#39;,
            type=bool,
            help=&quot;Set to true if passing a list of symbols to choose randomly from.&quot;,
            default=False
        )
        parser.add_argument(
            &#39;--tradevalue&#39;,
            type=int,
            help=&quot;The amount the bot should trade. e.g. $40000&quot;,
            default=40000
        )
        parser.add_argument(
            &#39;--seconds&#39;,
            type=int,
            help=&quot;The number of seconds the bot should wait between each trade.&quot;,
            default=4
        )

    def handle(self, **options):
        paper_bool = options[&#39;paper&#39;]
        random_bool = options[&#39;random&#39;]
        symbol_or_symbols = options[&#39;symbol&#39;]
        amount = options[&#39;tradevalue&#39;]
        seconds_between = options[&#39;seconds&#39;]
        log = options[&#39;folder&#39;]
        tm = TradeMaker(
            paper_bool = paper_bool,
            random = random_bool,
            symbol_or_symbols = symbol_or_symbols,
            amount = amount,
            seconds_between = seconds_between,
            log = log
        )
        loop = asyncio.get_event_loop()
        try:
            loop.run_until_complete(asyncio.gather(
                tm.run_stream(),
                tm.make_trades()
            ))
        except KeyboardInterrupt:
            tm.stop_stream()
            print(&quot;Stopped with Interrupt&quot;)
        finally:
            tm.stop_stream()
            loop.close()

When I run the command, I get the following output in my terminal (information censored for security):

    python manage.py trade_maker_v5
2023-03-30 11:51:48,342 - INFO - Subscribing to trade updates
2023-03-30 11:51:48,342 - INFO - Preparing stream
Bought AAPL: id=UUID(&#39;foo&#39;) client_order_id=&#39;bar&#39; created_at=datetime.datetime(2023, 3, 30, 18, 51, 49, 995853, tzinfo=datetime.timezone.utc) updated_at=datetime.datetime(2023, 3, 30, 18, 51, 49, 995921, tzinfo=datetime.timezone.utc) submitted_at=datetime.datetime(2023, 3, 30, 18, 51, 49, 994623, tzinfo=datetime.timezone.utc) filled_at=None expired_at=None canceled_at=None failed_at=None replaced_at=None replaced_by=None replaces=None asset_id=UUID(&#39;foo&#39;) symbol=&#39;AAPL&#39; asset_class=&lt;AssetClass.US_EQUITY: &#39;us_equity&#39;&gt; notional=None qty=&#39;1&#39; filled_qty=&#39;0&#39; filled_avg_price=None order_class=&lt;OrderClass.SIMPLE: &#39;simple&#39;&gt; order_type=&lt;OrderType.MARKET: &#39;market&#39;&gt; type=&lt;OrderType.MARKET: &#39;market&#39;&gt; side=&lt;OrderSide.BUY: &#39;buy&#39;&gt; time_in_force=&lt;TimeInForce.DAY: &#39;day&#39;&gt; limit_price=None stop_price=None status=&lt;OrderStatus.PENDING_NEW: &#39;pending_new&#39;&gt; extended_hours=False legs=None trail_percent=None trail_price=None hwm=None
2023-03-30 11:51:48,480 - INFO - Waiting for order status update
2023-03-30 11:51:49,493 - INFO - Waiting for order status update
2023-03-30 11:51:50,500 - INFO - Waiting for order status update

If I run the webhook seprately in another terminal while my bot runs, it works. I can run the following code:

    from alpaca.trading.stream import TradingStream

trading_stream = TradingStream(ALPACA_ID, ALPACA_KEY, paper=True)

async def update_handler(data):
    print(data)

trading_stream.subscribe_trade_updates(update_handler)
trading_stream.run()

It will print out all the data as my bot runs. Why would it work seperately, but not in a coroutine?

答案1

得分: 0

Django导致了问题。将机器人从Django中移除,使其成为独立的机器人,并添加了一些额外的异步语句后,现在它可以工作。

Django最近在4.2版本中添加了异步支持,但该项目使用的是Django 4.1。

英文:

Django was causing the issue. After removing the bot from Django and making it a standalone bot and adding a few more async statements, it now works.

Django recently added async in 4.2, but this project was running Django 4.1

huangapple
  • 本文由 发表于 2023年3月31日 03:00:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定