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

尝试创建看涨期权收益配置文件,但不断收到此错误

如何解决尝试创建看涨期权收益配置文件,但不断收到此错误

我正在尝试在 Python 中创建一个收益配置文件,但不断得到:

"ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U32'),dtype('<U32')) -> dtype('<U32')" 

当我声明变量 callPayoff 时。这是我的代码如下:

import pandas as pd
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt

pd.options.mode.chained_assignment = None
tickers = input("Enter Ticker in CAPS: ")
print (tickers)

inTradata = yf.download(tickers = tickers,period = '7d',interval = '1m')

curPrice = inTradata.iloc[-1,inTradata.columns.get_loc("Close")]
strikePrice = input("What is your Strike Price?: ")

lowerbound = curPrice * 0.8
upperbound = curPrice * 1.2

curPrice_PP = np.arange(lowerbound,upperbound,0.01)

#use a lambda for a call payoff function:
callPayoff = lambda curPrice,strikePrice: np.maximum(curPrice_PP - strikePrice,0)

#use a lambda for a put payoff function
putPayoff = lambda curPrice,strikePrice: np.maximum(strikePrice - curPrice_PP,0)

#plot the call payoff
plt.figure(1)
plt.title('Call Option Payoff at Expiration')
plt.xlabel("Underlying stock price")
plt.ylabel("Price of Option at Expiration")
plt.plot(curPrice_PP,callPayoff(curPrice_PP,strikePrice))

解决方法

这个更简单的示例是否让您了解错误发生的位置和原因?

In [261]: np.arange(10)-'astromg'
Traceback (most recent call last):
  File "<ipython-input-261-b7b809d0f016>",line 1,in <module>
    np.arange(10)-'astromg'
UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U21'),dtype('<U21')) -> dtype('<U21')
,

正如@hpaulj 所说,你不能减去字符串!当我提示用户输入执行价格时,它会将输入保存为字符串,我只是将其更改为:

strikePrice = int(input("What is your Strike Price?: "))

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?