首页 > 解决方案 > 尝试创建看涨期权收益配置文件但不断收到此错误

问题描述

我正在尝试在 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))

标签: pythonpython-3.xnumpymatplotlibyfinance

解决方案


As @hpaulj stated, you cannot subtract strings! When I prompted the user to input a strike price, it saved the input as a string, I simply changed this to:

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

推荐阅读