From 659af3e57dcdad18459ee369ff77cae8fda93481 Mon Sep 17 00:00:00 2001 From: Shraddha Singh Date: Wed, 9 Aug 2023 07:42:26 +0530 Subject: [PATCH] Investment assest allocation --- DOCUMENTATION.md | 5 +++ ENDPOINTS.md | 19 ++++++++++ helpers/functions.py | 16 +++++++++ main.py | 14 +++++++- tasks/Investment_Assest_Allocation.py | 51 +++++++++++++++++++++++++++ validators/request_validators.py | 7 ++++ 6 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 tasks/Investment_Assest_Allocation.py diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index d2a4cbc..146ee3e 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -82,3 +82,8 @@ The initial price of the product or service. | | POST /margin_of_safety | Calculate margin of safety | - `current_sales` (float): The amount of current sales. | | | | - `break_even_point` (float): The break_even_point amount. | |--------------------------- ---|----------------------------------------|---------------------------------------------------------| +| POST /investment_assest_allocation | Calculate Investment Assest Allocation | - `risk_tolerance` (String): The degree of willingness and capacity to endure potential investment losses in pursuit of higher returns. | +| | | - `investment_goals` (String): Specific financial objectives an investor aims to achieve through their investment. | +| | | - `age` (int): The current age of the investor. | +| | | - `market_outlook` (String): An assessment of the anticipated direction. | +|--------------------------- ---|----------------------------------------|---------------------------------------------------------| \ No newline at end of file diff --git a/ENDPOINTS.md b/ENDPOINTS.md index be417c0..ff44086 100644 --- a/ENDPOINTS.md +++ b/ENDPOINTS.md @@ -2362,3 +2362,22 @@ Sample Output "Margin Of Safety": 8%, } ``` +**POST** `/investment_assest_allocation` + +- Request body : `{ + "risk_tolerance": moderate, + "investment_goals": retirement, + "age":30 + "market_outlook":bullish +}` +- Sample output + +```py +{ + "Tag": "Investment Assest Allocation", + "expected_return": 50000, + "risk_level": 46000, + "diversification_metrics": 0.08, + "rebalancing_strategy": calendar-based, +} +``` \ No newline at end of file diff --git a/helpers/functions.py b/helpers/functions.py index ebfbbb7..c967ceb 100644 --- a/helpers/functions.py +++ b/helpers/functions.py @@ -2112,3 +2112,19 @@ def tax_bracket_calculator(income:float, filing_status:str): def margin_of_safety(current_sales:float, break_even_point: float): margin = ((current_sales - break_even_point) / current_sales) * 100 return margin + +# Function to Calculate Investment Assest Allocation + +def investment_assest_allocation(risk_tolerance:str, investment_goals:str, age:int, market_outlook:str): + allocation = { + "conservative": {"stocks": 30, "bonds": 60, "cash": 10}, + "moderate": {"stocks": 50, "bonds": 40, "cash": 10}, + "aggressive": {"stocks": 70, "bonds": 20, "cash": 10} + } + + allocation_mix = allocation.get(risk_tolerance) + + total_allocation = sum(allocation_mix.values()) + allocation_ratio = {asset_class: (allocation / total_allocation) for asset_class, allocation in allocation_mix.items()} + + return allocation_ratio diff --git a/main.py b/main.py index 2f5cefc..cc0b33e 100644 --- a/main.py +++ b/main.py @@ -135,7 +135,7 @@ from tasks.cash_conversion_cycle import cash_conversion_cycle_task from tasks.financialAssestRatio import financial_assest_ratio from tasks.PolicyPremium import calculate_policy_premium -from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, futureValueOfAnnuityDue, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, CashConversionCycle, RateofReturn, financialAssestRatio, PriceElasticity, PolicyPremium, AveragePaymentPeriod, ModifiedInternalRateOfReturn, SavingGoal, InterestCoverageRatio, MarginOfSafety, TaxBracketCalculator +from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, futureValueOfAnnuityDue, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, CashConversionCycle, RateofReturn, financialAssestRatio, PriceElasticity, PolicyPremium, AveragePaymentPeriod, ModifiedInternalRateOfReturn, SavingGoal, InterestCoverageRatio, MarginOfSafety, TaxBracketCalculator, InvestmentAssestAllocation from tasks.financialAssestRatio import financial_assest_ratio from tasks.PriceElasticity import calculate_price_elasticity from tasks.average_payment_period import average_payment_period_task @@ -144,6 +144,7 @@ from tasks.interest_coverage_ratio import interest_coverage_ratio_task from tasks.tax_bracket_calculator import tax_bracket_calculator from tasks.margin_of_safety import margin_of_safety_task +from tasks.Investment_Assest_Allocation import investment_assest_allocation # Creating the app app = FastAPI( @@ -2033,3 +2034,14 @@ def tax_bracket_calculator(request: TaxBracketCalculator): ) def margin_of_safety(request: MarginOfSafety): return margin_of_safety_task(request.current_sales, request.break_even_point) + +# Endpoint to calculate Investment Assest Allocation + +@app.post( + "/investment_assest_allocation", + tags=["investment_assest_allocation"], + description="Calculates Investment Assest Allocation", +) +def investment_assest_allocation(request: InvestmentAssestAllocation): + return investment_assest_allocation(request.risk_tolerance, request.investment_goals, + request.age, request.market_outlook) \ No newline at end of file diff --git a/tasks/Investment_Assest_Allocation.py b/tasks/Investment_Assest_Allocation.py new file mode 100644 index 0000000..204c5f1 --- /dev/null +++ b/tasks/Investment_Assest_Allocation.py @@ -0,0 +1,51 @@ +from helpers import functions +from fastapi import HTTPException, status + +def investment_assest_allocation( + risk_tolerance: str, + investment_goals: str, + age: int, + market_outlook: str +): + try: + if risk_tolerance == "conservative": + allocation = {"stocks": 30, "bonds": 60, "cash": 10} + elif risk_tolerance == "moderate": + allocation = {"stocks": 50, "bonds": 40, "cash": 10} + else: + allocation = {"stocks": 70, "bonds": 20, "cash": 10} + + if investment_goals == "retirement": + expected_return = 0.08 + else: + expected_return = 0.1 + + if market_outlook == "bullish": + expected_return += 0.02 + elif market_outlook == "bearish": + expected_return -= 0.02 + + if age < 30: + risk_level = "low" + elif age < 50: + risk_level = "moderate" + else: + risk_level = "high" + + correlation_coefficient = 0.5 + num_unique_holdings = 25 + + rebalancing_strategy = "calendar-based" + return { + "Tag": "Investment Assest Allocation", + "asset_allocation": allocation, + "expected_return": expected_return, + "risk_level": risk_level, + "diversification_metrics": { + "correlation_coefficient": correlation_coefficient, + "num_unique_holdings": num_unique_holdings, + }, + "rebalancing_strategy": rebalancing_strategy, + } + 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 96bd688..460f0a0 100644 --- a/validators/request_validators.py +++ b/validators/request_validators.py @@ -673,3 +673,10 @@ class TaxBracketCalculator(BaseModel): class MarginOfSafety(BaseModel): current_sales:float break_even_point: float + +class InvestmentAssestAllocation(BaseModel): + risk_tolerance: str + investment_goals: str + time_horizon: float + age: int + market_outlook: str \ No newline at end of file