thanks for your reply. I have a try, but it will spend much time using my code as follow:
sigma=0.01
C=0 ### C denote price using BSM model
upper=0.008
lower=0.006
[S,r,q,T,K,p]=data1.loc[0,[‘S(t)’,’r’,’q’,’DTM’,’X’,’Option Price’]]
tmp1=S*(math.exp((-1)*q*T))
tmp2=S/K
tmp3=(np.log(tmp2)+T*(r-q+(0.5*(sigma**2))))/(sigma*np.sqrt(T))
tmp4=tmp3-np.sqrt(T)*sigma
tmp5=norm.cdf(tmp3,0,1)
tmp6=norm.cdf(tmp4,0,1)
while abs(C-p)>1e-6:
C=tmp1*tmp5-K*math.exp((-1)*r*T)*tmp6
if C-p>0:
upper=sigma
sigma=(sigma+lower)/2
else:
lower=sigma
sigma=(sigma+upper)/2
print(sigma)
so, do you have better way to solve the problem? thanks!!