I am doing a project where I have to calculate year to date daily compound interest on transactions.Currently I am only getting the current balance with no interest. I believe my issue is with the foreach loop but I am not sure where I have made a mistake. It is going through the days but either there is no interest being applied or runningTotal is not carrying over properly.
public IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
{
for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
yield return day;
}
public decimal PrincipalplusInterest()
{
DateTime startDate = new DateTime( DateTime.Now.Year, 1, 1);
DateTime endDate = startDate.AddYears(1);
int compoundingTimes = 12;
TimeSpan duration = endDate - startDate;
var numberOfDaysInYear = duration.Days;
decimal runningTotal = 0;
double interestRate = InterestRate;
foreach (DateTime day in EachDay(startDate, endDate))
{
decimal sumOfAnyTransactionToday = Transactions.Where(x => x.TransactionDate == day).Sum(x => x.Amount);
runningTotal = runningTotal + sumOfAnyTransactionToday;
double timePeriod = 1 / numberOfDaysInYear;
double rate = interestRate / compoundingTimes;
double total = System.Convert.ToDouble(runningTotal);
double principalAndInterest = total * Math.Pow((1 + rate), (compoundingTimes * timePeriod));
runningTotal = System.Convert.ToDecimal(principalAndInterest);
}
return runningTotal;
}
[DisplayName("Year to Date Interest")]
public decimal YrToDateInterest()
{
decimal interest = PrincipalplusInterest() - CurrentBalanceCalculation();
return interest;
}