在带有设置参数的矩阵中查找特定元素

发布于 2025-01-28 07:01:15 字数 1757 浏览 3 评论 0原文

我为财务选项创建了一个代码,并且要完成它的最后一件事。我希望我的代码找到特定选项的隐含波动率。我的代码要求用户输入股票的股票股票,到期日期,罢工价格以及该选项是否为呼叫或看台。

这是我想要做的:

  1. 根据它是通话还是put,寻找合适的 在终端中给出的矩阵。
  2. 提取有条件的暗示波动率值(我可以在选项中处理日期和股票。 用户输入的
  3. 返回隐含波动率

我该如何处理?我查看了许多模块(熊猫,熊猫datareader,基本的python函数,numpy,sys等)及其文档,似乎没有什么适合我想要的。这是我从我想要的数据中获得的输出的示例。

from yahoo_fin import options

chain = options.get_options_chain("nflx", "2022-06-10")
print(chain)

$ python implied
{'calls':           Contract Name         Last Trade Date  Strike  Last Price    Bid    Ask  Change % Change Volume  Open Interest Implied Volatility
0   NFLX220610C00120000  2022-05-04 12:27PM EDT   120.0       75.50  46.50  49.60    0.00        -      -              0             87.26%
1   NFLX220610C00130000  2022-05-09 10:25AM EDT   130.0       50.05   0.00   0.00    0.00        -      -              0              0.00%
2   NFLX220610C00135000   2022-05-05 1:50PM EDT   135.0       55.75  34.50  35.60    0.00        -      -              1             81.37%
3   NFLX220610C00145000  2022-05-10 10:04AM EDT   145.0       36.77   0.00   0.00    0.00        -      -              0              0.00%

'puts':           Contract Name         Last Trade Date  Strike  Last Price    Bid    Ask  Change % Change Volume Open Interest Implied Volatility
0   NFLX220610P00100000   2022-05-11 3:59PM EDT   100.0        0.59   0.56   0.66    0.19  +47.50%     83            59            100.05%
1   NFLX220610P00105000  2022-05-11 11:43AM EDT   105.0        0.40   0.71   0.86   -0.20  -33.33%      2             1             96.48%
2   NFLX220610P00110000   2022-05-11 3:22PM EDT   110.0        0.97   0.96   1.10    0.38  +64.41%     35             6             93.51%

I created a code for financial options and there is one last thing missing to finish it. I want my code to find the implied volatility for a specific option. My code asks the user to input the stock's ticker, date to maturity, strike price, and if the option is a call or a put.

Here is what I would like it to do:

  1. Based on whether it is a call or a put, look for the suitable
    matrix that is given in the terminal.
  2. Extract the implied volatility value conditional to the strike price (I can already handle the date and ticker in the options.get_options_chain function)
    the user inputted
  3. Return the implied volatility

How should I go about this? I looked at many modules (pandas, pandas-DataReader, basic Python functions, NumPy, sys, etc.) and their documentation, and nothing seems to fit what I want. Here is an example of the output that I am getting from the data that I want.

from yahoo_fin import options

chain = options.get_options_chain("nflx", "2022-06-10")
print(chain)

$ python implied
{'calls':           Contract Name         Last Trade Date  Strike  Last Price    Bid    Ask  Change % Change Volume  Open Interest Implied Volatility
0   NFLX220610C00120000  2022-05-04 12:27PM EDT   120.0       75.50  46.50  49.60    0.00        -      -              0             87.26%
1   NFLX220610C00130000  2022-05-09 10:25AM EDT   130.0       50.05   0.00   0.00    0.00        -      -              0              0.00%
2   NFLX220610C00135000   2022-05-05 1:50PM EDT   135.0       55.75  34.50  35.60    0.00        -      -              1             81.37%
3   NFLX220610C00145000  2022-05-10 10:04AM EDT   145.0       36.77   0.00   0.00    0.00        -      -              0              0.00%

'puts':           Contract Name         Last Trade Date  Strike  Last Price    Bid    Ask  Change % Change Volume Open Interest Implied Volatility
0   NFLX220610P00100000   2022-05-11 3:59PM EDT   100.0        0.59   0.56   0.66    0.19  +47.50%     83            59            100.05%
1   NFLX220610P00105000  2022-05-11 11:43AM EDT   105.0        0.40   0.71   0.86   -0.20  -33.33%      2             1             96.48%
2   NFLX220610P00110000   2022-05-11 3:22PM EDT   110.0        0.97   0.96   1.10    0.38  +64.41%     35             6             93.51%

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一念一轮回 2025-02-04 07:01:15

您获得的结果集是用键或putspd.dataframe作为值的键。

首先访问DICT,然后通过选择strike(右行)和隐含波动率(右列)来选择数据框中所需的数据。

使用ILOC [0]您将在pd.Series中获得第一个元素。

from yahoo_fin import options
import pandas as pd

# set basic parameters e. g., maturity, option type
option_type = 'calls'
strike = 180.0
last_trade_date = '2022-05-10'

chain = options.get_options_chain("nflx", "2022-06-10")
print(chain)

df = pd.DataFrame(chain.get(option_type))
vola = df[df['Strike'] == strike]['Implied Volatility'].iloc[0]

输出:

3.13%

The result set you get is a dict with the keys calls or puts and a pd.DataFrame as a value.

Access the dict first, then select the required data in the DataFrame by selecting the strike (right row) and Implied Volatility (right column).

With iloc[0] you will get the first element within the pd.Series.

from yahoo_fin import options
import pandas as pd

# set basic parameters e. g., maturity, option type
option_type = 'calls'
strike = 180.0
last_trade_date = '2022-05-10'

chain = options.get_options_chain("nflx", "2022-06-10")
print(chain)

df = pd.DataFrame(chain.get(option_type))
vola = df[df['Strike'] == strike]['Implied Volatility'].iloc[0]

Output:

3.13%
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文