作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我使用 VBA 和 ExcelPython 引用从 Excel 文件调用 python 脚本。 python 脚本工作正常,除了 Excel 不断告诉我在指定的行上有类型不匹配:
Function calcCapValue(times As Range, fRates As Range, strike As Range, vol As Range, delta As Double, pv As Range) As Variant
Set methods = PyModule("PyFunctions", AddPath:=ThisWorkbook.Path)
Set result = PyCall(methods, "CalculateCapValue", KwArgs:=PyDict("times", times.Value2, "fwdRates", fRates.Value2, "strike", strike.Cells(1, 1).Value2, "flatVol", vol.Cells(1, 1).Value2, "delta", delta, "pv", pv.Cells(1, 1).Value2))
calcCapValue = PyVar(PyGetItem(result, "res")) ' <--- TYPE MISMATCH HERE
Exit Function
End Function
不明白为什么,我正在遵循这里的示例代码:https://sourceforge.net/p/excelpython/wiki/Putting%20it%20all%20together/在这里:http://www.codeproject.com/Articles/639887/Calling-Python-code-from-Excel-with-ExcelPython
仍然出现这种类型不匹配的情况,我不明白为什么。
这是Python脚本:
#imports
import numpy as np
from scipy.stats import norm
#
# Cap Price Calculation
#
def CalculateCapValue(times, fwdRates, strike, flatVol, delta, pv):
capPrice = 0;
#Iterate through each time for caplet price
for i in range(0, len(times)):
ifr = float(fwdRates[i][0])
iti = float(times[i][0])
d1 = (np.log(ifr/strike)+((flatVol**2)*iti)/2)/(flatVol*np.sqrt(iti))
d2 = d1 - (flatVol*np.sqrt(iti))
Nd1 = norm.cdf(d1)
Nd2 = norm.cdf(d2)
capPrice += pv*delta*(ifr*Nd1 - strike*Nd2)
return {"res": capPrice}
谢谢!
最佳答案
Sourceforge 上的 ExcelPython 讨论论坛回答的问题:
http://sourceforge.net/p/excelpython/discussion/general/thread/97f6c1b0/
关于python - ExcelPython返回类型不匹配问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752924/
我是一名优秀的程序员,十分优秀!