python - 尝试创建看涨期权收益配置文件但不断收到此错误
问题描述
我正在尝试在 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))
解决方案
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?: "))
推荐阅读
- string - ARM 程序集取消引用字符串仅检索 4 个字节
- javascript - 未定义函数javascript?
- javascript - 导航菜单 JQuery 问题
- ansible - Ansible 循环直到条件匹配。
- python - 如何让堆栈跟踪显示在日志中
- linq - 如何添加 where 条件列出?
- python - 美丽的汤从表中提取数据
- ios - DJIFlightControllerState - 硬件时间戳
- libtcod - 即使包含在 MacOS Mojave 上的 Xcode 上也找不到 libtcod.hpp?
- python - pyparsing:忽略任何不匹配的标记