home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / ODesign / SetupPSE.exe / data.z / Portfolio.java < prev    next >
Encoding:
Java Source  |  1997-04-23  |  4.9 KB  |  211 lines

  1. package COM.odi.demo.pport;
  2.  
  3. /**
  4.  *      <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
  5.  *
  6.  * A Portfolio contains a sequence of trades, over time, each of
  7.  * which is a purchase or sale of some number of some security on
  8.  * some date.
  9.  */
  10.  
  11. import java.io.*;
  12. import java.util.*;
  13. import COM.odi.*;
  14. import COM.odi.util.*;
  15.  
  16. final
  17. public class Portfolio
  18. {
  19.   /* The name. */
  20.  
  21.   String name;
  22.  
  23.   /* The sequence of trades. */
  24.  
  25.   private OSVector trades;
  26.  
  27.   /* Hash table of all Portfolio objects, keyed by symbol. */
  28.  
  29.   static OSHashtable all = new OSHashtable();
  30.  
  31.  
  32.   Portfolio(String name)
  33.   {
  34.     this.name = name;
  35.     trades = new OSVector();
  36.     all.put(name, this);
  37.   }
  38.  
  39.   /**
  40.    * Get the Portfolio with the given name.  If there isn't one,
  41.    * create one automatically and return it.
  42.    * @param name The name of the Portfolio to get.
  43.    */
  44.   static Portfolio get(String name)
  45.   {
  46.     Portfolio result = (Portfolio)all.get(name);
  47.     if (result == null) {
  48.       result = new Portfolio(name);
  49.  
  50.       /* Temporary, for debugging */
  51.       System.out.println("New portfolio " + name);
  52.  
  53.     }
  54.     return result;
  55.   }
  56.  
  57.   /**
  58.    * Get the Portfolio with the given name.  If there isn't one,
  59.    * throw Error.
  60.    * @param name The name of the Portfolio to get.
  61.    */
  62.   static Portfolio lookup(String name)
  63.   {
  64.     Portfolio result = (Portfolio)all.get(name);
  65.     if (result == null) {
  66.       throw new Error("Portfolio not found");
  67.     }
  68.     return result;
  69.   }
  70.  
  71.   /**
  72.    * Add a trade to the Portfolio's list of trades.
  73.    * @param trade The trade to add.
  74.    */
  75.   void add(Trade trade) {
  76.     trades.addElement(trade);
  77.   }
  78.  
  79.   /**
  80.    * Compute the total "net asset value" of the portfolio for each of
  81.    * the days within the specified sequence.
  82.    * @param startDate The first date of the sequence.
  83.    * @param endDate The last date of the sequence.
  84.    * @return An array with one element per day of the sequence, giving the
  85.    *         total value of the portfolio on each day.
  86.    */
  87.   int[] computeNAV(TradeDate startDate, TradeDate endDate)
  88.   {
  89.     Hashtable secPP = new Hashtable();
  90.     int [] result = new int[startDate.daysUntil(endDate) + 1];
  91.  
  92.     TradeDate date = new TradeDate(startDate);
  93.     int relativeDate = 0;
  94.  
  95.     Enumeration tradeEnum = trades.elements();
  96.     Trade trade = (Trade)tradeEnum.nextElement();
  97.  
  98.     while (! date.equals(endDate)) {
  99.  
  100.       /* Update the SecurityPP objects to reflect all trades up to
  101.          the current date. */
  102.  
  103.       while (trade != null) {
  104.  
  105.     if (! trade.date.equals(date))
  106.       break;
  107.  
  108.     /* Get the SecurityPP for this trade's security.  Adjust
  109.        its position by the delta of this trade. */
  110.  
  111.     SecurityPP pp = (SecurityPP)secPP.get(trade.security);
  112.     if (pp == null) {
  113.       pp = new SecurityPP(trade.security, trade.delta);
  114.       secPP.put(trade.security, pp);
  115.     } else {
  116.       pp.position += trade.delta;
  117.     }
  118.  
  119.     /* Update the price of the security for this date.  A price of
  120.        zero can mean that there wasn't any trading this day, so
  121.        the date's price is zero, leave the price unaltered. */
  122.  
  123.     int price = trade.security.getPrice(date);
  124.     if (price > 0)
  125.       pp.price = price;
  126.  
  127.     trade = (Trade)tradeEnum.nextElement();
  128.       }
  129.  
  130.       /* Compute the portfolio's net asset value for this date. */
  131.  
  132.       Enumeration ppEnum = secPP.elements();
  133.       int value = 0;
  134.       while (ppEnum.hasMoreElements()) {
  135.     SecurityPP pp = (SecurityPP)ppEnum.nextElement();
  136.     value += pp.price * pp.position;
  137.       }
  138.       result[relativeDate++] = value;
  139.  
  140.       date.next();
  141.     }
  142.     return result;
  143.   }
  144.  
  145.   /**
  146.    * Load up a new database from the textual portfolios file.
  147.    * @param pathname The pathname of the portfolios file.
  148.    * @param db The database to store the objects into.
  149.    */
  150.   static void load(String pathname, Database db) throws IOException
  151.   {
  152.     BufferedReader stream = null;
  153.     try {
  154.       stream = new BufferedReader(new FileReader(pathname));
  155.       all.clear();
  156.       String line = stream.readLine();
  157.       while (line != null) {
  158.     StringTokenizer tkn = new StringTokenizer(line, "\t");
  159.     Portfolio port = get(tkn.nextToken());
  160.     Security sec = Security.get(tkn.nextToken());
  161.     TradeDate date = new TradeDate(tkn.nextToken());
  162.     int delta = (new Integer(tkn.nextToken())).intValue();
  163.     port.add(new Trade(sec, date, delta));
  164.     line = stream.readLine();
  165.       }
  166.     } finally {
  167.       if (stream != null)
  168.     stream.close();
  169.     }
  170.     db.createRoot("portfolios", all);
  171.   }
  172.  
  173.   static void initialize(Database db)
  174.   {
  175.     all = (OSHashtable)db.getRoot("portfolios");
  176.   }
  177.  
  178.   public String getName() {
  179.     return name;
  180.   }
  181.  
  182.   static public Enumeration getAll() {
  183.     return all.elements();
  184.   }
  185.  
  186.   public OSVector getTrades() {
  187.     return trades;
  188.   }
  189.  
  190.  
  191.   /* This class is never used as a persistent hash key. */
  192.   public int hashCode() {
  193.     return super.hashCode();
  194.   }
  195.  
  196. }
  197.  
  198. class SecurityPP
  199. {
  200.   Security security;
  201.   int position;
  202.   int price;
  203.  
  204.   SecurityPP(Security security, int position)
  205.   {
  206.     this.security = security;
  207.     this.position = position;
  208.   }
  209. }
  210.  
  211.