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

Calculating the yf.download function in a pandas column #2033

Open
iiivasyaiii opened this issue Aug 22, 2024 · 3 comments
Open

Calculating the yf.download function in a pandas column #2033

iiivasyaiii opened this issue Aug 22, 2024 · 3 comments

Comments

@iiivasyaiii
Copy link

Hello!
The script downloads stock quotes from Yahoo Finance (module yfinance). Then it downloads the quarterly option chain. Then the script calculates and adds the lower and upper strike columns and the lower and upper option ticker columns. In the penultimate line, the script adds the option Close price column from the 'Call_lower_ticker' column to the date from Stocks.index (using the yf.download function).
Everything works correctly until the penultimate line: Stocks['Call_lower_price'] = Stocks['Call_lower_ticker'].apply(yf.download(['Call_lower_ticker'], start=Stocks.index)['Close'].iloc[0])

  1. How do I make the function work?
  2. And is it possible to download historical data for yf.option.chain?

Code:
`%%time
import yfinance as yf
import pandas as pd
import warnings
import datetime
warnings.filterwarnings("ignore", message="The 'unit' keyword in TimedeltaIndex construction is deprecated and will be removed in a future version. Use pd.to_timedelta instead.", category=FutureWarning, module="yfinance.utils")

Stocks=yf.download('SPY', period="1y", interval="1d", group_by='ticker')

stock = yf.Ticker('SPY')
Expirations = stock.options
options_chain = stock.option_chain('2024-09-20')
Calls_desk=options_chain.calls

Stocks['110_for_Call'] = Stocks['Close']*1.1
Stocks['Call_lower_strike'] = Stocks['110_for_Call'].apply(lambda x: Calls_desk.iloc[Calls_desk[Calls_desk['strike'] < x]['strike'].idxmax()]['strike'])
Stocks['Call_upper_strike'] = Stocks['110_for_Call'].apply(lambda x: Calls_desk.iloc[Calls_desk[Calls_desk['strike'] > x]['strike'].idxmin()]['strike'])
Stocks['Call_lower_ticker'] = Stocks['110_for_Call'].apply(lambda x: Calls_desk.iloc[Calls_desk[Calls_desk['strike'] < x]['strike'].idxmax()]['contractSymbol'])
Stocks['Call_upper_ticker'] = Stocks['110_for_Call'].apply(lambda x: Calls_desk.iloc[Calls_desk[Calls_desk['strike'] > x]['strike'].idxmin()]['contractSymbol'])
Stocks['Call_lower_price'] = Stocks['Call_lower_ticker'].apply(yf.download(['Call_lower_ticker'], start=Stocks.index)['Close'].iloc[0])
Stocks`

Error:
`[100%%] 1 of 1 completed
[100%%
] 1 of 1 completed

1 Failed download:
['CALL_LOWER_TICKER']: ValueError('The truth value of a DatetimeIndex is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().')

IndexError Traceback (most recent call last)
File :19

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexing.py:1191, in _LocationIndexer.getitem(self, key)
1189 maybe_callable = com.apply_if_callable(key, self.obj)
1190 maybe_callable = self._check_deprecated_callable_usage(key, maybe_callable)
-> 1191 return self._getitem_axis(maybe_callable, axis=axis)

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexing.py:1752, in _iLocIndexer._getitem_axis(self, key, axis)
1749 raise TypeError("Cannot index by location index with a non-integer key")
1751 # validate the location
-> 1752 self._validate_integer(key, axis)
1754 return self.obj._ixs(key, axis=axis)

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexing.py:1685, in _iLocIndexer._validate_integer(self, key, axis)
1683 len_axis = len(self.obj._get_axis(axis))
1684 if key >= len_axis or key < -len_axis:
-> 1685 raise IndexError("single positional indexer is out-of-bounds")

IndexError: single positional indexer is out-of-bounds`

@ValueRaider
Copy link
Collaborator

Your question is for StackOverflow.

@ValueRaider ValueRaider closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2024
@iiivasyaiii
Copy link
Author

I will ask this question there. But it seems to me that this is related to the behavior of the yf.download function. I do not understand in what format the parameters should be passed to yf.download?

And the second question: is it possible to download historical data for yf.option.chain?

@ValueRaider
Copy link
Collaborator

ValueRaider commented Aug 22, 2024

Your first question is about Pandas & lambdas not yfinance. download accepts a list of symbols.

@ValueRaider ValueRaider reopened this Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants