|
| 1 | +import { useState } from "react"; |
| 2 | + |
| 3 | +interface ROIResult { |
| 4 | + totalReturn: number; |
| 5 | + annualYield: number; |
| 6 | + breakEvenMonths: number; |
| 7 | +} |
| 8 | + |
| 9 | +const SP500_ANNUAL_RATE = 0.1; |
| 10 | + |
| 11 | +function calcROI(amount: number, months: number, annualRate = 0.08): ROIResult { |
| 12 | + const years = months / 12; |
| 13 | + const totalReturn = amount * Math.pow(1 + annualRate, years) - amount; |
| 14 | + const annualYield = annualRate * 100; |
| 15 | + const breakEvenMonths = Math.ceil(Math.log(2) / Math.log(1 + annualRate) * 12); |
| 16 | + return { totalReturn, annualYield, breakEvenMonths }; |
| 17 | +} |
| 18 | + |
| 19 | +export default function ROICalculator() { |
| 20 | + const [amount, setAmount] = useState(10000); |
| 21 | + const [months, setMonths] = useState(12); |
| 22 | + |
| 23 | + const roi = calcROI(amount, months); |
| 24 | + const sp500 = calcROI(amount, months, SP500_ANNUAL_RATE); |
| 25 | + |
| 26 | + return ( |
| 27 | + <div className="p-4 border rounded-xl space-y-4"> |
| 28 | + <h3 className="font-semibold text-lg">ROI Calculator</h3> |
| 29 | + |
| 30 | + <div className="flex gap-4"> |
| 31 | + <label className="flex flex-col text-sm"> |
| 32 | + Investment (USD) |
| 33 | + <input |
| 34 | + type="number" |
| 35 | + value={amount} |
| 36 | + onChange={(e) => setAmount(Number(e.target.value))} |
| 37 | + className="border rounded px-2 py-1 mt-1" |
| 38 | + /> |
| 39 | + </label> |
| 40 | + <label className="flex flex-col text-sm"> |
| 41 | + Holding Period (months) |
| 42 | + <input |
| 43 | + type="number" |
| 44 | + value={months} |
| 45 | + onChange={(e) => setMonths(Number(e.target.value))} |
| 46 | + className="border rounded px-2 py-1 mt-1" |
| 47 | + /> |
| 48 | + </label> |
| 49 | + </div> |
| 50 | + |
| 51 | + <div className="grid grid-cols-3 gap-2 text-center text-sm"> |
| 52 | + <div className="bg-green-50 rounded p-2"> |
| 53 | + <p className="font-medium">Total Return</p> |
| 54 | + <p className="text-green-700">${roi.totalReturn.toFixed(2)}</p> |
| 55 | + </div> |
| 56 | + <div className="bg-blue-50 rounded p-2"> |
| 57 | + <p className="font-medium">Annual Yield</p> |
| 58 | + <p className="text-blue-700">{roi.annualYield.toFixed(1)}%</p> |
| 59 | + </div> |
| 60 | + <div className="bg-yellow-50 rounded p-2"> |
| 61 | + <p className="font-medium">Break-even</p> |
| 62 | + <p className="text-yellow-700">{roi.breakEvenMonths}mo</p> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + |
| 66 | + <p className="text-xs text-gray-500"> |
| 67 | + S&P 500 equivalent return: <strong>${sp500.totalReturn.toFixed(2)}</strong> |
| 68 | + </p> |
| 69 | + </div> |
| 70 | + ); |
| 71 | +} |
0 commit comments