Finance: Black Scholes Model not working - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Finance: Black Scholes Model not working (/thread-27127.html) |
Finance: Black Scholes Model not working - pwt - May-27-2020 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: 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. RE: Finance: Black Scholes Model not working - buran - May-27-2020 you don't pass argument for volatility . So default value is None It needs volatility [to be evaluated True] to calculate rest
RE: Finance: Black Scholes Model not working - pwt - May-27-2020 (May-27-2020, 05:40 AM)buran Wrote: you don't pass argument for 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? RE: Finance: Black Scholes Model not working - buran - May-27-2020 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.
RE: Finance: Black Scholes Model not working - pwt - May-27-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 OK, if I understand it correctly, I should add error handling to capture for incorrect input. Is that right? RE: Finance: Black Scholes Model not working - buran - May-27-2020 (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. |