Market Risk Swap Trade in CouchDB

Sometime ago I blogged about the PV01 calculations of a swap. Given the Web 2.0 movement over the years, some work I did whilst on a train journey (or was it a plane, I can’t remember) was to makes the F# market risk calculation post the calculated values into CouchDB. Here a few bits and pieces from that work:

    "tradeId": 1002,
    "trader": "Terry Trader",
    "desk": "Bond Desk",
    "region": "New York",
    "notional": 100,
    "payDayCountBasis": "30/360",
    "receiveDayCountBasis": "A/360",
    "tradeDate": "2010-01-01",
    "assetClass": "Rates",
    "instrument": "Swap",
    "paymentFrequency": "6m",
    "payRate": "5%",
    "receiveRate": "USD LIBOR",
    "duration": "5Yr Annual Fixed 5%/Float LIBOR",
    "terminationDate": "2015-01-01",
    "effectiveDate": "2010-01-01",
    "exposureMeasureDates": "http:\\\\localhost:8001\\Rates\\Bond\\exposureDates\\20100101",
    "yieldCurve": "http:\\\\localhost:8001\\yieldCurve\\USD\\20100101",
    "forcastCurve": "http:\\\\localhost:8001\\forcastCurve\\USD\\20100101",
    "fixedLegs": ["2010-07-01", "2011-01-01", "2011-07-01", "2012-01-01", "2012-07-01", "2013-01-01", "2013-07-01", "2014-01-01", "2013-07-01", "2015-01-01"],
    "floatingLegs": ["2010-07-01", "2011-01-01", "2011-07-01", "2012-01-01", "2012-07-01", "2013-01-01", "2013-07-01", "2014-01-01", "2013-07-01", "2015-01-01"],
    "fixedLegPVCashflows": [-2.44,-2.38,-2.31,-2.25,-2.19,-2.16,-2.09,-2.03,-1.99,-1.95],
    "expectedExposures": {},
    "floatingLegPVCashflows": [2.45,2.62,2.51,2.62,2.35,2.03,2.35,2.20,2.04,1.99],
    "effectiveExpectedExposures": {},
    "effectiveExpectedPositiveExposure": {}

Which when inserted into CouchDB, allow one to calculate the NPV of the swap from the PV cashflows using the following Map and Reduce:

function(doc) {
  if (doc.instrument == "Swap") {
    emit(doc.tradeId, sum(doc.fixedLegPVCashflows)+sum(doc.floatingLegPVCashflows));

function (keys, values) {
  return sum(values);

You’ll notice from the JSON that I’ve omitted the various counterparty exposure values, mainly to reduce this posting size, but also to allow for more discussion in a future posting.

~ by mdavey on February 23, 2012.

One Response to “Market Risk Swap Trade in CouchDB”

  1. can you please provide a full code snippet from your f# swap (fpml) to cashflow generator ? (it is not in previous posts)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: