static void Main(string[] args) { ////入金为负值,出金为正值 //double[] payments = { -6800, 1000, 2000, 4000 }; // payments // 以第一天为起始点的天数; //double[] days = { 01, 08, 16, 25 }; // days of payment (as day of year) //double xirr = XirrCalculator.Newtons_method(0.1, // XirrCalculator.total_f_xirr(payments, days), // XirrCalculator.total_df_xirr(payments, days)); List <TransactionModel> transactionModelList = new List <TransactionModel>() { new TransactionModel() { TransactionAmountUSD = 8000, TransactionType = TransactionType.Deposit, TransactionDateUtc = new DateTime(2016, 1, 1) }, new TransactionModel() { TransactionAmountUSD = 1000, TransactionType = TransactionType.WithDraw, TransactionDateUtc = new DateTime(2017, 2, 1) }, new TransactionModel() { TransactionAmountUSD = 2000, TransactionType = TransactionType.WithDraw, TransactionDateUtc = new DateTime(2017, 3, 1) }, new TransactionModel() { TransactionAmountUSD = 2000, TransactionType = TransactionType.WithDraw, TransactionDateUtc = new DateTime(2017, 4, 1) }, new TransactionModel() { TransactionAmountUSD = 2000, TransactionType = TransactionType.WithDraw, TransactionDateUtc = new DateTime(2017, 5, 1) } }; double xirr = XirrCalculatorUtility.Xirr(transactionModelList); Console.WriteLine("XIRR value is {0}", xirr); Console.ReadKey(); }
public static double Xirr(IEnumerable<TransactionModel> transactionModels) { List<double> paymentList = new List<double>(); List<double> dayList = new List<double>() ; TransactionModel firstTransaction = transactionModels.ToList().FirstOrDefault(); foreach (var tmpTransaction in transactionModels) { if (tmpTransaction.TransactionType == TransactionType.Deposit) paymentList.Add((double)-tmpTransaction.TransactionAmountUSD); else paymentList.Add((double)tmpTransaction.TransactionAmountUSD); //Days是int; TotalDays是double dayList.Add((tmpTransaction.TransactionDateUtc-firstTransaction.TransactionDateUtc).Days); } return Xirr(0.1, XirrCalculatorUtility.total_f_xirr(paymentList.ToArray(), dayList.ToArray()), XirrCalculatorUtility.total_df_xirr(paymentList.ToArray(), dayList.ToArray())); }