using System; using System.Linq; using EvoCalculator.Core.FinanceFormulas; using EvoCalculator.Core.Models.Calculation.Models.Prepared; using TridentGoalSeek; namespace EvoCalculator.Core.Calculation.Columns { public class SumColumn : BaseColumnWithXIRR, IGoalSeekAlgorithm { private readonly PreparedValues _preparedValues; private readonly PercentPaymentColumn _percentPaymentColumn; public SumColumn(int count, DateTempColumn dateTempColumn , PreparedValues preparedValues , PercentPaymentColumn percentPaymentColumn) : base(count, dateTempColumn) { this._preparedValues = preparedValues; this._percentPaymentColumn = percentPaymentColumn; } public void ComputeValues(double x) { Values[0] = -this._preparedValues.BaseCost; Values[1] = this._preparedValues.FirstPaymentSum; for (var i = 2; i < Values.Length - 1; i++) { Values[i] = x * _percentPaymentColumn.Values[i] / 100; } Values[^1] = _preparedValues.LastPaymentSum; } public decimal Calculate(decimal inputVariable) { var x = Convert.ToDouble(inputVariable); this.ComputeValues(x); var XIRR = new XIRR(Flows); IRR = XIRR.GetResult(); return Convert.ToDecimal(IRR); } public void ComputeValues(decimal requiredIRR) { var goalSeek = new GoalSeek(this); goalSeek.SeekResult(requiredIRR, new GoalSeekOptions( startingStabPoint: Convert.ToDecimal(this._preparedValues.BaseCost / this._preparedValues.Nmper) , tineExplorePercentage: 10 // , maximumAttempts: 10000 // , initialTineSpacing: 1 // , focusPercentage: 100 // , trimFinalInputValue: true )); } } }