diff --git a/tda/client/base.py b/tda/client/base.py index 2ab035e..09cef2d 100644 --- a/tda/client/base.py +++ b/tda/client/base.py @@ -11,6 +11,7 @@ import pickle import tda import time +import warnings from tda.orders.generic import OrderBuilder from ..utils import EnumEnforcer @@ -581,6 +582,8 @@ def get_option_chain( strike_range=None, strike_from_date=None, strike_to_date=None, + from_date=None, + to_date=None, volatility=None, underlying_price=None, interest_rate=None, @@ -604,14 +607,14 @@ def get_option_chain( :param strike: Return options only at this strike price. :param strike_range: Return options for the given range. See :class:`Options.StrikeRange` for choices. - :param strike_from_date: Only return expirations after this date. For - strategies, expiration refers to the nearest - term expiration in the strategy. Accepts - ``datetime.date`` and ``datetime.datetime``. - :param strike_to_date: Only return expirations before this date. For - strategies, expiration refers to the nearest - term expiration in the strategy. Accepts - ``datetime.date`` and ``datetime.datetime``. + :param from_date: Only return expirations after this date. For + strategies, expiration refers to the nearest term + expiration in the strategy. Accepts ``datetime.date`` + and ``datetime.datetime``. + :param to_date: Only return expirations before this date. For + strategies, expiration refers to the nearest term + expiration in the strategy. Accepts ``datetime.date`` + and ``datetime.datetime``. :param volatility: Volatility to use in calculations. Applies only to ``ANALYTICAL`` strategy chains. :param underlying_price: Underlying price to use in calculations. @@ -626,6 +629,24 @@ def get_option_chain( :param option_type: Types of options to return. See :class:`Options.Type` for choices. ''' + if strike_from_date: + warnings.warn( + 'The strike_from_date argument is deprecated and will be ' + + 'removed in a future version of tda-api. Please use ' + + 'from_date instead.', Warning) + assert from_date is None, \ + 'strike_from_date and from_date cannot be set simultaneously' + from_date = strike_from_date + + if strike_to_date: + warnings.warn( + 'The strike_to_date argument is deprecated and will be ' + + 'removed in a future version of tda-api. Please use ' + + 'to_date instead.', Warning) + assert to_date is None, \ + 'strike_to_date and to_date cannot be set simultaneously' + to_date = strike_to_date + contract_type = self.convert_enum( contract_type, self.Options.ContractType) strategy = self.convert_enum(strategy, self.Options.Strategy) @@ -653,12 +674,10 @@ def get_option_chain( params['strike'] = strike if strike_range is not None: params['range'] = strike_range - if strike_from_date is not None: - params['fromDate'] = self._format_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) + if from_date is not None: + params['fromDate'] = self._format_date('from_date', from_date) + if to_date is not None: + params['toDate'] = self._format_date('to_date', to_date) if volatility is not None: params['volatility'] = volatility if underlying_price is not None: diff --git a/tests/test_client.py b/tests/test_client.py index 0b3a0f0..e64e621 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -851,7 +851,7 @@ def test_get_option_chain_strike_range_unchecked(self): def test_get_option_chain_from_date_datetime(self): self.client.get_option_chain( - 'AAPL', strike_from_date=NOW_DATETIME) + 'AAPL', from_date=NOW_DATETIME) self.mock_session.get.assert_called_once_with( self.make_url('/v1/marketdata/chains'), params={ 'apikey': API_KEY, @@ -860,7 +860,7 @@ def test_get_option_chain_from_date_datetime(self): def test_get_option_chain_from_date_date(self): - self.client.get_option_chain('AAPL', strike_from_date=NOW_DATE) + self.client.get_option_chain('AAPL', from_date=NOW_DATE) self.mock_session.get.assert_called_once_with( self.make_url('/v1/marketdata/chains'), params={ 'apikey': API_KEY, @@ -870,14 +870,30 @@ def test_get_option_chain_from_date_date(self): 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.client.get_option_chain('AAPL', from_date='2020-01-01') self.assertEqual(str(cm.exception), "expected type in (datetime.date, datetime.datetime) for " + - "strike_from_date, got 'builtins.str'") + "from_date, got 'builtins.str'") + def test_get_option_chain_strike_from_date_date(self): + self.client.get_option_chain('AAPL', strike_from_date=NOW_DATE) + self.mock_session.get.assert_called_once_with( + self.make_url('/v1/marketdata/chains'), params={ + 'apikey': API_KEY, + 'symbol': 'AAPL', + 'fromDate': NOW_DATE_ISO}) + + def test_get_option_chain_strike_from_date_and_from_date(self): + with self.assertRaises(AssertionError) as err: + self.client.get_option_chain( + 'AAPL', strike_from_date=NOW_DATE, from_date=NOW_DATE) + self.assertEqual(str(err.exception), + 'strike_from_date and from_date cannot be set simultaneously') + + def test_get_option_chain_to_date_datetime(self): - self.client.get_option_chain('AAPL', strike_to_date=NOW_DATETIME) + self.client.get_option_chain('AAPL', to_date=NOW_DATETIME) self.mock_session.get.assert_called_once_with( self.make_url('/v1/marketdata/chains'), params={ 'apikey': API_KEY, @@ -886,7 +902,7 @@ def test_get_option_chain_to_date_datetime(self): def test_get_option_chain_to_date_date(self): - self.client.get_option_chain('AAPL', strike_to_date=NOW_DATE) + self.client.get_option_chain('AAPL', to_date=NOW_DATE) self.mock_session.get.assert_called_once_with( self.make_url('/v1/marketdata/chains'), params={ 'apikey': API_KEY, @@ -896,12 +912,29 @@ def test_get_option_chain_to_date_date(self): 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.client.get_option_chain('AAPL', to_date='2020-01-01') self.assertEqual(str(cm.exception), "expected type in (datetime.date, datetime.datetime) for " + - "strike_to_date, got 'builtins.str'") + "to_date, got 'builtins.str'") + def test_get_option_chain_strike_to_date_date(self): + self.client.get_option_chain('AAPL', strike_to_date=NOW_DATE) + self.mock_session.get.assert_called_once_with( + self.make_url('/v1/marketdata/chains'), params={ + 'apikey': API_KEY, + 'symbol': 'AAPL', + 'toDate': NOW_DATE_ISO}) + + + def test_get_option_chain_strike_to_date_and_to_date(self): + with self.assertRaises(AssertionError) as err: + self.client.get_option_chain( + 'AAPL', strike_to_date=NOW_DATE, to_date=NOW_DATE) + self.assertEqual(str(err.exception), + 'strike_to_date and to_date cannot be set simultaneously') + + def test_get_option_chain_volatility(self): self.client.get_option_chain('AAPL', volatility=40.0) self.mock_session.get.assert_called_once_with(