Python Forum
Finance: Black Scholes Model not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finance: Black Scholes Model not working
#1
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.
Reply
#2
you don't pass argument for volatility. So default value is None
It needs volatility [to be evaluated True] to calculate rest
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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?
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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?
Reply
#6
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  model.fit and model.predict errors hatflyer 6 1,191 Nov-10-2023, 01:39 AM
Last Post: hatflyer
  Python help for yahoo finance! Zshanar 7 3,297 Jul-31-2022, 05:27 PM
Last Post: paulyan
  remove all color but red, then replace it with black kucingkembar 14 6,917 Dec-29-2021, 07:50 PM
Last Post: deanhystad
  FileNotFoundError: [Errno 2] No such file or directory: 'model/doc2vec.model/Articles Anldra12 10 5,679 Jun-11-2021, 04:48 PM
Last Post: snippsat
  Checkbuttons always come up as black boxes regardless of the state kenwatts275 5 4,620 Jul-07-2020, 08:00 PM
Last Post: kenwatts275
  How to use nb-black python cde formatter ErnestTBass 3 6,747 Jun-04-2020, 03:51 PM
Last Post: ErnestTBass
  after using openpyxl to add colors to script, black shows up white online in excel Soundtechscott 1 3,651 Jun-08-2019, 10:33 PM
Last Post: Soundtechscott
  Because the emoji appears black and white at the exit ? nerd 3 5,518 Jan-28-2019, 11:34 PM
Last Post: nerd
  Real time pricing Yahoo finance nathanz 1 2,486 Aug-09-2018, 01:02 PM
Last Post: metulburr
  Python interface only black and white........ Wilson 3 6,050 Jul-15-2017, 01:20 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020