![]() |
a Python class for Option - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: a Python class for Option (/thread-25427.html) |
a Python class for Option - deberi - Mar-30-2020 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 RE: a Python class for Option - ndc85430 - Mar-30-2020 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. RE: a Python class for Option - deberi - Apr-28-2020 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: 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 RE: a Python class for Option - buran - Apr-28-2020 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 RE: a Python class for Option - deberi - Apr-28-2020 (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 any advice on how to move forward with this? i followed the handbook RE: a Python class for Option - buran - Apr-28-2020 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?
|