微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在代码中获取以下错误以对期权定价

如何解决在代码中获取以下错误以对期权定价

'''
Call Option
[2]:

import numpy as np
import matplotlib.pyplot as plt
import yfinance as yf
[4]:

dfo = yf.Ticker("ARWR")

dfo.options
[7]:

dfo_exp = dfo.option_chain('2020-12-18')
[8]:

dfo_exp.calls
[8]:
contractSymbol  lastTradeDate   strike  lastPrice   bid ask change  percentChange   volume  openInterest    impliedVolatility   inTheMoney  contractSize    currency
0   ARWR201218C00018000 2020-08-24 04:03:42 18.0    25.90   0.0 0.0 0.0 0.0 NaN 3   0.000010    True    REGULAR USD
1   ARWR201218C00020000 2020-09-02 18:28:58 20.0    20.00   0.0 0.0 0.0 0.0 1.0 1   0.000010    True    REGULAR USD
2   ARWR201218C00025000 2020-08-19 19:28:43 25.0    21.87   0.0 0.0 0.0 0.0 50.0    50  0.000010    True    REGULAR USD
3   ARWR201218C00028000 2020-07-07 15:06:32 28.0    20.50   15.4    16.4    0.0 0.0 6.0 0   0.000010    True    REGULAR USD
4   ARWR201218C00030000 2020-07-20 14:31:38 30.0    23.20   16.0    18.2    0.0 0.0 1.0 2   0.748049    True    REGULAR USD
5   ARWR201218C00031000 2020-09-14 14:35:57 31.0    8.40    0.0 0.0 0.0 0.0 2.0 2   0.000010    True    REGULAR USD
6   ARWR201218C00033000 2020-09-14 18:21:53 33.0    7.10    0.0 0.0 0.0 0.0 23.0    43  0.000010    True    REGULAR USD
7   ARWR201218C00034000 2020-09-15 15:07:42 34.0    6.61    0.0 0.0 0.0 0.0 8.0 27  0.000010    True    REGULAR USD
8   ARWR201218C00035000 2020-09-16 15:51:32 35.0    17.57   0.0 0.0 0.0 0.0 12.0    54  0.000010    True    REGULAR USD
9   ARWR201218C00036000 2020-09-14 18:26:38 36.0    5.80    0.0 0.0 0.0 0.0 14.0    15  0.000010    True    REGULAR USD
10  ARWR201218C00037000 2020-09-16 19:07:56 37.0    14.50   0.0 0.0 0.0 0.0 1.0 128 0.000010    True    REGULAR USD
11  ARWR201218C00038000 2020-09-14 14:35:57 38.0    5.00    0.0 0.0 0.0 0.0 73.0    134 0.000010    True    REGULAR USD
12  ARWR201218C00039000 2020-07-24 16:28:33 39.0    14.00   9.1 11.1    0.0 0.0 10.0    11  0.547856    True    REGULAR USD
13  ARWR201218C00040000 2020-09-16 18:40:21 40.0    13.80   0.0 0.0 0.0 0.0 6.0 199 0.000010    True    REGULAR USD
14  ARWR201218C00041000 2020-09-16 15:21:43 41.0    13.15   0.0 0.0 0.0 0.0 3.0 6   0.000010    True    REGULAR USD
15  ARWR201218C00042000 2020-09-16 15:13:11 42.0    13.04   0.0 0.0 0.0 0.0 3.0 44  0.000010    True    REGULAR USD
16  ARWR201218C00043000 2020-09-11 17:42:00 43.0    3.00    0.0 0.0 0.0 0.0 5.0 11  0.000010    True    REGULAR USD
17  ARWR201218C00044000 2020-09-16 14:56:14 44.0    10.00   0.0 0.0 0.0 0.0 31.0    146 0.000010    True    REGULAR USD
18  ARWR201218C00045000 2020-09-16 14:58:25 45.0    10.40   0.0 0.0 0.0 0.0 6.0 133 0.000010    True    REGULAR USD
19  ARWR201218C00050000 2020-09-16 17:23:56 50.0    8.10    0.0 0.0 0.0 0.0 84.0    348 0.031260    False   REGULAR USD
20  ARWR201218C00055000 2020-09-16 17:03:51 55.0    6.80    0.0 0.0 0.0 0.0 269.0   659 0.062509    False   REGULAR USD
21  ARWR201218C00060000 2020-09-16 19:16:11 60.0    4.00    0.0 0.0 0.0 0.0 71.0    1872    0.125009    False   REGULAR USD
22  ARWR201218C00065000 2020-09-16 15:33:56 65.0    3.80    0.0 0.0 0.0 0.0 39.0    222 0.125009    False   REGULAR USD
23  ARWR201218C00070000 2020-09-16 14:27:51 70.0    3.00    0.0 0.0 0.0 0.0 18.0    40  0.125009    False   REGULAR USD
24  ARWR201218C00075000 2020-09-16 16:55:12 75.0    3.00    0.0 0.0 0.0 0.0 19.0    39  0.250007    False   REGULAR USD
[10]:

df = yf.download("ARWR")
[*********************100%***********************]  1 of 1 completed
[11]:

df.head()
[11]:
Open    High    Low Close   Adj Close   Volume
Date                        
1993-12-16  2925.0  2925.0  2925.0  2925.0  2925.0  0
1993-12-17  2762.5  2762.5  2762.5  2762.5  2762.5  0
1993-12-20  2762.5  2762.5  2762.5  2762.5  2762.5  0
1993-12-21  2762.5  2762.5  2762.5  2762.5  2762.5  0
1993-12-22  2762.5  2762.5  2762.5  2762.5  2762.5  0
[12]:

df.tail()
[12]:
Open    High    Low Close   Adj Close   Volume
Date                        
2020-09-10  36.319000   37.805000   34.900002   35.200001   35.200001   1269300
2020-09-11  35.529999   35.660000   32.860001   33.209999   33.209999   1904100
2020-09-14  33.860001   34.680000   33.220001   34.040001   34.040001   1363700
2020-09-15  34.480000   34.959999   33.610001   33.799999   33.799999   1034400
2020-09-16  40.049999   53.119999   40.049999   47.430000   47.430000   23998700
[13]:

df['Adj Close'][-1]
[13]:
47.43000030517578
[14]:

spot_price = df['Adj Close'][-1] # current price
share_price = np.arange(0.9*spot_price,1.1*spot_price)
strike_price = dfo_exp.calls['strike'],50 # exercise price of an options that is fixed price
call_price = dfo_exp.calls['lastPrice'],8.10 # price of an option or premium 
[15]:

def call_option(share_price,strike_price,call_price):
    pnl = np.where(share_price > strike_price,share_price - strike_price,0)  
    return pnl - call_price
[16]:

payoff_long_call = call_option(share_price,call_price)
# Plot the graph
plt.subplots(figsize=(16,8))
plt.gca().spines['bottom'].set_position('zero')
plt.plot(share_price,payoff_long_call,label='Call option buyer payoff',color='g')
plt.xlabel('Range Stock Price')
plt.ylabel('Profit and loss')
plt.grid(which='both')
plt.legend()
plt.show()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-50bdca645437> in <module>
----> 1 payoff_long_call = call_option(share_price,call_price)
      2 # Plot the graph
      3 plt.subplots(figsize=(16,8))
      4 plt.gca().spines['bottom'].set_position('zero')
      5 plt.plot(share_price,color='g')

<ipython-input-15-1db3531586cb> in call_option(share_price,call_price)
      1 def call_option(share_price,call_price):
----> 2     pnl = np.where(share_price > strike_price,0)
      3     return pnl - call_price

ValueError: operands Could not be broadcast together with shapes (10,) (2,) 
[17]:

payoff_short_call = payoff_long_call * -1.0
# Plot
plt.subplots(figsize=(16,payoff_short_call,label='Short 240 Strike Call',color='r')
plt.xlabel('Range Stock Price')
plt.ylabel('Profit and loss')
plt.grid(which='both')
plt.legend()
plt.show()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-17-862460f48f4d> in <module>
----> 1 payoff_short_call = payoff_long_call * -1.0
      2 # Plot
      3 plt.subplots(figsize=(16,color='r')

NameError: name 'payoff_long_call' is not defined
[ ]:

​
0
2
Python 3 | Idle
Options_Call.ipynb
Ln 1,Col 1
'''

在运行代码时遇到上述错误,并且看不到我的错误在哪里,我认为这是由于我输入了输入。我在git hub上找到了代码,并对其进行了修改,但是我对python来说还很陌生,所以我有点挣扎。如果需要,我可以提供整个文件代码的后两部分有两个错误

任何帮助将不胜感激。

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。