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

api for get order by id #25

Merged
merged 1 commit into from
Jun 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
26 changes: 26 additions & 0 deletions cschwabpy/SchwabAsyncClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,31 @@ async def place_order_async(
if not self.__keep_client_alive:
await client.aclose()

async def get_order_by_id_async(
self,
account_number_hash: AccountNumberWithHashID,
order_id: int,
) -> Optional[Order]:
"""Get a specific order by order ID."""
await self._ensure_valid_access_token()
target_url = f"{SCHWAB_TRADER_API_BASE_URL}/accounts/{account_number_hash.hashValue}/orders/{order_id}"
client = httpx.AsyncClient() if self.__client is None else self.__client
try:
response = await client.get(
url=target_url, params={}, headers=self.__auth_header()
)
if response.status_code == 200:
order_json = response.json()
return Order(**order_json)
elif response.status_code == 404:
# order not found
return None
else:
raise Exception("Failed to get order. Status: ", response.status_code)
finally:
if not self.__keep_client_alive:
await client.aclose()

async def get_orders_async(
self,
account_number_hash: AccountNumberWithHashID,
Expand All @@ -241,6 +266,7 @@ async def get_orders_async(
max_count: int = 1000,
status: Optional[OrderStatus] = None,
) -> List[Order]:
"""Get orders for a specific account within a time range."""
await self._ensure_valid_access_token()
target_url = f"{SCHWAB_TRADER_API_BASE_URL}/accounts/{account_number_hash.hashValue}/orders"
target_url += f"?fromEnteredTime={util.to_iso8601_str(from_entered_time)}&toEnteredTime={util.to_iso8601_str(to_entered_time)}&maxResults={max_count}"
Expand Down
25 changes: 25 additions & 0 deletions cschwabpy/SchwabClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,31 @@ def place_order(
if not self.__keep_client_alive:
client.close()

def get_order_by_id(
self,
account_number_hash: AccountNumberWithHashID,
order_id: int,
) -> Optional[Order]:
"""Get a specific order by order ID."""
self._ensure_valid_access_token()
target_url = f"{SCHWAB_TRADER_API_BASE_URL}/accounts/{account_number_hash.hashValue}/orders/{order_id}"
client = httpx.Client() if self.__client is None else self.__client
try:
response = client.get(
url=target_url, params={}, headers=self.__auth_header()
)
if response.status_code == 200:
order_json = response.json()
return Order(**order_json)
elif response.status_code == 404:
# order not found
return None
else:
raise Exception("Failed to get order. Status: ", response.status_code)
finally:
if not self.__keep_client_alive:
client.close()

def get_orders(
self,
account_number_hash: AccountNumberWithHashID,
Expand Down
1 change: 1 addition & 0 deletions cschwabpy/models/trade_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class OrderActivityType(str, Enum):

class ExecutionType(str, Enum):
FILL = "FILL"
CANCELED = "CANCELED"


class QuantityType(str, Enum):
Expand Down
Loading