home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-23 | 4.9 KB | 211 lines |
- package COM.odi.demo.pport;
-
- /**
- * <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
- *
- * A Portfolio contains a sequence of trades, over time, each of
- * which is a purchase or sale of some number of some security on
- * some date.
- */
-
- import java.io.*;
- import java.util.*;
- import COM.odi.*;
- import COM.odi.util.*;
-
- final
- public class Portfolio
- {
- /* The name. */
-
- String name;
-
- /* The sequence of trades. */
-
- private OSVector trades;
-
- /* Hash table of all Portfolio objects, keyed by symbol. */
-
- static OSHashtable all = new OSHashtable();
-
-
- Portfolio(String name)
- {
- this.name = name;
- trades = new OSVector();
- all.put(name, this);
- }
-
- /**
- * Get the Portfolio with the given name. If there isn't one,
- * create one automatically and return it.
- * @param name The name of the Portfolio to get.
- */
- static Portfolio get(String name)
- {
- Portfolio result = (Portfolio)all.get(name);
- if (result == null) {
- result = new Portfolio(name);
-
- /* Temporary, for debugging */
- System.out.println("New portfolio " + name);
-
- }
- return result;
- }
-
- /**
- * Get the Portfolio with the given name. If there isn't one,
- * throw Error.
- * @param name The name of the Portfolio to get.
- */
- static Portfolio lookup(String name)
- {
- Portfolio result = (Portfolio)all.get(name);
- if (result == null) {
- throw new Error("Portfolio not found");
- }
- return result;
- }
-
- /**
- * Add a trade to the Portfolio's list of trades.
- * @param trade The trade to add.
- */
- void add(Trade trade) {
- trades.addElement(trade);
- }
-
- /**
- * Compute the total "net asset value" of the portfolio for each of
- * the days within the specified sequence.
- * @param startDate The first date of the sequence.
- * @param endDate The last date of the sequence.
- * @return An array with one element per day of the sequence, giving the
- * total value of the portfolio on each day.
- */
- int[] computeNAV(TradeDate startDate, TradeDate endDate)
- {
- Hashtable secPP = new Hashtable();
- int [] result = new int[startDate.daysUntil(endDate) + 1];
-
- TradeDate date = new TradeDate(startDate);
- int relativeDate = 0;
-
- Enumeration tradeEnum = trades.elements();
- Trade trade = (Trade)tradeEnum.nextElement();
-
- while (! date.equals(endDate)) {
-
- /* Update the SecurityPP objects to reflect all trades up to
- the current date. */
-
- while (trade != null) {
-
- if (! trade.date.equals(date))
- break;
-
- /* Get the SecurityPP for this trade's security. Adjust
- its position by the delta of this trade. */
-
- SecurityPP pp = (SecurityPP)secPP.get(trade.security);
- if (pp == null) {
- pp = new SecurityPP(trade.security, trade.delta);
- secPP.put(trade.security, pp);
- } else {
- pp.position += trade.delta;
- }
-
- /* Update the price of the security for this date. A price of
- zero can mean that there wasn't any trading this day, so
- the date's price is zero, leave the price unaltered. */
-
- int price = trade.security.getPrice(date);
- if (price > 0)
- pp.price = price;
-
- trade = (Trade)tradeEnum.nextElement();
- }
-
- /* Compute the portfolio's net asset value for this date. */
-
- Enumeration ppEnum = secPP.elements();
- int value = 0;
- while (ppEnum.hasMoreElements()) {
- SecurityPP pp = (SecurityPP)ppEnum.nextElement();
- value += pp.price * pp.position;
- }
- result[relativeDate++] = value;
-
- date.next();
- }
- return result;
- }
-
- /**
- * Load up a new database from the textual portfolios file.
- * @param pathname The pathname of the portfolios file.
- * @param db The database to store the objects into.
- */
- static void load(String pathname, Database db) throws IOException
- {
- BufferedReader stream = null;
- try {
- stream = new BufferedReader(new FileReader(pathname));
- all.clear();
- String line = stream.readLine();
- while (line != null) {
- StringTokenizer tkn = new StringTokenizer(line, "\t");
- Portfolio port = get(tkn.nextToken());
- Security sec = Security.get(tkn.nextToken());
- TradeDate date = new TradeDate(tkn.nextToken());
- int delta = (new Integer(tkn.nextToken())).intValue();
- port.add(new Trade(sec, date, delta));
- line = stream.readLine();
- }
- } finally {
- if (stream != null)
- stream.close();
- }
- db.createRoot("portfolios", all);
- }
-
- static void initialize(Database db)
- {
- all = (OSHashtable)db.getRoot("portfolios");
- }
-
- public String getName() {
- return name;
- }
-
- static public Enumeration getAll() {
- return all.elements();
- }
-
- public OSVector getTrades() {
- return trades;
- }
-
-
- /* This class is never used as a persistent hash key. */
- public int hashCode() {
- return super.hashCode();
- }
-
- }
-
- class SecurityPP
- {
- Security security;
- int position;
- int price;
-
- SecurityPP(Security security, int position)
- {
- this.security = security;
- this.position = position;
- }
- }
-
-