Posts: 9
Threads: 2
Joined: May 2020
May-27-2020, 05:27 AM
(This post was last modified: May-27-2020, 05:33 AM by buran.)
Hi,
I'm trying to learn Python and is only my second day learning the code. At the moment, I'm trying to use the Mibian library to do some simple options pricing.
I've written this simple code to do some calculations:
import mibian
def BSModel():
c = mibian.BS([40.75, 32, 1, 127, 30], putPrice=0.95)
print("Implied vol is ",c.impliedVolatility)
print("Delta is ",c.putDelta)
print("Theta is ",c.putTheta)
print("Vega is ",c.vega)
print("Gamma is ",c.gamma) The results from the above are as below:
Output: BSModel()
Implied vol is 45.654296875
Delta is None
Theta is None
Vega is None
Gamma is None
Strangely, it's only showing the calculation for the implied volatility but the rest is returning none. I've tried changing the input data but it's doing the same.
I feel like I'm made some newbie error somewhere. Appreciate any feedback. Thanks.
Posts: 8,159
Threads: 160
Joined: Sep 2016
you don't pass argument for volatility . So default value is None
It needs volatility [to be evaluated True] to calculate rest
Posts: 9
Threads: 2
Joined: May 2020
(May-27-2020, 05:40 AM)buran Wrote: you don't pass argument for volatility . So default value is None
It needs volatility [to be evaluated True] to calculate rest
Duh! You're right. I should pass the volatility back into the function.
I've rewritten the code as such:
def BSModel():
c = mibian.BS([40.75, 32, 1, 127, 30], putPrice=0.95)
impvol = c.impliedVolatility
print("Implied vol is ",impvol)
c = mibian.BS([40.75, 32, 1, 127, 30], volatility =impvol, putPrice=0.95)
print("Delta is ",c.putDelta)
print("Theta is ",c.putTheta)
print("Vega is ",c.vega)
print("Gamma is ",c.gamma) Is that the best way to write the code or is there a more elegant way?
Posts: 8,159
Threads: 160
Joined: Sep 2016
May-27-2020, 07:35 AM
(This post was last modified: May-27-2020, 07:39 AM by buran.)
shouldn't you using historical volatility, not implied volatility?
looking further at the source code. if volatility == 0 , the if volatility: will be False and the rest of the block will not be executed and the greeks will be None. However if you look at internal methods like _delta() , _delta2() etc., some of them have if self.volatility == 0 or self.daysToExpiration == 0 . So there is problem - there is special handling of case when volatility == 0, yet, it never get executed.
Posts: 9
Threads: 2
Joined: May 2020
(May-27-2020, 07:35 AM)buran Wrote: shouldn't you using historical volatility, not implied volatility?
Both volatilities can be use, depending on what the user is trying to achieve. In my case, I want to know what's the implied volatility (I guess you can say what market thinks volatility will be going forward) and corresponding Greeks (delta, theta, gamma etc) based on where the market is trading the options at. One can also chose to feed in the historical volatility to get the option price if they think historical volatility is a fair predictor of the value of the option, etc.
Quote:looking further at the source code. if volatility == 0 , the if volatility: will be False and the rest of the block will not be executed and the greeks will be None. However if you look at internal methods like _delta() , _delta2() etc., some of them have if self.volatility == 0 or self.daysToExpiration == 0 . So there is problem - there is special handling of case when volatility == 0, yet, it never get executed.
OK, if I understand it correctly, I should add error handling to capture for incorrect input. Is that right?
Posts: 8,159
Threads: 160
Joined: Sep 2016
(May-27-2020, 07:58 AM)pwt Wrote: OK, if I understand it correctly, I should add error handling to capture for incorrect input. Is that right? It depend what you want - as it is now, you will get the same result if volatility is 0 or you don't supply argument for volatility at all. So, unless you change the code there will be no error, just be aware that volatility=0 will yield None for greeks.
|