Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rd/async order filled #89

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion brokers/tastytrade_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
35 changes: 24 additions & 11 deletions brokers/tradier_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down