Created
March 5, 2022 22:05
-
-
Save ApprenticeofEnder/e9b80b95cbd0adbfd8130c6ccf00bc2c to your computer and use it in GitHub Desktop.
IRR Calculator (Finance)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def calculate_irr(initial_investment, payments): | |
| min_irr = 0 | |
| max_irr = 0.50 | |
| irr = 0 | |
| while True: | |
| result = initial_investment | |
| mid_irr = (min_irr + max_irr) / 2 | |
| for index, payment in enumerate(payments): | |
| result += payment / ((1 + mid_irr) ** (index + 1)) | |
| if abs(result) < 0.005: | |
| irr = mid_irr | |
| break | |
| if result > 0: | |
| min_irr = mid_irr | |
| elif result < 0: | |
| max_irr = mid_irr | |
| return irr | |
| def main(): | |
| try: | |
| initial_investment = -1 * abs(round(float(input("Enter initial investment: ")),2)) | |
| num_payments = int(input("Enter number of payments: ")) | |
| payments = [] | |
| if num_payments <= 0: | |
| raise "Invalid payments" | |
| for i in range(num_payments): | |
| payments.append(round(float(input(f"Enter payment {i+1}:")),2)) | |
| irr = calculate_irr(initial_investment, payments) | |
| print(f"Your IRR is: {round(irr * 100, 2)}%") | |
| except: | |
| print("Invalid input, please try again.") | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment