ExcelPython 返回类型不匹配问题
我在用VBA从Excel文件调用一个Python脚本,配合ExcelPython这个工具。这个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}
谢谢!
1 个回答
2
在Sourceforge的ExcelPython讨论论坛上回答了这个问题:
http://sourceforge.net/p/excelpython/discussion/general/thread/97f6c1b0/