Assignment
Assignment
Assignment
Calculate Mutual Fund Profit
Objective:
Create a class or a function to calculate the profit for a mutual fund investment.
Function/Class Signature:
calculate_profit(scheme_code, start_date, end_date, capital=1000000.0)
Parameters:
scheme_code
(str): The unique scheme code of the mutual fund.start_date
(str – dd-mm-yyyy): The purchase date of the mutual fund.end_date
(str – dd-mm-yyyy): The redemption date of the mutual fund.capital
(float, optional): The initial investment amount (default: 1000000.0).
Example:
Let’s assume you invested ₹1,000,000 in a mutual fund with scheme code 101206 on 26-07-2023 and redeemed it on 18-10-2023. To calculate the profit, follow these steps:
Get the Net Asset Value (NAV) of the mutual fund on both the purchase date (26-07-2023) and redemption date (18-10-2023) using the API: https://api.mfapi.in/mf/101206.
- NAV on 26-07-2023: 3682.28990
- NAV on 18-10-2023: 3737.30240
Calculate the number of units allotted on the purchase date:
Number of units allotted on 26-07-2023 = Initial investment / NAV on 26-07-2023 Number of units allotted on 26-07-2023 = 1000000 / 3682.28990 = 271.57 units
Calculate the value of the units on the redemption date:
Value of 271.57 units as on 18-10-2023 = Number of units allotted on 26-07-2023 * NAV on 18-10-2023 Value of 271.57 units as on 18-10-2023 = 271.57 * 3737.30240 = 1014939.75
Calculate the net profit:
Net Profit = Value on redemption date - Initial investment
Net Profit = 1014939.75 - 1000000 = 14939.75
Task:
- Create a Python function or class that performs these calculations and returns the net profit. Make sure to handle date formatting, API requests, and all necessary calculations within the function or class.
- Create a FastAPI app with single route
/profit
that takes query string paramsscheme_code
,start_date
,end_date
,capital
. The request would essentially will look like/profit?scheme_code=101206& start_date= 26-07-2023&end_date=18-10-2023&capital=1000000.
Use the Python function to calculate profit. - Follow FastAPI best practices mentioned at https://github.com/zhanymkanov/fastapi-best-practices
- Create a public repo on Github, push your code & send the repo url to priyank@koffi.com.
Important:
- If start_date or end_date is not present in the data, use the next available date. So if 22-10-2023 is not available in the data, use 23-10-2023. If 23-10-2023 also not available, use 24-10-2023 and so on.
- You can append scheme_code to https://api.mfapi.in/mf/<scheme_code> to get the data.
- Bonus points for unit tests using pytest.
- Bonus points for using a good software architecture. You can refer Architecture Patterns with Python book.