Skip to content

Instantly share code, notes, and snippets.

@tylermorten
Created April 13, 2012 14:45
Show Gist options
  • Save tylermorten/2377376 to your computer and use it in GitHub Desktop.
Save tylermorten/2377376 to your computer and use it in GitHub Desktop.
Calculate XNPV
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalculateXNPV
{
class Calculate
{
IEnumerable<DateTime> dates;
IEnumerable<int> payments;
public Calculate(IEnumerable<DateTime> dates, IEnumerable<int> payments)
{
this.dates = dates;
this.payments = payments;
}
public double CalcXnpv(int period, double rate)
{
if (period == 0)
{
return payments.ElementAt(0);
}
TimeSpan span = dates.ElementAt(period) - dates.ElementAt(0);
return CalcXnpv(period - 1, rate) + (payments.ElementAt(period) / Math.Pow((1 + (rate/365)), span.Days));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment