From 5c98bf62ae689719de9a0ec828e7dde7f4e47aa1 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Wed, 27 Nov 2024 13:00:59 -0800 Subject: [PATCH 1/2] async order filled --- brokers/tradier_broker.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/brokers/tradier_broker.py b/brokers/tradier_broker.py index 28d0c6a..c037bad 100644 --- a/brokers/tradier_broker.py +++ b/brokers/tradier_broker.py @@ -95,19 +95,32 @@ def get_positions(self): logger.error('Failed to retrieve positions', extra={'error': str(e)}) - def _is_order_filled(self, order_id): + async def _is_order_filled(self, order_id): logger.info('Checking if order is filled', extra={'order_id': order_id}) try: - response = requests.get( - f"{self.base_url}/accounts/{self.account_id}/orders/{order_id}", headers=self.headers) - response.raise_for_status() - order_status = response.json()['order']['status'] - logger.info('Order status retrieved', extra={ - 'order_status': order_status}) - return order_status == 'filled' - except requests.RequestException as e: - logger.error('Failed to retrieve order status', - extra={'error': str(e)}) + async with aiohttp.ClientSession(headers=self.headers) as session: + async with session.get( + f"{self.base_url}/accounts/{self.account_id}/orders/{order_id}" + ) as response: + if response.status != 200: + logger.error( + 'Failed to retrieve order status', + extra={'error': f"HTTP status code {response.status}"} + ) + return False + data = await response.json() + order_status = data['order']['status'] + logger.info( + 'Order status retrieved', + extra={'order_status': order_status} + ) + return order_status == 'filled' + except aiohttp.ClientError as e: + logger.error( + 'Failed to retrieve order status', + extra={'error': str(e)} + ) + return False def _place_order(self, symbol, quantity, side, price=None, order_type='limit'): logger.info('Placing order', extra={ From 905f71b5fc184e6b4f739347d620fb137a2e9596 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Wed, 27 Nov 2024 13:04:58 -0800 Subject: [PATCH 2/2] fix tt --- brokers/tastytrade_broker.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/brokers/tastytrade_broker.py b/brokers/tastytrade_broker.py index 486526c..07a8a1d 100644 --- a/brokers/tastytrade_broker.py +++ b/brokers/tastytrade_broker.py @@ -333,7 +333,11 @@ async def _place_order(self, symbol, quantity, side, price=None, order_type='lim def _is_order_filled(self, order_id): status = self._get_order_status(order_id) - return self.check_is_order_filled_from_response(status) + if status is None: + return False + if status.get('remaining_quantity') == 0: + return True + return False def _get_order_status(self, order_id): logger.info('Retrieving order status', extra={'order_id': order_id})