포트폴리오 서비스(복리 계산기, 수익률 계산기 추가하기!)

·2024년 1월 8일
0

Portfolio Backtest

목록 보기
17/31

오늘 할일

  • 투자를 하다보면 복리 수익률과 수익률을 구하는 경우가 종종 있다.
  • 쉬어가면서 복리 계산기와 수익률 계산기를 추가하자!

수익률 계산기

Controller

    @GetMapping("/ror")
    public String getRor(Model model, @RequestParam double buyPrice, @RequestParam double sellPrice, @RequestParam long quantity) {
        model.addAttribute("ror", (sellPrice - buyPrice) / (double) buyPrice * 100);
        model.addAttribute("profit", (long) ((sellPrice - buyPrice) / (double) buyPrice * 100 * quantity));
        model.addAttribute("buyPrice", buyPrice);
        model.addAttribute("sellPrice", sellPrice);
        model.addAttribute("quantity", quantity);

        return "calculator";
    }
    

Page



복리 계산기

Controller

    @GetMapping("/welfare")
    public String getWelfare(Model model,
                             @RequestParam(defaultValue = "0.0") String price,
                             @RequestParam(defaultValue = "0") String term,
                             @RequestParam(defaultValue = "0.0") String welfareRor) {
        try {
            double priceDouble = Double.parseDouble(price);
            long termLong = Long.parseLong(term);
            double welfareRorDouble = Double.parseDouble(welfareRor);

            model.addAttribute("price", price);
            model.addAttribute("term", term);
            model.addAttribute("welfareRor", welfareRor);

            List<Double> rorList = RorCalculator.getWelfareRorList(welfareRorDouble, termLong);
            List<Double> revenueList = new ArrayList<>();
            List<Double> priceList = new ArrayList<>();

            revenueList.add(priceDouble * (rorList.get(0) / 100));
            priceList.add(priceDouble + revenueList.get(0));

            for(int i = 1 ; i < rorList.size() ; i++){
                revenueList.add((priceDouble * (rorList.get(i) / 100)) - (priceDouble * (rorList.get(i - 1) / 100)));
                priceList.add(priceList.get(i - 1) + revenueList.get(i));
            }

            model.addAttribute("totalProfit", (long) (priceDouble * (rorList.get(rorList.size() - 1) / 100)));
            model.addAttribute("finalAmount", (long) (priceDouble * (1 + rorList.get(rorList.size() - 1) / 100)));

            model.addAttribute("rorList", rorList);
            model.addAttribute("revenueList", revenueList);
            model.addAttribute("priceList", priceList);

        } catch (NumberFormatException e) {
            // 잘못된 입력 형식에 대한 처리
            return "calculator";
        }
        return "calculator";
    }
    

calculator util

    public static List<Double> getWelfareRorList(double welfareRor, long term){
        List<Double> welfareRorList = new ArrayList<>();
        welfareRorList.add(welfareRor);
        for(int i = 1; i < term; i++){
            double calWelfare = (welfareRorList.get(i - 1) / 100 + 1) * (1 + welfareRor / 100) - 1;
            welfareRorList.add(calWelfare * 100);
        }
        return welfareRorList;
    }
    

복리 수익률을 계산하는 함수를 추가하였다.

page



다음에 할일

  • 포트폴리오를 저장할 수 있도록 회원가입과 로그인을 구현해보자!
profile
백엔드 개발자가 꿈인 컴공과

0개의 댓글

관련 채용 정보