[英]Why can't Python find the second function in my module?
極端Python新手在這里。 我正在嘗試為LibreOffice Calc的Black-Scholes選項/希臘公式編寫一些函數。 我想制作一個具有各種功能的大模塊,需要在一些電子表格中使用。 我將它們保存在一個名為TradingUtilities.py的文件中。 前兩個函數如下所示:
def BSCall(S, K, r, sig, T):
import scipy.stats as sp
import numpy as np
d1 = (np.log(S/K) - (r - 0.5 * sig * sig) * T)/(sig*np.sqrt(T))
d2 = d1 - sig*np.sqrt(T)
P1 = sp.norm.cdf(d1)
P2 = sp.norm.cdf(d2);
call = S*P1 - K * np.exp(-r*T)*P2;
return call
def BSPUt(S, K, r, sig, T):
import scipy.stats as sp
import numpy as np
d1 = (np.log(S/K) - (r-0.5*sig*sig)*T)/(sig*np.sqrt(T))
d2 = d1 - sig*np.sqrt(T)
P1 = sp.norm.cdf(-d1)
P2 = sp.norm.cdf(-d2)
put = K*exp(-r*t)*P2 - S*P1
return put
當我從命令行運行腳本時,第一個功能運行良好。 但是當我嘗試運行第二個時,出現以下錯誤,
>>> import TradingUtilities as tp
>>> tp.BSCall(238, 238.5, 0.05, 0.09, 0.09)
2.9860730330243541
>>> tp.BSPut(238, 238, 0.05, 0.09, 0.09)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'TradingUtilities' has no attribute 'BSPut'
我正在嘗試找出問題所在,但到目前為止還沒有運氣。 如果有人能看出我做錯了什么,或指出正確的方向,我將不勝感激。
謝謝!
您的代碼中有錯別字
>>> tp.BSPut(238, 238, 0.05, 0.09, 0.09)
應該
>>> tp.BSPUt(238, 238, 0.05, 0.09, 0.09)
或者,您可以在主代碼BSPUt
BSPut
更改為BSPut
。
與您在命令行中編寫的內容相比,您的函數名稱中有錯別字。 Python區分大小寫。
它應該是: def BSPut(S, K, r, sig, T):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.