refactor: replace position check error with friendly 'No open positions found' message

This commit is contained in:
justin committed 2026-02-12 09:42:21 -07:00
1 parent e21bf3c8dd
commit b4a645cf01
2 files changed
+11 -2

No files matched your search

+8 -1
View File
@@ -1,7 +1,14 @@
import time
import logging
import backoff
import alpaca_trade_api as tradeapi
logging.getLogger('backoff').setLevel(logging.CRITICAL)
def _is_position_not_found(e):
return isinstance(e, tradeapi.rest.APIError) and "position does not exist" in str(e)
class AlpacaClient:
def __init__(self, api_key_id, api_secret_key, base_url, api_version="v2"):
@@ -50,7 +57,7 @@ class AlpacaClient:
def close_all_positions(self):
return self.api.close_all_positions()
@backoff.on_exception(backoff.expo, (tradeapi.rest.APIError, ConnectionError), max_tries=5, jitter=backoff.full_jitter)
@backoff.on_exception(backoff.expo, (tradeapi.rest.APIError, ConnectionError), max_tries=5, jitter=backoff.full_jitter, giveup=_is_position_not_found)
def get_position(self, symbol):
return self.api.get_position(symbol)