This video demonstrates how to implement the Black-Scholes option pricing model in Python, covering the calculation of d1 and d2 parameters, the use of the normal cumulative distribution function for pricing calls and puts, and the inclusion of error handling for invalid inputs. The implementation uses numpy for mathematical operations and scipy.stats.norm for the normal distribution, allowing users to calculate theoretical option prices based on underlying price, strike price, interest rate, volatility, and time to maturity.
Implementing Black-Scholes Option Pricing in Python: A Step-by-Step Guide
Added:hello and welcome back to ASX options this is Jonathan speaking today we're going to be implementing the black Scholes formula in Python so let's let's just get started so we're going to add a new file I'm using the visual studio editor but you could use whatever editor of Python you want so let's just go ahead create a new file black Scholes Doug py okay so we're going to be implementing the black Scholes formula in Python so the first thing we're going to want to do is import numpy as NP okay and we're gonna need and Sai PI dot that's or from so PI dot stats we're going to import norm because we're going to need the normal distribution so let's think about the parameters that we use in our Excel video so let's just replicate them so define variables so we hand our interest rate which was one percent or point zero one we have our underlying at $30 a strike price was 40 and time was 240 days remember that's out of 365 and and we also had our volatility or as Sigma which was 30% so let's create a function because then we're going to be able to use it again and again and again so let's call it black Scholes and we're going to take those inputs sk2 Sigma and there's one more thing we need to tell it which type it's going to be a call or a put so I'm going to call that type and we're gonna say that if we pass the string C it's going to be a call and if we pass the string P it's going to be a put but let's by default make it equal to C so what are we going to do in this function it's always good to define what you're going to do so if someone's reading over your code they can they can see clearly we're going to calculate the black Scholes option price for a call or put so the first thing that we're going to define is d1 and d2 so d1 is the natural logarithm so MP log of s over K plus the interest rate plus Sigma squared divided by two and this is what I forgot in the excel you have to multiply that by the time so let's divide that entire thing by sigma x by n p dot square root so the square root function you can get from math but I'm using the numpy square root could just have easily squared it to the half so now pretty comfortable that that's our d1 so let's go ahead and define d2 and if we remember from last time it's just d1 minus sigma x by the square root of time ok excellent so now we have to take into consideration whether it's going to be a call or a put and we also want to allow for the scenario that some of the values are coming in incorrectly so we're going to do a try and exception so we're going to try if the type above is equivalent to C then it's going to be the call so we're gonna call that the price if you remember if it's a call it's the underlying price times by the normal distribution so norm got a cumulative distribution function and we just have to type in d1 centered around 0 and because it's the standard normal distribution we have a very standard deviation of 1 so- the strife the discounted strike price so NP exponential minus x t so that's the discounted strike price times by norm C D F D to centered around zero and one excellent so now I would say else but let's just else if type is equal to P then the price of a push is going to be equal to the discounted strike forgot the MP there is countered strike times by the normal distribution of minus D to centered around zero variance 1 minus s times 1 dot C D F of minus D 1 0 1 excellent so that's ad that's that try and let's make our exception ok so when are we going to accept this so if something goes wrong what do we want it to print back let's say okay let's assume that therefore you've typed in neither C or P you've made some kind of mistake was to say please confirm all option parameters parameters above and three exclamation marks because why not excellent and we can't forget to absolutely return something so under the try if we've calculated a call price or a put price let's return the price of that option excellent so let's test this out so we're going to print option price is and I'm gonna round it because it'll have infinite amount of places so black Scholes and remember we've defined these parameters above so we've actually called them exactly by the name so let's just copy and paste them okay and because we're using the round function here because we're using the round function here we're going around two decimal places finish off the brackets save that down now in our terminal here we are going to type Python black-scholes py and we're just gonna hit run up there no no module Sify so for any of you that have that we're just gonna pip install those modules super easy I just didn't have that in this environment but numpy must have been there so we'll just wait for that to install and then we'll be able to run that again so just up key enter excellent option price is 51 cents and if you remember from the excel video that was about similar with with that despite the fact that we've missed out the T and that that's because it was close to one year so you know we divide where times in by 0.6 0.7 it's maybe a very small number anyway 0.3 squared divided by you know / - it's quite kind of a small number so times in by one or point six doesn't really make a big impact so let's see what happens when we change this to put yeah we're gonna save that and ten point two five so very very similar there so we're gonna try and add it and create a dashboard in further videos so we can make this more interactive excellent so thank you very much for listening guys this is a SX options and if you liked the video go ahead and subscribe so you get more content and hit the like button you
Up Next

Streetwear's Impact on Luxury Fashion Industry | Analysis
@THECASUALco
37.6K views•2019-07-02

Building Iconic Brands: Marketing Strategies from Rohan Oza
@CNBC
16.7K views•2017-09-28

Decoy Effect: How Pricing Psychology Influences Consumer Spending
@bobinvestsUS
90K views•2026-01-05

The Planned Obsolescence of Light Bulbs and Tech
@veritasium
25.3M views•2021-03-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Business







































