Created
December 15, 2013 04:02
-
-
Save Artiavis/7968715 to your computer and use it in GitHub Desktop.
A quick script for calculating some of the Greeks in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from math import erf,exp,log,pi,sqrt | |
import argparse | |
def phi(x): | |
'Cumulative distribution function for the standard normal distribution' | |
return (1.0 + erf(x / sqrt(2.0))) / 2.0 | |
def d(pos,r, sigma, T, K, S0): | |
x = ((r+0.5*sigma**2)*T - log(K/S0))/(sigma*sqrt(T)) | |
if not pos: | |
x -= sigma*sqrt(T) | |
return x | |
def _delta(d1): | |
return phi(d1) | |
def _gamma(sigma, T, S0, d1): | |
return 1/(sigma*S0*sqrt(2*pi*T))*exp(-d1**2/2) | |
def _theta(r, T, X, S0, sigma, gamma, d2): | |
return -r*exp(-r*T)*X*phi(d2)-0.5*sigma**2*S0**2*gamma | |
def main(S0, X, r, t, sigma): | |
T = t/12 | |
d1 = d(True, r, sigma, T, X, S0) | |
d2 = d(False, r, sigma, T, X, S0) | |
delta = _delta(d1) | |
gamma = _gamma(sigma, T, S0, d1) | |
theta = _theta(r, T, X, S0, sigma, gamma, d2) | |
print() | |
print("Delta: {0}".format(delta)) | |
print("Gamma: {0}".format(gamma)) | |
print("Theta: {0}".format(theta)) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description = "Prints the greeks for specified params") | |
parser.add_argument('price', metavar='S0', type=float, help='current price of underlying stock') | |
parser.add_argument('strike', metavar='X', type=float, help='strike price of option') | |
parser.add_argument('interest', metavar='r', type=float, help='interest per period') | |
parser.add_argument('time', metavar='T', type=float, help='period (months)') | |
parser.add_argument('sigma', metavar=u'σ', type=float, help='volatility') | |
args=parser.parse_args() | |
main(args.price, args.strike, args.interest, args.time, args.sigma) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment