Python for Finance: Analyze Big Financial Data

(Elle) #1
Figure 14-12. Screenshot of the error message of the web service

However, usually you want to use a web service quite a bit differently — for example,


from a scripting environment like IPython. To this end, we can use the functionality the


urllib library provides:


In  [ 91 ]: import numpy as np
import urllib
url = ‘http://localhost:4000/’

A simple call to the web service without providing any parameters returns the following


error message, which (apart from formatting issues) is the same as in the screenshot in


Figure 14-12:


In  [ 92 ]: print urllib.urlopen(url).read()
Out[92]: Missing parameter V0 (current volatility level)
Missing parameter r (risk-free interest rate)
Missing parameter kappa (mean-reversion factor)
Missing parameter T (time horizon in years)
Missing parameter theta (long-run mean of volatility)
Missing parameter zeta (factor of the expected volatility risk premium)
Missing parameter sigma (volatility of volatility)
Missing parameter K (strike)

Of course, we need to provide a number of parameters. Therefore, we first build a URL


string object in which we can replace specific parameter values during later calls:


In  [ 93 ]: urlpara =   url +   ‘application?V0=%s&kappa=%s&theta=%s&sigma=%s&zeta=%s’
urlpara += ‘&T=%s&r=%s&K=%s’

A possible parameterization might be the following one:


In  [ 94 ]: urlval  =   urlpara %   ( 25 ,  2.0,     20 ,   1.0,    0.0,    1.5,    0.02,   22.5)
urlval
Out[94]: ‘http://localhost:4000/application?V0=25&kappa=2.0&theta=20&sigma=1.0&z
eta=0.0&T=1.5&r=0.02&K=22.5’

Using this particular URL string returns an option value, as desired:


In  [ 95 ]: print urllib.urlopen(urlval).read()
Out[95]: 0.202937705934
Free download pdf