所以我要使用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/