From 59dd3db4fd5e121b0205863525d15101c64fc22e Mon Sep 17 00:00:00 2001 From: Shraddha Singh Date: Wed, 12 Jul 2023 19:36:14 +0530 Subject: [PATCH 1/2] Rate of return endpoint is added --- DOCUMENTATION.md | 9 ++++++++- ENDPOINTS.md | 21 ++++++++++++++++++++- helpers/functions.py | 5 +++++ main.py | 16 +++++++++++++++- tasks/RateofReturn.py | 25 +++++++++++++++++++++++++ validators/request_validators.py | 9 ++++++++- 6 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 tasks/RateofReturn.py diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 8004f7ee..eee1d9d0 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -25,4 +25,11 @@ | | | - `marketable_securities` (float): The amount of marketable_securities.| | | | - `net_receivables` (float): The amount of net_receivables.| | | | - `annual_operating_expenses` (float): The amount of annual_operating_expenses.| -| | | - `non_cash_charges` (float): The amount of non cash charges.| \ No newline at end of file +| | | - `non_cash_charges` (float): The amount of non cash charges.| +|-----------------------------|----------------------------------------------|-----------------------------------------------------------| +| post /rate_of_return | Calculate Rate of Return | - `initial_investment` (float): Initial amount invested. | +| | | - `final_value` (float): the value of the investment at the end of the investment. | +| | | - `time_period` (float): The number of months. | +| | | - `cash_flows` (float): A list of cash flows over the investment period. | +| | | - `holding_period` (float): The specific holding period of the investment. | +| --------------------------- | ---------------------------------------- | --------------------------------------------------------- | \ No newline at end of file diff --git a/ENDPOINTS.md b/ENDPOINTS.md index d8d581cb..80451a2f 100644 --- a/ENDPOINTS.md +++ b/ENDPOINTS.md @@ -2130,4 +2130,23 @@ Sample Output "Daily Operational Expenses": 753.42, "Defensive Interval Ratio": 92.90 } -``` \ No newline at end of file +``` +**POST** `/rate_return_calculator` + +- Request body : `{ + "initial_investment": 10000, + "final_value": 12500, + "time_period": 3, + "cash_flows": [500, 800, -200], + "holding_period": 2, +}` +- Sample output + +```py +{ + "Tag": "Rate of return", + "rate_of_return": 25.0, + "annualized_return": 8.333333333333334, + "holding_period_return": 50.0, +} +``` \ No newline at end of file diff --git a/helpers/functions.py b/helpers/functions.py index 97358a5b..397f42ac 100644 --- a/helpers/functions.py +++ b/helpers/functions.py @@ -1982,3 +1982,8 @@ def defensive_interval_ratio(cash: float, marketable_securities: float, ratio = current_assets / daily_operational_expenses return ratio +# Function to Calculate Debt Service Coverage Ratio. + +def rate_of_return(initial_investment: float, final_value: float ): + rate_of_return = ((final_value - initial_investment) / initial_investment) * 100 + return rate_of_return \ No newline at end of file diff --git a/main.py b/main.py index c4dd1afc..b0f13058 100644 --- a/main.py +++ b/main.py @@ -131,7 +131,8 @@ from tasks.profit_percentage import profit_percentage_task from tasks.loss_percentage import loss_percentage_task from tasks.defensive_interval_ratio import defensive_interval_ratio_task -from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, +from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, RateofReturn +from tasks.RateofReturn import calculate_rate_of_return # Creating the app app = FastAPI( @@ -1877,3 +1878,16 @@ def defensive_interval_ratio(request: DefensiveIntervalRatio): return defensive_interval_ratio_task(request.cash, request.marketable_securities, request.net_receivables, request.annual_operating_expenses , request.non_cash_charges) +# Endpoint to calculate Rate of return + +@app.post( + "/rate_of_return", + tags=["rate_of_return"], + description="Calculate Rate of return", +) +def rate_of_return(request: RateofReturn): + return calculate_rate_of_return(request.initial_investment, + request.final_value, + request.cash_flows, + request.time_period, + request.holding_period) \ No newline at end of file diff --git a/tasks/RateofReturn.py b/tasks/RateofReturn.py new file mode 100644 index 00000000..c3c04429 --- /dev/null +++ b/tasks/RateofReturn.py @@ -0,0 +1,25 @@ +from helpers import functions +from fastapi import HTTPException, status + +def calculate_rate_of_return(initial_investment: float, final_value: float, cash_flows: float, +time_period: float, holding_period: float): + try: + net_cash_flows = sum(cash_flows) + final_value += net_cash_flows + + if holding_period > 0: + rate_of_return = ((final_value - initial_investment) / (initial_investment + net_cash_flows)) * 100 + holding_period_return = ((final_value - initial_investment) / initial_investment) * 100 + else: + rate_of_return = ((final_value - initial_investment) / initial_investment) * 100 + holding_period_return = rate_of_return + + annualized_return = rate_of_return / time_period + return { + "Tag": "Rate of return", + "rate_of_return": rate_of_return, + "annualized_return": annualized_return, + "holding_period_return": holding_period_return + } + except: + return HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) \ No newline at end of file diff --git a/validators/request_validators.py b/validators/request_validators.py index 076ddd24..9399e1da 100644 --- a/validators/request_validators.py +++ b/validators/request_validators.py @@ -602,4 +602,11 @@ class DefensiveIntervalRatio(BaseModel): marketable_securities: float net_receivables: float annual_operating_expenses: float - non_cash_charges: float \ No newline at end of file + non_cash_charges: float + +class RateofReturn(BaseModel): + initial_investment: float + final_value: float + cash_flows: float + time_period: float + holding_period: float \ No newline at end of file From 273c2cc8520ad9046c173dc1bfdef852a752329b Mon Sep 17 00:00:00 2001 From: Subho Ghosh <93722719+ighoshsubho@users.noreply.github.com> Date: Wed, 12 Jul 2023 19:53:31 +0530 Subject: [PATCH 2/2] Update main.py --- main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.py b/main.py index 14911a0b..89b4f59f 100644 --- a/main.py +++ b/main.py @@ -131,9 +131,8 @@ from tasks.profit_percentage import profit_percentage_task from tasks.loss_percentage import loss_percentage_task from tasks.defensive_interval_ratio import defensive_interval_ratio_task -from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, RateofReturn from tasks.RateofReturn import calculate_rate_of_return -from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, futureValueOfAnnuityDue, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, financialAssestRatio +from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, futureValueOfAnnuityDue, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, financialAssestRatio, RateofReturn from tasks.financialAssestRatio import financial_assest_ratio # Creating the app