Python for Finance: Analyze Big Financial Data

(Elle) #1

def add_constant(self, key, constant):
self.constants[key] = constant


def get_constant(self, key):
return self.constants[key]


def add_list(self, key, list_object):
self.lists[key] = list_object


def get_list(self, key):
return self.lists[key]


def add_curve(self, key, curve):
self.curves[key] = curve


def get_curve(self, key):
return self.curves[key]


def add_environment(self, env):


overwrites existing values, if they exist


for key in env.constants:
self.constants[key] = env.constants[key]
for key in env.lists:
self.lists[key] = env.lists[key]
for key in env.curves:
self.curves[key] = env.curves[key]


Although there is nothing special in the market_environment class, a simple example


shall illustrate how convenient it is to work with instances of the class:


In  [ 1 ]:  from market_environment import  *

In  [ 2 ]:  import datetime as dt

In  [ 3 ]:  dates   =   [dt.datetime( 2015 ,     1 ,     1 ),   dt.datetime( 2015 ,  7 ,     1 ),
dt.datetime( 2016 , 1 , 1 )]

In  [ 4 ]:  csr =   constant_short_rate(‘csr’,  0.05)

In  [ 5 ]:  me_1    =   market_environment(‘me_1’,  dt.datetime( 2015 ,  1 ,     1 ))

In  [ 6 ]:  me_1.add_list(‘symbols’,    [‘AAPL’,    ‘MSFT’, ‘FB’])

In  [ 7 ]:  me_1.get_list(‘symbols’)
Out[ 7 ]: [‘AAPL’, ‘MSFT’, ‘FB’]

In  [ 8 ]:  me_2    =   market_environment(‘me_2’,  dt.datetime( 2015 ,  1 ,     1 ))

In  [ 9 ]:  me_2.add_constant(‘volatility’, 0.2)

In  [ 10 ]: me_2.add_curve(‘short_rate’,    csr)        #   add instance    of  discounting class

In  [ 11 ]: me_2.get_curve(‘short_rate’)
Out[ 11 ]: <constant_short_rate.constant_short_rate at 0x104ac3c90>

In  [ 12 ]: me_1.add_environment(me_2)      #   add complete    environment

In  [ 13 ]: me_1.get_curve(‘short_rate’)
Out[ 13 ]: <constant_short_rate.constant_short_rate at 0x104ac3c90>

In  [ 14 ]: me_1.constants
Out[ 14 ]: {‘volatility’: 0.2}

In  [ 15 ]: me_1.lists
Out[ 15 ]: {‘symbols’: [‘AAPL’, ‘MSFT’, ‘FB’]}

In  [ 16 ]: me_1.curves
Out[ 16 ]: {‘short_rate’: <constant_short_rate.constant_short_rate at 0x104ac3c90>}

In  [ 17 ]: me_1.get_curve(‘short_rate’).short_rate
Out[ 17 ]: 0.05

This illustrates the basic handling of this rather generic “storage” class. For practical

Free download pdf