Solved

Looking for code to calculate APR

Posted on 2006-06-30
9
1,688 Views
Last Modified: 2008-01-09
I am looking for code to calculate APR.
Specifically APR as defined in the UK by the FSA/OFT/DTI etc.

I have a feeling that it is done differently in the US.

I would prefer this in C# but I will accept offers in other languages as long as they don't use language specific functions (e.g. there are functions that do this built in to VB but they are no good to me).

For any given loan I have :

a - the loan amount
b - the number of payments
c - the interest rate
d - the total interest
e - the total repayment value

I just need the APR.

Cheers.
0
Comment
Question by:ozymandias
  • 4
  • 3
9 Comments
 
LVL 10

Expert Comment

by:athapa
ID: 17024280
If you post the vb code then that could be converted to c#. There are free tools and web services which can do that for you too.
0
 
LVL 15

Author Comment

by:ozymandias
ID: 17024361
I don't have any VB code if I did I could probably convert it myself.
0
 
LVL 10

Expert Comment

by:athapa
ID: 17025176
>(e.g. there are functions that do this built in to VB but they are no good to me)
Do you know which function in VB would calculate APR? Or are you saying that VB has language specific function?

Anyway I found this php script and converted that to c#. I'm not sure how accurate the script is. Let me know.
You can find that php script at  http://www.hotscripts.com/Detailed/11214.html

HTH,
AT

public class APR
{
      public APR()
      {
      }

      double calcPv(double i, double n)
      {
            // calculate present value                        
            return (1 - (System.Math.Pow((1.0D+i),-n)))/i;
      
      }

      double calcLoanPayment(double loanAmount, double rate, int years)
      {
            // calculate loan payment
            return loanAmount / calcPv(rate/12, years*12);
      }



      public double calcAPR(double loanAmount, int years, double rate, double points, double fees)
      {
      
            // calculate apr
            // this function does not validate input!!! the calling
            // application will have to make sure that valid input is entered.

            rate *=  .01;
            double f = 0;
            double f1 = 0;
      
            points = (loanAmount * (points * .01));

            f1 =  (loanAmount - (points+fees)) / (calcLoanPayment(loanAmount, rate, years) * 12);

      
            f = 999;
            double i = 0.0;
      
            while (f > f1)
            {

                  i = i + .01;
                  f = calcPv(i/12,years*12)/12;
            }

            while (f < f1)
            {
                  i = i - .001;
                  f = calcPv(i/12,years*12)/12;
            }  

            while (f > f1)
            {
                  i = i + .0001;
                  f = calcPv(i/12,years*12)/12;
            }  
      
            while (f < f1)
            {
                  i = i - .00001;
                  f = calcPv(i/12,years*12)/12;
            }

            while (f > f1)
            {
                  i = i + .000001;
                  f = calcPv(i/12,years*12)/12;
            }

            return i * 100;            // multiply by 100 to get a 'displayable' percentage

      }


}
0
 
LVL 10

Expert Comment

by:athapa
ID: 17025184
If Excel COM component is an option then check this out
http://www.bygsoftware.com/Excel/functions/PV_RATE.htm
If you release all com objects referenced on your calls then you won't leak memories or handles.

AT
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 15

Author Comment

by:ozymandias
ID: 17025983
Thanks. I have seen that same PHP script already.
It does not calculate APR as  defined in the UK.
I think it must be the US definition of APR.

Here are two definitions of how APR SHOULD be calculated (legally in the UK) :

http://www.iansharpe.com/art_apr.php
http://www.oft.gov.uk/NR/rdonlyres/19342C6E-BB3E-41E9-AA15-29054CD31D77/0/oft144.pdf

The above script does not vary its output based on the term of the loan.
If I borrow 1000 at 5% (no other fees etc) and repay it over 24 months the APR should be different from a scenario where I take the exact same loan and repay it over 48 months.
0
 
LVL 15

Author Comment

by:ozymandias
ID: 17026115
OK. This is the best I have been able to come up with.

It would be usefulto find a "completely relaible" APR calculator to compare results with.

            protected double APR(double LoanAmount, double PaymentAmount, double NumberOfPayments){
                  
                  double deposit = 0;
                  double p = LoanAmount;
                  double a = PaymentAmount;
                  double n = NumberOfPayments;
                  double r = 1.01;
                  double pp = (p - deposit);
                  double rnew = 0;
                  double q = 0;

                  while (Math.Abs(r - rnew) > 0.0000001){
                        double rn = Math.Pow(r,n);
                        double rnm1 = rn/r;
                        double fac = (pp*rn-q)*(1-r)/(1-rn)-a;
                        double facdash = (n*pp*rnm1*(1-r)-(pp*rn-q))/(1-rn)+(pp*rn-q)*(1-r)*n*rnm1/((1-rn)*(1-rn));
                        rnew = r-fac/facdash;
                        r = rnew;
                  }
                  double r12 = Math.Pow(r,12);
                  double apr = 0.01 * Math.Round((r12-1)*100*100);
                  return apr;
            }

Therefore, until I can either prove or disprove the accuracy of the above I will award points to ahyone who can provide me with a "known good" source of APR calculations with which I can compare my results.

Cheers.
0
 
LVL 15

Author Comment

by:ozymandias
ID: 17036915
Here is my final code which seems to work.
Where it differs from other APR calculations it is just a case of rounding differences.

            protected double APR(double LoanAmount, double PaymentAmount, double NumberOfPayments, bool EndOfPeriod){
                  
                  double deposit = 0;
                  double p = LoanAmount;
                  double a = PaymentAmount;
                  double n = NumberOfPayments;

                  if (!EndOfPeriod){
                        p = LoanAmount - PaymentAmount;
                        n -= 1;
                  }

                  double r = 1.01;
                  double pp = (p - deposit);
                  double rnew = 0;
                  double q = 0;

                  while (Math.Abs(r - rnew) > 0.0000001){
                        double rn = Math.Pow(r,n);
                        double rnm1 = rn/r;
                        double fac = (pp*rn-q)*(1-r)/(1-rn)-a;
                        double facdash = (n*pp*rnm1*(1-r)-(pp*rn-q))/(1-rn)+(pp*rn-q)*(1-r)*n*rnm1/((1-rn)*(1-rn));
                        rnew = r-fac/facdash;
                        r = rnew;
                  }
                  double r12 = Math.Pow(r,12);
                  double apr = Math.Round(0.01 * Math.Round((r12-1)*100*100),1);
                  return apr;
            }
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 17075425
Closed, 500 points refunded.
Netminder
Site Admin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What does this  TN Map<T, TN>(T src) mean? 5 33
Using Simpleject with Class Library 11 25
Create XML 5 35
XAML: Layout 8 9
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now