From c7643083599e924383e538f2c3bc6d768d335125 Mon Sep 17 00:00:00 2001 From: DyuthiVivek Date: Sun, 30 Jul 2023 21:04:29 +0530 Subject: [PATCH] added fd payout calculator --- DOCUMENTATION.md | 5 +++++ ENDPOINTS.md | 24 ++++++++++++++++++++++++ helpers/functions.py | 10 ++++++++++ main.py | 14 ++++++++++++++ tasks/fd_payout.py | 25 +++++++++++++++++++++++++ validators/request_validators.py | 6 ++++++ 6 files changed, 84 insertions(+) create mode 100644 tasks/fd_payout.py diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 0542f4d..6d15280 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -71,3 +71,8 @@ The initial price of the product or service. | | | | - `interest_rate` (float): The annual interest rate on the savings. | | | | - `goal_amount ` (float): The desired savings goal amount. | |-----------------------------|----------------------------------------|----------------------------------------------------------------------| +| GET /fd_payout | Calculate fd payout monthly vs yearly vs cumulative | - `p` (int): principal | +| | | - `t` (int): term in years | +| | | - `r` (float): rate in decimals | +| | | - `c` (int): number of months after which to compund | +|-----------------------------|----------------------------------------|----------------------------------------------------------------------| diff --git a/ENDPOINTS.md b/ENDPOINTS.md index 9a3aeac..198ac9b 100644 --- a/ENDPOINTS.md +++ b/ENDPOINTS.md @@ -2306,3 +2306,27 @@ Sample Output "total_contributions": 3600, "interest_earned": 315.27777777777777 }``` + +``` + +**POST** `/fd_payout` +- Request body : `p`, + `t`, + `r`, + `c`, + +- Sample output +```py +{ + { + "Tag": "fd_payout", + "Principal": 1000, + "Term (in years)": 2, + "Rate (as a decimal)" : 0.04, + "Compounding period" : 6, + "Payout_monthly": 80, + "Payout_yearly": 81, + "Payout_cumulative": 82, + } +} +``` diff --git a/helpers/functions.py b/helpers/functions.py index acad33d..7740501 100644 --- a/helpers/functions.py +++ b/helpers/functions.py @@ -2068,3 +2068,13 @@ def calculate_modified_internal_rate_of_return(ending_cash_flow: float, number_of_periods: int): mirr = ((ending_cash_flow / initial_cash_flow) ** (1 / number_of_periods)) - 1 return mirr*100 + +# Function to calculate fixed deposit payout for monthly, yearly, cumulative based on given compounding period +def fd_payout(p : float, t : int, r : float, c : int) -> list[int]: + c = 12//c + interest_monthly = p * t * r + amount_yearly = p * ((1 + r/c)**c) + interest_yearly = (amount_yearly - p) * t + amount_cumulative = p * ((1 + r/c)**(c * t)) + interest_cumulative = amount_cumulative - p + return [round(interest_monthly), round(interest_yearly), round(interest_cumulative)] diff --git a/main.py b/main.py index 6382da1..3e48849 100644 --- a/main.py +++ b/main.py @@ -1996,3 +1996,17 @@ def saving_goal(request: SavingGoal): request.monthly_contributions , request.interest_rate, request.goal_amount ) + + +# Endpoint for function fd_payout + +@app.post( + "/fd_payout", + tags=["fd_payout"], + description="Calculate monthly, yearly and cumulative fixed deposit payout", +) +def fd_payout( + request: FdPayout +): + return fd_payout(request.p, request.t, request.r, request.c) + diff --git a/tasks/fd_payout.py b/tasks/fd_payout.py new file mode 100644 index 0000000..209d700 --- /dev/null +++ b/tasks/fd_payout.py @@ -0,0 +1,25 @@ +def fd_payout( + p : float, t : int, r : float, c : int +): + + ''' + Calculate fixed deposit + Inputs: principal, term in years, rate as a decimal, compunding period(compund after how many months) + Ouput: fixed deposit payout for monthly, yearly and cumulative compounding + ''' + try: + payout = functions.fd_payout( + p, t, r, c + ) + return { + "Tag": "fd_payout", + "Principal": p, + "Term (in years)": t, + "Rate (as a decimal)" : r, + "Compounding period" : c, + "Payout_monthly": payout[0], + "Payout_yearly": payout[1], + "Payout_cumulative": payout[2], + } + except: + return HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/validators/request_validators.py b/validators/request_validators.py index b58f3c4..9a89edd 100644 --- a/validators/request_validators.py +++ b/validators/request_validators.py @@ -659,3 +659,9 @@ class ModifiedInternalRateOfReturn(BaseModel): ending_cash_flow: float initial_cash_flow: float number_of_periods: int + +class FdPayout(BaseModel): + p : int + t : int + r : float + c : int