File size: 1,147 Bytes
3baee5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
import uvicorn

app = FastAPI()

# Enable CORS for frontend requests
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Request model
class SIPRequest(BaseModel):
    P: float = 5000  # Monthly investment
    i: float = 12  # Annual interest rate
    n: int = 10  # Number of years

# SIP Calculation Function
def calculate_sip(P: float, i: float, n: int):
    monthly_rate = i / 12 / 100  # Convert annual rate to monthly decimal
    n_payments = n * 12  # Convert years to months
    M = P * (((1 + monthly_rate) ** n_payments - 1) / monthly_rate) * (1 + monthly_rate)
    total_invested = P * n_payments
    estimated_returns = M - total_invested
    return {"final_amount": M, "total_invested": total_invested, "estimated_returns": estimated_returns}

@app.post("/api/sip")
async def sip_api(data: SIPRequest):
    return calculate_sip(data.P, data.i, data.n)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)