Exemplo n.º 1
0
 private List portfolionames() {
   Session session = HibernateUtil.getSessionFactory().openSession();
   Query q_pname = session.getNamedQuery("Portfolioname.findAll");
   List l_pname = q_pname.list();
   session.close();
   return l_pname;
 }
Exemplo n.º 2
0
 /**
  * Set as current portfolio the one having the specified name.
  *
  * @param portfolioname portfolio name
  */
 public void setPortfolioname(String portfolioname) {
   this.portfolioname = new Portfolioname(portfolioname);
   Session session = HibernateUtil.getSessionFactory().openSession();
   session.beginTransaction();
   session.save(this.portfolioname);
   session.getTransaction().commit();
   session.close();
 }
Exemplo n.º 3
0
 /**
  * Update database with new operation.
  *
  * @param pname portfolio name
  * @param iname instrument name
  * @param amount portfolio amount
  * @param otype operation type: 's' or 'b'
  */
 public final void InstrumentOperation(String pname, String iname, Integer amount, String otype) {
   Session session = HibernateUtil.getSessionFactory().openSession();
   Query q_pname = session.getNamedQuery("Portfolioname.findByName").setParameter("name", pname);
   Portfolioname o_pname = (Portfolioname) q_pname.list().get(0);
   Query q_instr =
       session.getNamedQuery("Instrument.findByDescription").setParameter("desc", iname);
   Instrument instr = (Instrument) q_instr.list().get(0);
   Portfolio portfolio = new Portfolio(o_pname, otype.charAt(0), getLastDate(instr), amount);
   portfolio.setInstrument(instr);
   session.beginTransaction();
   session.save(portfolio);
   session.getTransaction().commit();
   session.close();
 }
Exemplo n.º 4
0
 /** Create a new portfolio. */
 public final void BuyItems() {
   Instrument[] instruments = optimization.getHistoricalSeries().getInstruments();
   int[][] quoteAmounts = optimization.getQuoteAmounts();
   for (int i = 0; i < instruments.length; i++) {
     if (quoteAmounts[selectedPtf][i] > 0) {
       Session session = HibernateUtil.getSessionFactory().openSession();
       Portfolio ptf =
           new Portfolio(
               portfolioname, 'b', getLastDate(instruments[i]), quoteAmounts[selectedPtf][i]);
       ptf.setInstrument(instruments[i]);
       session.beginTransaction();
       session.save(ptf);
       session.getTransaction().commit();
       session.close();
     }
   }
 }
Exemplo n.º 5
0
  /**
   * Extract portfolio information.
   *
   * @param name portfolio name
   * @return Portfolio information in <a>RawTable</a> container.
   */
  public RawTable GetPortfolio(String name) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Query q_port = session.getNamedQuery("Portfolioname.findByName").setParameter("name", name);
    portfolioname = (Portfolioname) q_port.list().get(0);
    Object[] l_pnames = portfolioname.getPortfolios().toArray();

    // Used instruments
    List<Instrument> portfolioInstruments = new ArrayList<>();
    for (Object l_pname : l_pnames) {
      Instrument instrument = ((Portfolio) l_pname).getOperationDate().getInstrument();
      if (!portfolioInstruments.contains(instrument)) portfolioInstruments.add(instrument);
    }

    // Start dates and prices
    History[] startDate = new History[portfolioInstruments.size()];
    for (Object l_pname : l_pnames) {
      Portfolio portfolio = (Portfolio) l_pname;
      int indexOf = portfolioInstruments.indexOf(portfolio.getOperationDate().getInstrument());
      if (startDate[indexOf] == null) {
        startDate[indexOf] = portfolio.getOperationDate();
      } else if (startDate[indexOf]
              .getInstrument()
              .equals(portfolio.getOperationDate().getInstrument())
          && startDate[indexOf].getHdate().after(portfolio.getOperationDate().getHdate())) {
        startDate[indexOf] = portfolio.getOperationDate();
      }
    }

    // NOTE: multiple operations on startDate are cumulated.
    int[] amounts = new int[portfolioInstruments.size()];
    Double portfolioStartValue = 0.0;
    for (Object l_pname : l_pnames) {
      Portfolio p = (Portfolio) l_pname;
      int indexOf = portfolioInstruments.indexOf(p.getOperationDate().getInstrument());
      if (p.getOperationDate().equals(startDate[indexOf])) {
        if (p.getBuyOrSell() == 'b') {
          amounts[indexOf] += p.getAmount();
          portfolioStartValue += p.getAmount() * p.getOperationDate().getPrice();
          // Add transaction costs
          portfolioStartValue += 19.0; // TODO Parametrize transaction costs
        } else {
          amounts[indexOf] -= p.getAmount();
          portfolioStartValue -= p.getAmount() * p.getOperationDate().getPrice();
        }
      }
    }
    // Re-run for updating amounts on newer operations
    double realizedGains = 0.0;
    double realizedLosses = 0.0;
    for (Object l_pname : l_pnames) {
      Portfolio p = (Portfolio) l_pname;
      int indexOf = portfolioInstruments.indexOf(p.getOperationDate().getInstrument());
      double currentPrice = p.getOperationDate().getPrice();
      // NOTE: multiple operations on startDate are not elaborated here.
      if (!p.getOperationDate().equals(startDate[indexOf])) {
        // Re-allocate amounts and update initial portfolio value
        if (p.getBuyOrSell() == 'b') {
          amounts[indexOf] += p.getAmount();
          portfolioStartValue += p.getAmount() * currentPrice;
        } else {
          amounts[indexOf] -= p.getAmount();
          portfolioStartValue -= p.getAmount() * startDate[indexOf].getPrice();
          if (amounts[indexOf] == 0) {
            double delta = currentPrice - startDate[indexOf].getPrice();
            if (delta > 0.0) realizedGains += p.getAmount() * delta;
            else realizedLosses += p.getAmount() * delta;
          }
        }
      }
    }
    // Current dates and prices
    History[] currentDate = new History[portfolioInstruments.size()];
    Double portfolioCurrentValue = 0.0;
    int nullAmounts = 0;
    for (int i = 0; i < portfolioInstruments.size(); i++) {
      currentDate[i] = getLastDate(portfolioInstruments.get(i));
      if (amounts[i] > 0) portfolioCurrentValue += amounts[i] * currentDate[i].getPrice();
      else nullAmounts++;
    }

    int n = portfolioInstruments.size() - nullAmounts + 3;
    Object[][] data = new Object[n][];
    nullAmounts = 0;
    for (int i = 0; i < portfolioInstruments.size(); i++) {
      if (amounts[i] == 0) {
        nullAmounts++;
        continue;
      }

      int m = i - nullAmounts;
      data[m] = new Object[RawTable.headers.length];
      data[m][0] = portfolioInstruments.get(i).getDescription();
      data[m][1] = portfolioInstruments.get(i).getName();
      data[m][2] = startDate[i].getHdate();
      data[m][3] = startDate[i].getPrice();
      data[m][4] = amounts[i];
      data[m][5] = currentDate[i].getPrice();
      data[m][6] =
          String.format("%.2f%%", 100.0 * ((Double) data[m][5] / (Double) data[m][3] - 1.0));
    }

    data[portfolioInstruments.size() - nullAmounts] = new Object[RawTable.headers.length];
    data[portfolioInstruments.size() - nullAmounts][0] = "Portfolio";
    data[portfolioInstruments.size() - nullAmounts][1] = portfolioname.getName();
    data[portfolioInstruments.size() - nullAmounts][2] = data[0][2];
    data[portfolioInstruments.size() - nullAmounts][3] = portfolioStartValue;
    data[portfolioInstruments.size() - nullAmounts][4] = 1;
    data[portfolioInstruments.size() - nullAmounts][5] = portfolioCurrentValue;
    data[portfolioInstruments.size() - nullAmounts][6] =
        String.format("%.2f%%", 100.0 * (portfolioCurrentValue / portfolioStartValue - 1.0));

    data[portfolioInstruments.size() - nullAmounts + 1] = new Object[RawTable.headers.length];
    data[portfolioInstruments.size() - nullAmounts + 1][0] = "Gains";
    data[portfolioInstruments.size() - nullAmounts + 1][3] = realizedGains;

    data[portfolioInstruments.size() - nullAmounts + 2] = new Object[RawTable.headers.length];
    data[portfolioInstruments.size() - nullAmounts + 2][0] = "Losses";
    data[portfolioInstruments.size() - nullAmounts + 2][3] = realizedLosses;

    return new RawTable(data);
  }