Skip to content

Commit

Permalink
Deprecate strike_from_date and strike_to_date (#150)
Browse files Browse the repository at this point in the history
* Deprecate strike_from_date and strike_to_date

* Update documentation formatting

* Ran make fix
  • Loading branch information
alexgolec authored Jan 20, 2021
1 parent 9b57c3b commit 8d862d7
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 22 deletions.
47 changes: 33 additions & 14 deletions tda/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pickle
import tda
import time
import warnings

from tda.orders.generic import OrderBuilder
from ..utils import EnumEnforcer
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
49 changes: 41 additions & 8 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit 8d862d7

Please sign in to comment.