Python for Finance: Analyze Big Financial Data

(Elle) #1

Valuation of European volatility options


in Gruenbichler-Longstaff (1996) model


square-root diffusion framework


— parameter dictionary & web service function



from vol_pricing_formula import calculate_option_value


model parameters


PARAMS={
‘V0’ : ‘current volatility level’,
‘kappa’ : ‘mean reversion factor’,
‘theta’ : ‘long-run mean of volatility’,
‘sigma’ : ‘volatility of volatility’,
‘zeta’ : ‘factor of the expected volatility risk premium’,
‘T’ : ‘time horizon in years’,
‘r’ : ‘risk-free interest rate’,
‘K’ : ‘strike’
}


function for web service


def get_option_value(data):
”’ A helper function for web service. ”’
errorline = ‘Missing parameter %s (%s)\n’
errormsg = ”
for para in PARAMS:
if not data.has_key(para):


check if all parameters are provided


errormsg += errorline % (para, PARAMS[para])
if errormsg != ”:
return errormsg
else:
result = calculate_option_value(
float(data[‘V0’]),
float(data[‘kappa’]),
float(data[‘theta’]),
float(data[‘sigma’]),
float(data[‘zeta’]),
float(data[‘T’]),
float(data[‘r’]),
float(data[‘K’])
)
return str(result)


To begin with, we add the path of the aforementioned Python scripts:


In  [ 87 ]: import sys
sys.path.append(“../python/volservice”)
# adjust if necessary to your path

We use the library Werkzeug to handle our WSGI application-based web service (recall that


Werkzeug is an integral part of Flask). To this end, we need to import some functions from


Werkzeug sublibraries:


In  [ 88 ]: from werkzeug.wrappers import Request,  Response

Furthermore, for our core WSGI application to follow, we need the function


get_option_value that we defined earlier:


In  [ 89 ]: from vol_pricing_service import get_option_value

The only thing that remains is to implement the WSGI application (function) itself. This


function might in our case look as follows:


In  [ 90 ]: def application(environ,    start_response):
request = Request(environ)
# wrap environ in new object
text = get_option_value(request.args)
# provide all parameters of call to function
# get back either error message or option value
response = Response(text, mimetype=‘text/html’)
# generate response object based on the returned text
Free download pdf