Skip to content

Instantly share code, notes, and snippets.

@tlmaloney
Created December 6, 2011 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlmaloney/1439426 to your computer and use it in GitHub Desktop.
Save tlmaloney/1439426 to your computer and use it in GitHub Desktop.
Market class
'''
Created on Nov 25, 2011
@author: tlmaloney
'''
class Market(object):
'''
A market provides information about observables, like Assets, Prices,
Agents, DiscountFactorCurves, etc.
The market will contain a dictionary of observables. The value
of each item in this dictionary will be an object (e.g. price, or discount
factor curve). For instance, market.observables['dfc_01'] could be a
DiscountFactorCurve object.
A market needs a unique_id, name, reference date, and reference asset, and
optionally a dictionary of observables.
'''
def __init__(self, unique_id, name, ref_date, ref_asset, observables={}):
'''
Constructor
Keyword arguments:
unique_id -- Unique market identifier
name -- name of the market, string instance
ref_date -- the first day of trading for the market, SerialDate instance
ref_asset -- the reference asset (could be currency), Asset instance
observables -- a dictionary of observables
'''
self.unique_id = unique_id
self.name = name
self.ref_date = ref_date
self.ref_asset = ref_asset
self.observables = observables
def set_observable(self, obs_id, obs):
'''
Set observable of type 'obs_id'
Keyword arguments:
obs_id -- observable identifier, string instance
obs -- observable object
'''
self.observables[obs_id] = obs
def get_observable(self, obs_id):
'''
Get observable of type 'obs_id'
Keyword arguments:
obs_id -- observable identifier, string instance
'''
return self.observables[obs_id]
def __repr__(self):
"""Return a string representation of this class.
"""
return self.__class__.__name__ + "(" + repr(self.unique_id) + ", " + repr(self.name) + ", " + repr(self.ref_date) + ", " + repr(self.ref_asset)+ ")"
def __str__(self):
"""Return a human-friendly string for this class.
"""
return "Market instance, Unique ID = " + str(self.unique_id) + ", name = " + str(self.name) + ", reference date = " + str(self.ref_date) + ", reference asset = " + str(self.ref_asset)
def make(unique_id, name, ref_date, ref_asset, observables={}):
'''
Makes a new Market instance
Keyword arguments:
unique_id -- Unique market identifier, integer instance
name -- name of the market, string instance
ref_date -- the first day of trading for the market, SerialDate instance
ref_asset -- the reference asset (could be currency), Asset instance
observables -- a dictionary of observables
'''
return Market(unique_id, name, ref_date, ref_asset, observables={})
'''
Created on Nov 28, 2011
@author: tlmaloney
'''
import unittest
import Market
import Asset
import Rate
import DiscountFactorCurve as DFC
import time.SerialDate as SD
unique_id1 = 1
name1 = 'Market1'
date1 = SD.make(20110101)
currency1 = Asset.make('Currency', 1)
market1 = Market.make(unique_id1, name1, date1, currency1)
class TestMarket(unittest.TestCase):
def test_market(self):
self.assertEqual(unique_id1, market1.unique_id)
self.assertEqual(name1, market1.name)
self.assertEqual(date1, market1.ref_date)
self.assertEqual(currency1, market1.ref_asset)
self.assertEqual(isinstance(market1.observables, dict), 1)
def test_observables(self):
flat_rate = Rate.make_continuous(0.05)
dfc = DFC.make_with_flat_rate(date1, flat_rate)
market1.set_observable('dfc_01', dfc)
self.assertEqual(dfc, market1.get_observable('dfc_01'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment