Mercurial > public > python-black-scholes
changeset 9:6f9a6fc6d4d9 0.0.3
add put option calculations
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Sat, 20 Nov 2021 18:47:17 +0100 |
parents | f213aa4891fc |
children | 8045763aecda |
files | README.md setup.cfg src/fbs/helpers.py src/fbs/main.py tests/test_helpers.py |
diffstat | 5 files changed, 64 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/README.md Sat Nov 20 18:04:06 2021 +0100 +++ b/README.md Sat Nov 20 18:47:17 2021 +0100 @@ -27,7 +27,12 @@ #### Command ```bash -fbs --spot-price=20.00 --exercise-price=21.00 --risk-free-rate=0.05 --std=0.25 --expiration=0.5 +fbs \ +--spot-price=20.00 \ +--exercise-price=21.00 \ +--risk-free-rate=0.05 \ +--std=0.25 \ +--expiration=0.5 ``` #### Output @@ -36,4 +41,6 @@ --------------------------------------------- European call option price: 1.197698084193286 --------------------------------------------- +European put option price: 1.6792062367882679 +--------------------------------------------- ``` \ No newline at end of file
--- a/setup.cfg Sat Nov 20 18:04:06 2021 +0100 +++ b/setup.cfg Sat Nov 20 18:47:17 2021 +0100 @@ -1,6 +1,6 @@ [metadata] name = fucking-black-scholes -version = 0.0.2 +version = 0.0.3 author = Dennis Concepcion Martin author_email = dennisconcepcionmartin@gmail.com description = A simple command line tool for pricing options using the Black-Scholes model
--- a/src/fbs/helpers.py Sat Nov 20 18:04:06 2021 +0100 +++ b/src/fbs/helpers.py Sat Nov 20 18:47:17 2021 +0100 @@ -27,13 +27,26 @@ def compute_eu_call_price(self): """ Determine the value of an European call option - :return: + :return: float """ d1 = self.compute_d1() d2 = self.compute_d2() - price = self.s * norm.cdf(d1) - self.k * math.exp(- self.r * self.t) * norm.cdf(d2) + price = self.s * norm.cdf(d1) - self.k * math.exp(-self.r * self.t) * norm.cdf(d2) + + return price + + def compute_eu_put_price(self): + """ + Determine the value of an European put option + :return: float + """ + + d1 = self.compute_d1() + d2 = self.compute_d2() + + price = self.k * math.exp(- self.r * self.t) * norm.cdf(-d2) - self.s * norm.cdf(-d1) return price @@ -60,3 +73,21 @@ d2 = d1 - self.std * np.sqrt(self.t) return d2 + + +def format_output(text, price): + """ + Format output to print to terminal + :param text: string, required + :param price: float, required + :return: string, string + """ + + output = '{}: {}'.format(text, price) + output_length = len(output) + line = ''.join('-' for _ in range(output_length)) + + return line, output + + +
--- a/src/fbs/main.py Sat Nov 20 18:04:06 2021 +0100 +++ b/src/fbs/main.py Sat Nov 20 18:47:17 2021 +0100 @@ -1,5 +1,6 @@ import click from fbs.helpers import Option +from fbs.helpers import format_output @click.command() @@ -11,14 +12,23 @@ def cli(spot_price, exercise_price, risk_free_rate, std, expiration): option = Option(spot_price, exercise_price, risk_free_rate, std, expiration) - # Format output - output = 'European call option price: {}'.format(option.compute_eu_call_price()) - output_length = len(output) - line = ''.join('-' for _ in range(output_length)) + line, call_output = format_output( + text='European call option price', + price=option.compute_eu_call_price() + ) - # Output + _, put_output = format_output( + text='European put option price', + price=option.compute_eu_put_price() + ) + + # Output call click.echo(line) - click.echo(output) + click.echo(call_output) + + # Output put + click.echo(line) + click.echo(put_output) click.echo(line)
--- a/tests/test_helpers.py Sat Nov 20 18:04:06 2021 +0100 +++ b/tests/test_helpers.py Sat Nov 20 18:47:17 2021 +0100 @@ -17,6 +17,12 @@ self.assertEqual(price, 1.20, 'EU call price is not equal to 1.20') + def test_compute_eu_put_price(self): + price = self.option.compute_eu_put_price() + price = round(price, 2) + + self.assertEqual(price, 1.68, 'EU put price is not equal to 1.68') + def test_compute_d1(self): d1 = self.option.compute_d1() d1 = round(d1, 2)