Programming Assignment 1 - Investment Strategies

Advanced Programming II, Spring 2002


Due Date

This assignment is due by 2:00 p.m. on Tuesday, 5 February.

See the assignment turn-in page (last modified on 22 January 2002) for instructions on turning in your assignment.

Background

Every month my broker buys $2,500 worth of shares in companies X, Y, and Z for my portfolio. I'd like my portfolio organized so that 50% of the total portfolio value comes from shares in company X, 30% of the total portfolio value comes from shares in company Y, and 20% of the total portfolio value comes from shares in company Z. In other words, in the ideal case:

value(portfolio)= value(X shares in portfolio)
+ value(Y shares in portfolio)
+ value(Z shares in portfolio)

and

0.5*value(portfolio) = value(X shares in portfolio)
0.3*value(portfolio) = value(Y shares in portfolio)
0.2*value(portfolio) = value(Z shares in portfolio)

where value(x) is the dollar value of x.

Unfortunately, my broker and I disagree on the best monthly investment strategy to use in purchasing stocks. My broker advocates Strategy S and I prefer Strategy C:

  1. Strategy S (for simple) - buy in proportion to the ideal portfolio organization; that is, buy $2,500*0.5 = $1,250 worth of shares in X, $2,500*0.3 = $750 worth of shares in Y, and so on.

  2. Strategy C (for complex) - buy stocks to minimize the difference between the actual and ideal portfolio percentages. For example, if the portfolio percentages are 38%, 50 and 12%, then X is is further away from its ideal percentage (50% - 38% = 12% vs. 20 - 18 = 8% for Z) and Strategy C would buy as many shares in X as possible to raise its percentage to 50%. (Shares in Y are not considered because they are over their percentage, which requires selling, but this problem involves only buying shares.) If there's any money left over, it would then buy as many shares of Z as possible to raise its percentage to 20%.

    If the portfolio is at its ideal organization, then Strategy C buys stocks just like Strategy S; that is, in proportion to the ideal portfolio organization.

The Problem

Write a program that inputs from std-in a set of monthly stock prices and manages two portfolios using Strategy S for one portfolio and Strategy C for the other portfolio. When the input ends, the program should output to std-out each portfolio's organization as described above, as well as the portfolio's value.

At the start of the program, each portfolio contains no shares. All stocks may be bought in fractional units; each month $2,500 worth of shares are added to the portfolio.

Input

Input is read from std-in and consists of a sequence of zero or more text lines. Each line contains three numbers giving the dollar value per share of stock in companies X, Y, and X. For example, the line

100.34 34.00 75.25

indicates that a share of X costs $100.34, a share of Y costs $34.00 and a share of Z costs $75.25.

If an input line doesn't have this format, your program should output an informative error message and exit.

Output

In the absence of any errors, when all input has been processed your program writes to std-out information about each portfolio, one portfolio per line. Each line has the form

strategy x-pct y-pct z-pct value

where

The percentages should be integers and the total portfolio value should be an dollar value. The output lines may appear in either order.

For example, the output

C 30 40 30 1000.34
S 27 35 38 1903.00

indicates the portfolio using the complex investment strategy has a total value of $1000.34, with shares of X contributing 30% to the total value, shares of Y contributing 40% of the total value, and so on (The numbers in this example were generated essentially at random and bear no relation to any other numbers appearing in this assignment).

Testing

The program stock-prices generates a set of stock prices you can use as input to your program; you can find the program in /export/home/class/cs-509/pa1. Each run of stock-prices generates a new set of prices, so you may find it convenient to capture the output in a file you can redirect into your program.


This page last modified on 31 January 2002.