Python Forum
a Python class for Option
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a Python class for Option
#1
Hi people!

I am not so new to python but still need help. I am really motivated to master all round python for finance (quant related).

Can you help with this easy task and explain me how to write this code.

*

Need a Python class for Option that does the following:

* To hold option attributes (Option type, Strike, time to expiry)

* Methods to price the option using black scholes formula

* Also methods to calculate Delta, Gamma, Vega, Theta

* These methods take an object of type Option as a parameter and additional parameters to take market details

* Market details are (Stock price, Interest Rate, Volatility)

* Write a script to construct a Call option on Apple Stock strike of 275 and time to expiry 1 year, market details are Stock price of 275, Interest rate of 2% and volatility of 30%

Call the above methods to calculate Price, Delta, Gamma, Vega, Theta

Write a script to compute and plot Price, Delta, Gamma, Vega, and Theta for the above call option and market details, but show how the Price and greeks change as time to expiry goes from 1 day, 1 week, 1 -12 months


THANK YOU
Reply
#2
We aren't here to do the work for you. What have you tried? If you don't know about classes, or finance, then you should go and learn using whatever materials are available to you. Then, try something based on what you've learnt and ask a more specific question when you're stuck.
Reply
#3
Hi,
I tried with this code, according to one of the finance books:

from math import * 
def bs_call(S,X,T,r,sigma):    
    d1 = (log(S/X)+(r+sigma*sigma/2.)*T)/(sigma*sqrt(T))    
    d2 = d1-sigma*sqrt(T)    
    return S*CND(d1)-X*exp(-r*T)*CND(d2)
def CND(X):  
    (a1,a2,a3,a4 ,a5)=(0.31938153,-0.356563782,1.781477937,-1.821255978,1.330274429)  
    L = abs(X)  
    K=1.0/(1.0+0.2316419*L)  
    w=1.0-1.0/sqrt(2*pi)*exp(-L*L/2.)*(a1*K+a2*K*K+a3*pow(K,3)+a4*pow(K,4)+ a5*pow(K,5))  
    if X<0:       
        w = 1.0-w  
        return w
bs_call(275,275,1,0.2,0.3)
but the error popped out:
Error:
TypeError Traceback (most recent call last) <ipython-input-3-ce84fa015079> in <module> ----> 1 bs_call(275,275,1,0.2,0.3) <ipython-input-1-1d3bcf5eff6d> in bs_call(S, X, T, r, sigma) 3 d1 = (log(S/X)+(r+sigma*sigma/2.)*T)/(sigma*sqrt(T)) 4 d2 = d1-sigma*sqrt(T) ----> 5 return S*CND(d1)-X*exp(-r*T)*CND(d2) TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
can anyone tell me what I am doing wrong? I was trying this in Jupiter:

Python class for Option

To hold option attributes (Option type, Strike, time to expiry) *
Methods to price the option using black scholes formula.
* Write a script to construct a Call option on Apple Stock
strike of 275 and time to expiry 1 year;
Stock price of 275,
Interest rate of 2% and
volatility of 30%

Write a script to compute and plot Price, Delta, Gamma, Vega, and Theta for the above call option and market details,
And show how the Price and greeks change as time to expiry goes from 1 day, 1 week, 1 -12 months
Reply
#4
in your CDN function, if X >= 0 it will return None. Obviously that is what happens in this case to cause an error (X=275). You may want to handle this case

As a side note - what you have done so far is NOT class, it's 2 functions
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
(Apr-28-2020, 07:47 AM)buran Wrote: in your CDN function, if X >= 0 it will return None. Obviously that is what happens in this case to cause an error (X=275). You may want to handle this case

As a side note - what you have done so far is NOT class, it's 2 functions


any advice on how to move forward with this? i followed the handbook
Reply
#6
you need to account for case when X>=0. It's up to you what you want to return in this case, but it should be a number (e.g. int, float, etc.) that can be used in calculations. Maybe you just want return w unindented one level?
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
  form select option tags, add "selected" on <option> in python at a condition Leon79 3 2,653 Jul-14-2020, 05:00 PM
Last Post: Leon79

Forum Jump:

User Panel Messages

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