2021-12-15 11:30:14 +03:00

39 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using EvoCalculator.Core.Models.Common;
namespace EvoCalculator.Core.FinanceFormulas
{
public class XNPV
{
private readonly IEnumerable<Flow> _flows;
private double _rate;
public XNPV(IEnumerable<Flow> flows, double rate)
{
_flows = flows;
_rate = rate;
}
public double GetResult()
{
if (_rate <= -1)
_rate = -1 + 1E-10; // Very funky ... Better check what an IRR <= -100% means
var startDate = _flows.OrderBy(i => i.Date).First().Date;
return
(from item in _flows
let days = -(item.Date - startDate).Days
select (double)item.Value * Math.Pow(1 + _rate, (double)days / 365)).Sum();
}
public double GetResultPrime()
{
var startDate = _flows.OrderBy(i => i.Date).First().Date;
return (from item in _flows
let daysRatio = -(item.Date - startDate).Days / 365
select (double)item.Value * daysRatio * Math.Pow(1.0 + _rate, daysRatio - 1)).Sum();
}
}
}