How to Build a Loan Calculator Using DecisionRules: A Technical Guide

The world of finance can be quite complicated. That’s why we chose a Loan Calculator as our next sample application. In this article, we’ll provide a detailed and easy-to-understand technical guide for the sample application and the logic implemented in DecisionRules.

By the end of this article, you’ll have a clear understanding of the application and the individual rules responsible for its functionality. If you want to replicate the rules that are used in our loan calculator, you can download the source files by clicking the download button bellow:

If you don’t know what a sample application is, be sure to check out our previous article: decisionrules.io/articles/decisionrules-in-action.

All available sample applications are available at samples.decisionrules.io.

1. Before we start…

In order to fully understand what our sample application does and the logic used inside of it, it’s important to answer a few questions:

  1. What is a loan?
    A loan is a contract between a borrower and a lender in which the borrower receives an amount of money (principal) that they are obligated to pay back in the future.

  2. What is an amortized loan?
    An amortized loan is a type of loan that is repaid in equal installments over a fixed period of time. Each payment consists of a portion of the principal and interest. As the loan is paid down, the amount of interest decreases and the amount of principal increases, resulting in a gradual reduction of the loan balance until it is fully repaid at the end of the term.

  3. What is a loan repayment calendar?
    A loan repayment calendar shows the borrower how much they need to pay each month, the portion that goes towards principal and interest, and the remaining balance of the loan.

Now that we know all of these terms, we can answer one final question:

  1. What does our loan calculator actually do?
    To put it simply, our loan calculator lets you calculate an amortized loan and generate a repayment calendar based on the calculated loan values.

2. Description of the web application

Description of the application runtime diagram:

  1. Initialization: When the web application loads for the first time, it automatically sends an API request to the individual rules in the 1. set of rules. As you can see in the diagram above, this set of rules is made up of 2 Rule Flows. The Sliders and Loan Calculator Rule Flows. Firstly, the web application calls the Sliders Rule Flow, which determines and returns the values of the sliders. After the values of the slider were determined/calculated, the Loan Calculator Rule Flow is called to calculate the individual loan values. 
  2. Reacting to changes: Whenever the user changes the input data in the web application, using sliders or inputs, the individual rules in the 1. set of rules are called in the same order as in the initialization part.
  3. Repayment schedule generation: When the user clicks the button to generate the repayment schedule, the web application calls the individual rules in the 2. set of rules.

3. Description of the individual rules and logic in DecisionRules

Our logic inside DecisionRules is divided into 3 main parts. In this section, we’ll describe all of these parts in detail.

  1. Loan Calculator (Rule Flow)
  2. Sliders (Rule Flow)
  3. Repayment Schedule

3.1. Loan Calculator (Rule Flow)

This Rule Flow is responsible for the calculation/determination of the interest rate, monthly payment, total payment, and APR values. It calculates/determines these values using the input data (loanAmount and loanDuration values) from the web application, and predefined formulas and conditions inside DecisionRules. As you can see in the diagram image above, the Rule Flow is made up of two Decision Tables and one Scripting Rule.

Description of the Rule Flow diagram:

  1. The Rule Flow receives the loanAmount and loanDuration values from the web application.
  2. The interestRate value is determined from the loanAmount and loanDuration values using the “Interest Rate” decision table.
  3. The monthlyPayment and totalPayment values are calculated from the loanAmount and loanDuration values and the previously determined interestRate value using the “Loan Calculator” decision table.
  4. The APR value is calculated from the loanAmount and loanDuration values and the calculated monthlyPayment value using the “APR” scripting rule.
  5. The Rule Flow returns the calculated values to the web application as an output.

In the code snippet below, you can see a commented example of the input and output model.

3.1.1. Interest Rate (Decision Table)

The purpose of this rule is to determine the interestRate value based on predefined conditions for the loanAmount and loanDuration values.

3.1.2. Loan Calculator (Decision Table)

The purpose of this rule is to calculate the monthlyPayment and totalPayment values using predefined financial formulas shown in the code snippets below:

3.1.3. APR (Scripting Rule)

The purpose of this scripting rule is to calculate the APR (Annual Percentage Rate) for a loan based on the loan amount, monthly payment, and loan duration. In order to calculate the value of the APR, the scripting rule uses a “Newton's method” algorithm.

You may be asking, what is an APR?

  • Annual Percentage Rate, or APR for short, is an all-inclusive, annualized cost indicator of a loan. It lets you see how much a loan costs in a year. It includes the interest rate and other fees borrowers have to pay. It helps compare loans, so you know which one is cheaper overall.

In the code snippet below, you can see our implementation of the APR calculator scripting rule, accompanied by the individual comments (steps) that walk you through the script and explain what it does in detail:

3.2. Sliders (Rule Flow)

This Rule Flow is responsible for the determination of the values used in the loan and repayment sliders in the web application, such as the minimal value of the loan slider (loanMin), or the maximal value of the repayment slider (repaymentMonthsMax). It determines these values using the loanAmount value, and predefined conditions inside DecisionRules. As you can see in the diagram image above, the Rule Flow is made up of only two Decision Tables.

Description of the Rule Flow diagram:

  1. The Rule Flow receives loanAmount value from the web application.
  2. The loanMin, loanAmount, and loanMax values (values for the loan slider) are determined based on the loanAmount value using the “Loan Slider” decision table.
  3. The repaymentMonthsMin and repaymentMonthsMax values (values for the repayment slider) are determined based on the loanAmount value using the “Repayment Slider” decision table.
  4. The Rule Flow returns the determined values to the web application as an output.

In the code snippet below, you can see a commented example of the input and output model:

3.2.1. Loan Slider (Decision Table)

The purpose of this rule is to determine the loanMin, loanAmount, and loanMax values based on predefined conditions for the loanAmount and loanDuration values. It is also able to determine the "default value" of the loan slider in the web application. This means that when the web application is first loaded, the value of the loan slider will be the value specified in this Loan Slider rule. For instance, if the loanAmount (default value) inside DecisionRules is set to $50,000, then the loan slider will initially show a value of $50,000.

3.2.2. Repayment Slider (Decision Table)

The purpose of this rule is to determine the repaymentMonthsMin and repaymentMonthsMax values based on predefined conditions for the loanAmount and loanDuration values.

3.3. Repayment Schedule

This set of rules is responsible for the generation of the Repayment Schedule for an amortized loan based on the loanAmount, loanDuration, and interestRate values. The repayment schedule contains details such as the beginning balance, principal, interest, and ending balance for each payment period. As you can see in the diagram image above, the Rule Flow is made up of one Decision Tables and one Scripting Rule.

Description of the Rule Flow diagram:

  1. The Rule Flow receives the loanAmount value from the web application.
  2. The calendar array, and the individual values contained in it, are generated from the loanAmount, loanDuration, and interestRate values using the “Repayment Schedule” scripting rule and decision table.
  3. The Rule Flow returns the generated calendar array back to the web application as an output.

In the code snippet below, you can see a commented example of the input and output model:

3.3.1. Repayment Schedule (Scripting Rule)

The purpose of this scripting rule is to generate the Repayment Schedule (calendar array). Each payment period is represented as an object inside the calendar array.

In the code snippet below, you can see our implementation of the Repayment Schedule scripting rule, accompanied by the individual comments (steps) that walk you through the script and explain what it does in detail:

3.3.2. Repayment Schedule (Decision Table)

The purpose of this rule is to calculate the values for each repayment period (schedule rows). Each repayment period contains values such as the beginning balance, principal, interest, and ending balance. These values (except beginning balance) are calculated using the predefined financial formulas shown in the code snippets below:

4. Conclusion

That's all there is to it! With DecisionRules, creating and implementing a complex logic for this loan calculator application is a breeze. In conclusion, the Loan Calculator application is an excellent example of how DecisionRules can be used to perform complex calculations with ease. It also shows how DecisionRules is a powerful tool that can help you automate many of your decision-making processes and reduce the risk of human error. Overall, DecisionRules provides a comprehensive and intuitive platform for developing and deploying automated decision-making systems.

If you want to learn more about how DecisionRules can help and benefit your business, be sure to visit our website or LinkedIn page. You can also speak with our experts about your specific use case or the capabilities of DecisionRules. Our sales team is also available to answer any questions you may have and would be happy to schedule a demo for you to see DecisionRules in action.

Thanks for reading!

More reading