Module3

mail@pastecode.io avatar
unknown
java
2 years ago
3.2 kB
3
Indexable
Never
 public static List<AnnualizedReturn> mainCalculateSingleReturn(String[] args)
      throws IOException, URISyntaxException {
    if(args.length == 0)
      return Collections.emptyList();

    File file = resolveFileFromResources(args[0]);
    ObjectMapper om = getObjectMapper();
    RestTemplate rt = new RestTemplate();

    List<PortfolioTrade> pt = Arrays.asList(om.readValue(file, PortfolioTrade[].class));

    List<AnnualizedReturn> ar = new ArrayList<>();

    for(PortfolioTrade x: pt){
      LocalDate s = x.getPurchaseDate();
      LocalDate dt = LocalDate.parse(args[1]);
      String symbol = x.getSymbol();
      String url = "https://api.tiingo.com/tiingo/daily/{symbol}/prices?startDate={s}&endDate={dt}&token=0c3030e3c53a205867d9fcbb578eb254e003da23";
      TiingoCandle[] listOfTiingo = rt.getForObject(url, TiingoCandle[].class, symbol, s, dt);
      Double buyPrice  = listOfTiingo[0].getOpen();
      Double sellPrice = listOfTiingo[listOfTiingo.length-1].getClose();

      ar.add(calculateAnnualizedReturns(LocalDate.parse(args[1]), x, buyPrice, sellPrice));
    }

    Collections.sort(ar, new Comparator<AnnualizedReturn>() {
      @Override
      public int compare(AnnualizedReturn o1, AnnualizedReturn o2) {
      //return Double.compare(o1.getClosingPrice() - o2.getClosingPrice());
        double delta = o2.getAnnualizedReturn() - o1.getAnnualizedReturn();
        if(delta > 0.00001) return 1;
        if(delta < -0.00001) return -1;
        return 0;
      }
    });
        
    return ar;
  }

  // todo: CRIO_TASK_MODULE_CALCULATIONS
  //  Return the populated list of AnnualizedReturn for all stocks.
  //  Annualized returns should be calculated in two steps:
  //   1. Calculate totalReturn = (sell_value - buy_value) / buy_value.
  //      1.1 Store the same as totalReturns
  //   2. Calculate extrapolated annualized returns by scaling the same in years span.
  //      The formula is:
  //      annualized_returns = (1 + total_returns) ^ (1 / total_num_years) - 1
  //      2.1 Store the same as annualized_returns
  //  Test the same using below specified command. The build should be successful.
  //     ./gradlew test --tests PortfolioManagerApplicationTest.testCalculateAnnualizedReturn

  public static AnnualizedReturn calculateAnnualizedReturns(LocalDate endDate,
      PortfolioTrade trade, Double buyPrice, Double sellPrice) {

        System.out.println(trade.getPurchaseDate()+" "+endDate+" "+buyPrice+" "+sellPrice);

        Double total_returns = (Double)((sellPrice - buyPrice)/buyPrice);
        LocalDate startDate = trade.getPurchaseDate();
        double total_num_years = ChronoUnit.DAYS.between(startDate, endDate)/365.24;

        System.out.println(total_num_years);

        // System.out.println(total_num_years);
        // System.out.println((double)(1/total_num_years));

        Double annualized_returns = Math.pow((1 + total_returns), (1.0 / total_num_years)) - 1;

        // System.out.println("AR: "+ annualized_returns+ " TNY: "+ total_num_years+" "+ total_returns);

      return new AnnualizedReturn(trade.getSymbol(), annualized_returns, total_returns);
  }