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

option_chain() return underlying data that comes with the options data #1606

Merged
merged 1 commit into from
Jul 27, 2023
Merged

option_chain() return underlying data that comes with the options data #1606

merged 1 commit into from
Jul 27, 2023

Conversation

DanielGoldfarb
Copy link
Contributor

@DanielGoldfarb DanielGoldfarb commented Jul 14, 2023

options_chain() return underlying data that comes with the options data. this is helpful to ensure that underlying bid,ask,trade and options bid,ask,trade are in sync, plus avoids an additional call since the data comes from the options chain request anyway.

@ValueRaider
Copy link
Collaborator

ValueRaider commented Jul 14, 2023

I don't use options so can't review. Maybe these can? @thezenvan @GF-Huang @yukunchen113 @gracekk

@DanielGoldfarb DanielGoldfarb changed the title options_chain() return underlying data that comes with the options data option_chain() return underlying data that comes with the options data Jul 14, 2023
@DanielGoldfarb
Copy link
Contributor Author

I am happy for anyone to review this; note that while someone who knows options may well understand some of the motivation behind this, in fact anyone can review the code and note the following:

  1. I have implemented this in such a way that it should have no effect on anyone who is not interested in this data: The pre-existing parts of the namedtuple returned by method option_chain() (i.e. Options.call and Options.put) remain unchanged; there is now an additional attribute Options.underlying which contains the dict of underlying information that always gets returned with an options request().
  2. I have tweaked the output from the internal method _download_options() to always return a dict. Previously, when data was available it would return a dict, otherwise it would return an empty list or None, depending which code path was taken.

That's the extent of this code change. Thanks!

Copy link
Contributor

@ricardoprins ricardoprins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

It gives some more useful info in a non-clunky way. I'd only note the change in return type in _download_options(), but I don't think it yields any negative impact.

@ValueRaider
Copy link
Collaborator

ValueRaider commented Jul 27, 2023

I'd only note the change in return type in _download_options()

Why? I misread, thought you were suggesting changing type.

Otherwise, I've quickly looked at code, is a simple change.

@DanielGoldfarb
Copy link
Contributor Author

DanielGoldfarb commented Jul 27, 2023

  1. I have tweaked the output from the internal method _download_options() to always return a dict. Previously, when data was available it would return a dict, otherwise it would return an empty list or None, depending which code path was taken.

I realize now that I explained what I did but not why. _download_options() was inconsistent in its return types, as mentioned in the quote above. It is an internal function to the class, and as far as I can tell it is called in only the one location. In either case, if it has no data to return, then an exception will be thrown when option_chain() attempts index into the returned value options, but at least now the returned type will be the same in all cases, and so the exception will be the same in all cases.

Alternatively I can now take this a step further. since the returned type will always be a dict, I can modify option_chain to handle the empty dict by returning empty dataframes instead of raising a KeyError when 'calls' is not in the dict.

At any rate, I thought it best for _download_options() to be consistent in the type that it returned.

@ValueRaider
Copy link
Collaborator

@DanielGoldfarb Up to you. I'm OK merging this as is.

@DanielGoldfarb
Copy link
Contributor Author

I would like to merge this as is, since this does not change the behavior of an "client facing" methods, and I would prefer to leave it at that. Thanks for reviewing!

@ValueRaider ValueRaider merged commit d528296 into ranaroussi:dev Jul 27, 2023
@ricardoprins
Copy link
Contributor

  1. I have tweaked the output from the internal method _download_options() to always return a dict. Previously, when data was available it would return a dict, otherwise it would return an empty list or None, depending which code path was taken.

I realize now that I explained what I did but not why. _download_options() was inconsistent in its return types, as mentioned in the quote above. It is an internal function to the class, and as far as I can tell it is called in only the one location. In either case, if it has no data to return, then an exception will be thrown when option_chain() attempts index into the returned value options, but at least now the returned type will be the same in all cases, and so the exception will be the same in all cases.

Alternatively I can now take this a step further. since the returned type will always be a dict, I can modify option_chain to handle the empty dict by returning empty dataframes instead of raising a KeyError when 'calls' is not in the dict.

At any rate, I thought it best for _download_options() to be consistent in the type that it returned.

I agree 100% with your reasoning here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants