Skip to content

Instantly share code, notes, and snippets.

@tlmaloney
Created December 7, 2011 04:26
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/1441458 to your computer and use it in GitHub Desktop.
Save tlmaloney/1441458 to your computer and use it in GitHub Desktop.
Price class
'''
Created on Nov 26, 2011
@author: tlmaloney
'''
import Payment
class Price(object):
'''
Price objects are produced from a Market.
In general, prices in the market can be represented by yields, implied
volatility, and spreads, but they should all be expressible as a cash price.
'''
def __init__(self, target_asset, price_level, settle_currency, time_stamp, settle_date):
'''
Constructor
Keyword arguments:
target_asset -- asset that is being priced
price_level -- level of price
settle_date-- date on which transfer of ownership occurs
settle_currency -- currency in which settlement amount is denominated
time_stamp -- date/time when price was observed
'''
self.target_asset = target_asset
self.price_level = price_level
self.settle_date = settle_date
self.settle_currency = settle_currency
self.time_stamp = time_stamp
def calc_cash_amount(self, market):
'''
Method to convert any Price object to a CurrencyPayment.
Refers to one unit of the target asset.
Implementation will be child-class specific.
'''
pass
def equals(self, price, market):
"""
Two prices are equal if:
They are for the same target Asset
For the same settlement date
Produce the same cash payment
Keyword arguments:
price -- Price instance
market -- Market instance
"""
try:
return self.target_asset == price.target_asset and self.settle_date == price.settle_date and self.calc_cash_amount(market) == price.calc_cash_market(market)
except Exception:
return False
def __repr__(self):
"""Return a string representation of this class.
"""
return self.__class__.__name__ + "(" + repr(self.target_asset) + "," + repr(self.price_level) + ", " + repr(self.settle_currency) + ", " + repr(self.time_stamp) + ", " + repr(self.settle_date) + ")"
def __str__(self):
"""Return a human-friendly string for this class.
"""
return "The price of " + str(self.target_asset) + " is " + str(self.price_level) + " of " + str(self.settle_currency) + " on " + str(self.time_stamp) + " settled on " + str(self.settle_date)
class CashPrice(Price):
'''
Price the target asset in terms of a price level of settle currency.
'''
def __init__(self, target_asset, price_level, settle_currency, time_stamp, settle_date):
'''
Constructor
Keyword arguments:
target_asset -- asset that is being priced
price_level -- level of price
settle_date-- date on which transfer of ownership occurs
settle_currency -- currency in which settlement amount is denominated
time_stamp -- date/time when price was observed
'''
self.target_asset = target_asset
self.price_level = price_level
self.settle_date = settle_date
self.settle_currency = settle_currency
self.time_stamp = time_stamp
def calc_cash_amount(self, market=None):
'''
Method to convert CashPrice object to a CurrencyPayment.
Refers to one unit of the target asset.
In this case, market is not necessary.
'''
return Payment.make_currency_payment(self.settle_date, self.settle_currency, self.price_level)
def make_cash_price(target_asset, price_level, settle_currency, time_stamp, settle_date=None):
'''
Makes a new CashPrice instance
Keyword arguments:
target_asset -- asset that is being priced
price_level -- level of price
settle_date -- date on which transfer of ownership occurs, optional
settle_currency -- currency in which settlement amount is denominated
time_stamp -- date/time when price was observed
'''
return CashPrice(target_asset, price_level, settle_currency, time_stamp, settle_date)
'''
Created on Nov 28, 2011
@author: tlmaloney
'''
import unittest
import Price
import fimero.time.SerialDate as SD
import Asset
target_asset = Asset.make('Asset1')
price_level = 1.0
settle_currency = Asset.make('Currency')
time_stamp = SD.make(20110101)
price = Price.make_cash_price(target_asset, price_level, settle_currency, time_stamp)
time_stamp2 = SD.make(20110102)
price2 = Price.make_cash_price(target_asset, price_level, settle_currency, time_stamp2)
class TestPrice(unittest.TestCase):
def test_variables(self):
self.assertEqual(target_asset, price.target_asset)
self.assertEqual(price_level, price.price_level)
self.assertEqual(time_stamp, price.time_stamp)
self.assertEqual(settle_currency, price.settle_currency)
def test_equals(self):
self.assertFalse(price.equals(price2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment