Posted By

pimteam on 02/08/12


Tagged

finance Calculator Investment compounding


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

eludnasud


CompCalc


 / Published in: PHP
 

URL: http://sharkinvestor.com/investment-calculator/

This class calculates compounded interest on investment, saving or loan. It allows variable compounding (reinvesting) percentage To do: add different types of loan calculations. Will be useful for all kind of financial calculators and apps. Here's a page where you can see the class used: http://sharkinvestor.com/investment-calculator/

  1. <?php
  2. /* This class calculates compounded interest on investment, saving or loan.
  3.  It allows variable compounding (reinvesting) percentage
  4.  To do: add different types of loan calculations.
  5.  Will be useful for all kind of financial calculators and apps.
  6.  Here's a page where you can see the class used: http://sharkinvestor.com/investment-calculator/ */
  7. class CompCalc
  8. {
  9. // calculates compounded investment
  10. // $num_periods - required, number of compounding periods (usually years or months)
  11. // $investment - required, the initial investment amount
  12. // $interest - required, interest in % for each period
  13. // $reinvest_percentage - optional, now much from the return is reinvested
  14. // $addition_per_period - optional, additional investment made in each period
  15. // @returns array of arrays - one array for each period:
  16. // [period, principal value, amount withdrawn so far, total return so far, total ROI]
  17. // the results should be read as "at the end of period X"
  18. function inv_calculate($num_periods, $investment, $interest,
  19. $reinvest_percentage=100, $addition_per_period=0)
  20. {
  21. // validate
  22. if($num_periods<=0) throw new Exception("Periods should be at least 1");
  23. if($investment<=0) throw new Exception("Investment amount should be bigger than 0");
  24. if(!is_numeric($interest)) throw new Exception("Interest should be a number");
  25. if(!is_numeric($reinvest_percentage)) throw new Exception("Reinvest % should be a number");
  26. if(!is_numeric($addition_per_period)) throw new Exception("Addition should be a number");
  27.  
  28. // convert interest and reinvest_percentage to decimal numbers
  29. $roi=$interest/100;
  30. $cp=$reinvest_percentage/100;
  31.  
  32. // Start the calculations
  33.  
  34. // STEP 1 & 2:
  35. // 1. When the user input specific number of periods, you just go thru them
  36. // 2. for each period calculate: Profit = Principal (Invested amount) X (ROI / 100)
  37. $total_withdrawn=0;
  38. $results=array();
  39.  
  40. for($i = 1 ; $i < ($num_periods+1) ; $i++ )
  41. {
  42. $new_principal=!empty($new_principal)?$new_principal:$investment;
  43. $new_principal=round($new_principal,2);
  44.  
  45. $profit = $new_principal * $roi;
  46. $profit=round($profit,2);
  47.  
  48. // STEP 3:
  49. // 3. Then substract the withdrawn amount.
  50. // This amount is: Withdrawn = Profit X (compounding percentage / 100)
  51. $addition = $profit * $cp;
  52. $withdraw=round(($profit-$addition),2);
  53.  
  54. // totoal return
  55. $total_withdrawn+=$withdraw;
  56.  
  57. // total ROI
  58. $total_roi=($total_withdrawn/$investment)*100;
  59. $total_roi=round($total_roi);
  60.  
  61. $new_principal=$new_principal+$addition;
  62.  
  63. // period adddition
  64. $investment+=$addition_per_period;
  65. $new_principal+=$addition_per_period;
  66.  
  67. $result=array("period"=>$i, "investment_value"=>$new_principal,
  68. "return_for_the_period"=>$withdraw, "total_return"=>$total_withdrawn,
  69. "total_roi"=>$total_roi);
  70. $results[]=$result;
  71. }
  72.  
  73. return $results;
  74. }
  75. }
  76. ?>

Report this snippet  

You need to login to post a comment.