Open iiivasyaiii opened 3 months ago
Your question is for StackOverflow.
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?
Your first question is about Pandas & lambdas not yfinance. download accepts a list of symbols.
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_lowerticker'], start=Stocks.index)['Close'].iloc[0])
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`