Skip to content

Commit

Permalink
Added comment about get_quote and futures
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgolec committed Apr 21, 2020
1 parent 229cfe1 commit ed12244
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
42 changes: 26 additions & 16 deletions tda/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, api_key, session, *, enforce_enums=True):
self.api_key = api_key
self.session = session

# XXX: This class's tests perform monkey patching to inject synthetic values
# XXX: This class's tests perform monkey patching to inject synthetic values
# of utcnow(). To avoid being confused by this, capture these values here so
# we can use them later.
_DATETIME = datetime.datetime
Expand All @@ -40,16 +40,16 @@ def __init__(self, api_key, session, *, enforce_enums=True):
def __assert_type(self, name, value, exp_types):
value_type = type(value)
value_type_name = '{}.{}'.format(
value_type.__module__, value_type.__name__)
value_type.__module__, value_type.__name__)
exp_type_names = ['{}.{}'.format(
t.__module__, t.__name__) for t in exp_types]
t.__module__, t.__name__) for t in exp_types]
if not any(isinstance(value, t) for t in exp_types):
if len(exp_types) == 1:
error_str = "expected type '{}' for {}, got '{}'".format(
exp_type_names[0], name, value_type_name)
exp_type_names[0], name, value_type_name)
else:
error_str = "expected type in ({}) for {}, got '{}'".format(
', '.join(exp_type_names), name, value_type_name)
', '.join(exp_type_names), name, value_type_name)
raise ValueError(error_str)

def __format_datetime(self, var_name, dt):
Expand Down Expand Up @@ -241,8 +241,8 @@ def get_orders_by_query(self,
statuses=statuses))

def place_order(self, account_id, order_spec):
'''Place an order for a specific account. If order creation was
successful, the response will contain the ID of the generated order. See
'''Place an order for a specific account. If order creation was
successful, the response will contain the ID of the generated order. See
:meth:`tda.utils.Utils.extract_order_id` for more details.
`Official documentation
<https://developer.tdameritrade.com/account-access/apis/post/accounts/
Expand Down Expand Up @@ -383,6 +383,8 @@ def search_instruments(self, symbols, projection):
'projection': projection,
}

print(params)

path = '/v1/instruments'
return self.__get_request(path, params)

Expand Down Expand Up @@ -422,7 +424,7 @@ def get_hours_for_multiple_markets(self, markets, date):
:param markets: Market to return hours for. Iterable of
:class:`Markets`.
:param date: The date for which market hours information is requested.
:param date: The date for which market hours information is requested.
Accepts ``datetime.date`` and ``datetime.datetime``.
'''
markets = self.convert_enum_iterable(markets, self.Markets)
Expand All @@ -444,7 +446,7 @@ def get_hours_for_single_market(self, market, date):
:param markets: Market to return hours for. Instance of
:class:`Markets`.
:param date: The date for which market hours information is requested.
:param date: The date for which market hours information is requested.
Accepts ``datetime.date`` and ``datetime.datetime``.
'''
market = self.convert_enum(market, self.Markets)
Expand Down Expand Up @@ -632,10 +634,10 @@ def get_option_chain(
params['range'] = strike_range
if strike_from_date is not None:
params['fromDate'] = self.__format_date(
'strike_from_date', strike_from_date)
'strike_from_date', strike_from_date)
if strike_to_date is not None:
params['toDate'] = self.__format_date(
'strike_to_date', strike_to_date)
'strike_to_date', strike_to_date)
if volatility is not None:
params['volatility'] = volatility
if underlying_price is not None:
Expand Down Expand Up @@ -759,10 +761,10 @@ def get_price_history(
params['frequency'] = frequency
if start_datetime is not None:
params['startDate'] = self.__datetime_as_millis(
'start_datetime', start_datetime)
'start_datetime', start_datetime)
if end_datetime is not None:
params['endDate'] = self.__datetime_as_millis(
'end_datetime', end_datetime)
'end_datetime', end_datetime)
if need_extended_hours_data is not None:
params['needExtendedHoursData'] = need_extended_hours_data

Expand All @@ -773,19 +775,27 @@ def get_price_history(
# Quotes

def get_quote(self, symbol):
'''Get quote for a symbol.
'''
Get quote for a symbol. Note due to limitations in URL encoding, this
method is not recommended for instruments with symbols symbols
containing non-alphanumeric characters, for example as futures like
``/ES``. To get quotes for those symbols, use :meth:`Client.get_quotes`.
`Official documentation
<https://developer.tdameritrade.com/quotes/apis/get/marketdata/
%7Bsymbol%7D/quotes>`__.'''
%7Bsymbol%7D/quotes>`__.
'''
params = {
'apikey': self.api_key,
}

import urllib
path = '/v1/marketdata/{}/quotes'.format(symbol)
return self.__get_request(path, params)

def get_quotes(self, symbols):
'''Get quote for a symbol.
'''Get quote for a symbol. This method supports all symbols, including
those containing non-alphanumeric characters like ``/ES``.
`Official documentation
<https://developer.tdameritrade.com/quotes/apis/get/marketdata/
quotes>`__.'''
Expand Down
12 changes: 6 additions & 6 deletions tda/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ def set_account_id(self, account_id):

def extract_order_id(self, place_order_response):
'''Attempts to extract the order ID from a response object returned by
:meth:`Client.place_order() <tda.client.Client.place_order>`. Return
:meth:`Client.place_order() <tda.client.Client.place_order>`. Return
``None`` if the order location is not contained in the response.
:param place_order_response: Order response as returned by
:meth:`Client.place_order()
<tda.client.Client.place_order>`. Note this
method requires that the order was
:meth:`Client.place_order()
<tda.client.Client.place_order>`. Note this
method requires that the order was
successful.
:raise ValueError: if the order was not succesful or if the order's
account ID is not equal to the account ID set in this
:raise ValueError: if the order was not succesful or if the order's
account ID is not equal to the account ID set in this
``Utils`` object.
'''
Expand Down
44 changes: 22 additions & 22 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ def test_get_orders_by_path_vanilla(self):
def test_get_orders_by_path_from_not_datetime(self):
with self.assertRaises(ValueError) as cm:
self.client.get_orders_by_path(
ACCOUNT_ID, from_entered_datetime='2020-01-01')
ACCOUNT_ID, from_entered_datetime='2020-01-01')
self.assertEqual(str(cm.exception),
"expected type 'datetime.datetime' for " +
"from_entered_datetime, got 'builtins.str'")
"expected type 'datetime.datetime' for " +
"from_entered_datetime, got 'builtins.str'")

@patch('tda.client.datetime.datetime', mockdatetime)
def test_get_orders_by_path_to_not_datetime(self):
with self.assertRaises(ValueError) as cm:
self.client.get_orders_by_path(
ACCOUNT_ID, to_entered_datetime='2020-01-01')
ACCOUNT_ID, to_entered_datetime='2020-01-01')
self.assertEqual(str(cm.exception),
"expected type 'datetime.datetime' for " +
"to_entered_datetime, got 'builtins.str'")
"expected type 'datetime.datetime' for " +
"to_entered_datetime, got 'builtins.str'")

@patch('tda.client.datetime.datetime', mockdatetime)
def test_get_orders_by_path_max_results(self):
Expand Down Expand Up @@ -443,8 +443,8 @@ def test_get_hours_for_multiple_markets_str(self):
Client.Markets.EQUITY,
Client.Markets.BOND], '2020-01-01')
self.assertEqual(str(cm.exception),
"expected type in (datetime.date, datetime.datetime) for " +
"date, got 'builtins.str'")
"expected type in (datetime.date, datetime.datetime) for " +
"date, got 'builtins.str'")

def test_get_hours_for_multiple_markets_unchecked(self):
self.client.set_enforce_enums(False)
Expand Down Expand Up @@ -479,8 +479,8 @@ def test_get_hours_for_single_market_str(self):
self.client.get_hours_for_single_market(
Client.Markets.EQUITY, '2020-01-01')
self.assertEqual(str(cm.exception),
"expected type in (datetime.date, datetime.datetime) for " +
"date, got 'builtins.str'")
"expected type in (datetime.date, datetime.datetime) for " +
"date, got 'builtins.str'")

def test_get_hours_for_single_market_unchecked(self):
self.client.set_enforce_enums(False)
Expand Down Expand Up @@ -626,8 +626,8 @@ def test_get_option_chain_from_date_str(self):
with self.assertRaises(ValueError) as cm:
self.client.get_option_chain('AAPL', strike_from_date='2020-01-01')
self.assertEqual(str(cm.exception),
"expected type in (datetime.date, datetime.datetime) for " +
"strike_from_date, got 'builtins.str'")
"expected type in (datetime.date, datetime.datetime) for " +
"strike_from_date, got 'builtins.str'")

def test_get_option_chain_to_date_datetime(self):
self.client.get_option_chain('AAPL', strike_to_date=NOW_DATETIME)
Expand All @@ -649,8 +649,8 @@ def test_get_option_chain_to_date_str(self):
with self.assertRaises(ValueError) as cm:
self.client.get_option_chain('AAPL', strike_to_date='2020-01-01')
self.assertEqual(str(cm.exception),
"expected type in (datetime.date, datetime.datetime) for " +
"strike_to_date, got 'builtins.str'")
"expected type in (datetime.date, datetime.datetime) for " +
"strike_to_date, got 'builtins.str'")

def test_get_option_chain_volatility(self):
self.client.get_option_chain('AAPL', volatility=40.0)
Expand Down Expand Up @@ -796,8 +796,8 @@ def test_get_price_history_start_datetime_str(self):
with self.assertRaises(ValueError) as cm:
self.client.get_price_history(SYMBOL, start_datetime='2020-01-01')
self.assertEqual(str(cm.exception),
"expected type 'datetime.datetime' for " +
"start_datetime, got 'builtins.str'")
"expected type 'datetime.datetime' for " +
"start_datetime, got 'builtins.str'")

def test_get_price_history_end_datetime(self):
self.client.get_price_history(SYMBOL, end_datetime=EARLIER_DATETIME)
Expand All @@ -810,8 +810,8 @@ def test_get_price_history_end_datetime_str(self):
with self.assertRaises(ValueError) as cm:
self.client.get_price_history(SYMBOL, end_datetime='2020-01-01')
self.assertEqual(str(cm.exception),
"expected type 'datetime.datetime' for " +
"end_datetime, got 'builtins.str'")
"expected type 'datetime.datetime' for " +
"end_datetime, got 'builtins.str'")

def test_get_price_history_need_extended_hours_data(self):
self.client.get_price_history(SYMBOL, need_extended_hours_data=True)
Expand Down Expand Up @@ -896,8 +896,8 @@ def test_get_transactions_start_date_str(self):
with self.assertRaises(ValueError) as cm:
self.client.get_transactions(ACCOUNT_ID, start_date='2020-01-01')
self.assertEqual(str(cm.exception),
"expected type in (datetime.date, datetime.datetime) for " +
"start_date, got 'builtins.str'")
"expected type in (datetime.date, datetime.datetime) for " +
"start_date, got 'builtins.str'")

def test_get_transactions_end_date(self):
self.client.get_transactions(ACCOUNT_ID, end_date=NOW_DATETIME)
Expand All @@ -917,8 +917,8 @@ def test_get_transactions_end_date_str(self):
with self.assertRaises(ValueError) as cm:
self.client.get_transactions(ACCOUNT_ID, end_date='2020-01-01')
self.assertEqual(str(cm.exception),
"expected type in (datetime.date, datetime.datetime) for " +
"end_date, got 'builtins.str'")
"expected type in (datetime.date, datetime.datetime) for " +
"end_date, got 'builtins.str'")

# get_preferences

Expand Down

0 comments on commit ed12244

Please sign in to comment.