From 5e3ea5400e765d2a8b98ef342925895140847aa5 Mon Sep 17 00:00:00 2001 From: Alex Golec Date: Tue, 2 Apr 2024 21:25:00 -0400 Subject: [PATCH] Account information --- schwab/__init__.py | 10 ++++++ schwab/client/base.py | 60 ++++++++++++++++++++++++++++++++++-- schwab/client/synchronous.py | 10 +++--- schwab/debug.py | 2 +- 4 files changed, 74 insertions(+), 8 deletions(-) diff --git a/schwab/__init__.py b/schwab/__init__.py index e69de29..4556514 100644 --- a/schwab/__init__.py +++ b/schwab/__init__.py @@ -0,0 +1,10 @@ +from . import auth +from . import client +#from . import contrib +from . import debug +#from . import orders +#from . import streaming + +from .version import version as __version__ + +LOG_REDACTOR = debug.LogRedactor() diff --git a/schwab/client/base.py b/schwab/client/base.py index 9e59c87..5f7c599 100644 --- a/schwab/client/base.py +++ b/schwab/client/base.py @@ -9,7 +9,7 @@ import json import logging import pickle -#import tda +import schwab import time import warnings @@ -44,7 +44,7 @@ def __init__(self, api_key, session, *, enforce_enums=True, self.logger = get_logger() self.request_number = 0 - #tda.LOG_REDACTOR.register(api_key, 'API_KEY') + schwab.LOG_REDACTOR.register(api_key, 'API_KEY') self.token_metadata = token_metadata @@ -129,3 +129,59 @@ def set_timeout(self, timeout): #setting-a-default-timeout-on-a-client>`__ for examples.''' self.session.timeout = timeout + + + ########################################################################## + # Accounts + + class Account: + class Fields(Enum): + '''Account fields passed to :meth:`get_account` and + :meth:`get_accounts`''' + POSITIONS = 'positions' + ORDERS = 'orders' + + def get_account(self, account_id, *, fields=None): + '''Account balances, positions, and orders for a specific account. + `Official documentation + `__. + + :param fields: Balances displayed by default, additional fields can be + added here by adding values from :class:`Account.Fields`. + ''' + fields = self.convert_enum_iterable(fields, self.Account.Fields) + + params = {} + if fields: + params['fields'] = ','.join(fields) + + path = '/trader/v1/accounts/{}'.format(account_id) + return self._get_request(path, params) + + def get_account_numbers(self): + # TODO: Documentation + ''' + ''' + path = '/trader/v1/accounts/accountNumbers' + return self._get_request(path, {}) + + def get_accounts(self, *, fields=None): + '''Account balances, positions, and orders for all linked accounts. + `Official documentation + `__. + + :param fields: Balances displayed by default, additional fields can be + added here by adding values from :class:`Account.Fields`. + ''' + fields = self.convert_enum_iterable(fields, self.Account.Fields) + + params = {} + if fields: + params['fields'] = ','.join(fields) + + path = '/trader/v1/accounts' + return self._get_request(path, params) + + diff --git a/schwab/client/synchronous.py b/schwab/client/synchronous.py index f025d14..ba22b43 100644 --- a/schwab/client/synchronous.py +++ b/schwab/client/synchronous.py @@ -12,7 +12,7 @@ class Client(BaseClient): def _get_request(self, path, params): self.ensure_updated_refresh_token() - dest = 'https://api.tdameritrade.com' + path + dest = 'https://api.schwabapi.com' + path req_num = self._req_num() self.logger.debug('Req %s: GET to %s, params=%s', @@ -26,7 +26,7 @@ def _get_request(self, path, params): def _post_request(self, path, data): self.ensure_updated_refresh_token() - dest = 'https://api.tdameritrade.com' + path + dest = 'https://api.schwabapi.com' + path req_num = self._req_num() self.logger.debug('Req %s: POST to %s, json=%s', @@ -40,7 +40,7 @@ def _post_request(self, path, data): def _put_request(self, path, data): self.ensure_updated_refresh_token() - dest = 'https://api.tdameritrade.com' + path + dest = 'https://api.schwabapi.com' + path req_num = self._req_num() self.logger.debug('Req %s: PUT to %s, json=%s', @@ -54,7 +54,7 @@ def _put_request(self, path, data): def _patch_request(self, path, data): self.ensure_updated_refresh_token() - dest = 'https://api.tdameritrade.com' + path + dest = 'https://api.schwabapi.com' + path req_num = self._req_num() self.logger.debug('Req %s: PATCH to %s, json=%s', @@ -68,7 +68,7 @@ def _patch_request(self, path, data): def _delete_request(self, path): self.ensure_updated_refresh_token() - dest = 'https://api.tdameritrade.com' + path + dest = 'https://api.schwabapi.com' + path req_num = self._req_num() self.logger.debug('Req %s: DELETE to %s'.format(req_num, dest)) diff --git a/schwab/debug.py b/schwab/debug.py index ff8a707..2910a7e 100644 --- a/schwab/debug.py +++ b/schwab/debug.py @@ -131,7 +131,7 @@ def _enable_bug_report_logging(output=sys.stderr, loggers=None): loggers = ( schwab.auth.get_logger(), schwab.client.base.get_logger(), - schwab.streaming.get_logger(), + #schwab.streaming.get_logger(), get_logger()) class RecordingHandler(logging.Handler):