Algorithms and Data Structures I
Project 5: The Stock Market
Background
The stock market allows investors to buy shares of publicly-traded companies. Investors can make a profit by selling shares they own for a higher price than what they paid. Profit is calculated as:
profit = (price_sold - price_bought) * num_shares
For example, if I buy 2 shares of company A for $5 each, and then a week later sell them for $8 each, I have made $6 in profit: $8 - $5 = $3 and $3 * 2 shares = $6.
Unfortunately, stock prices don’t just go up. Sometimes a stock will be bought and then sold at a lower price for a loss. For example, I can buy 3 shares of company X at $10 each, and then sell all three shares later on for $4 each. This would net me ($4 - $10) * 3 = - $18.
In this project, share transactions, or Trades, hold the following information:
- TradeType: whether the investor bought or sold shares
- Lot: a group of shares
Each Lot contains the following information:
- Ticker: which company the shares are for
- quantity: how many shares are in the lot
- pricePerShare: the price for each share in the Lot
Lots of shares (that’s Lots with a capital L) must be sold in the order they were bought, also known as first in first out (FIFO). This means we should use a Queue to represent a history of Trades.
For example, take this history of Trades:
When selling shares in Trade 3, the first 20 of the 30 sold shares must come from Trade 1. This means that the shares from Trade 1 are sold at a profit of $2 per share or $40 total.
Trade 3 still has 10 shares left to sell. These 10 must now come from the Lot in Trade 2. They are sold at a profit of $6 per share, or $60 total.
The total profit from selling 30 shares in Trade 3 is $40 from Trade 1 + $60 from Trade 2 = $100 total profit for Ticker AAA.
Exercise
Your job is to complete the implementation of the Portfolio class, which extends the PortfolioInterface.
The Portfolio class starts with two private fields (you can add more if you’d like):
- Hash Map> positions
- Maps Tickers to a Queue of Lots that were purchased by the Portfolio. Because the Lots are stored in a Queue, the first Lots removed from the Queue are the first that must be sold.
- Note: Java’s Queue is an abstract class. You can use the java .util .ArrayDeque when adding new Queues to the positions HashMap.
- Hash Map profits
- Maps Tickers to their total amount of profit (or loss) so far. Every time a Lot is sold, the
profits map should be updated to reflect change in profits or losses from that Lot’s Ticker.
The PortfolioInterface lists a few methods that you are responsible for implementing:
- void handleTrade(Trade trade)
- Process a single Trade. If it is a BUY, then shares are added to the portfolio. If it is a SELL, then shares must be removed from the portfolio in the order they were received and the profits must be calculated and updated.
- Note: You can safely assume that calls to handleTrade() that SELL shares will only ever happen when the Portfolio has enough shares of that Ticker to sell. The shares might be split across multiple Lots, but there will always be enough to sell.
- void handleTrades(List trades)
- Processes a list of Trades from the beginning of the list to the end. Trades in this list might sell shares that were bought in Trades located earlier in the list.
- double getProfit(Ticker ticker)
- Returns the net profit for all sales involving shares from Ticker
- double getTotal Profit()
- Returns the net profit for the entire Portfolio, that is, the sum of profits for every Ticker.
- int getSharesHeld(Ticker ticker)
- Returns the number of shares that the portfolio holds for Ticker. Ex: if a Portfolio sells all of its shares for a given Ticker, calling getSharesHeld afterwards must return 0.
Starter Code
The Starter Code for Project 5 is located here. Here is a rundown of each starter file:
- Ticker.java
- Represents a hashable Ticker for a company (ex: Apple’s stock ticker is “AAPL”, Google’s is “GOOG”)
- Lot.java
- Represents a group (or “ Lot”) of shares. Includes the Ticker of the company, the quantity of shares in the Lot, and the price per share.
- Note: There is a setQuantity() method in the event that a Lot has to be split up during a partial sale (see example with company AAA above)
- Trade.java
- Represents a trade of one Lot of shares. Has a TradeType (either BUY or SELL) and a Lot.
- PortfolioInterface.java
- The Interface that the Portfolio class must implement. Each method in this file has a descriptive comment explaining what the method should do.
- Portfolio.java
- An implementation of the PortfolioInterface that you will complete for this project.
- Project5.java
- A driver program that creates a Portfolio and buys and sells some Lots of shares.
- You can either run Project.java directly (java Project5) or with a filename argument (java Project5 trades .csv) and it will bulk-handle a CSV file of Trades
- trades.csv
- A comma-separated-values (CSV) file containing 200 fictional Trades. You can test your work by running java Project5 trades .csv.
Sample Output
Running java Project5 should yield:
I own 10 shares of AAA
I own 0 shares of AAA
Profits : 150 .0
|
I own 10 shares of BBB
I own 0 shares of BBB
Profits from : -25 .0
Total Profits : 125 .0
|
Running java Project5 trades .csv should yield:
Company AAA :
Profit to-date : 12466 .91
Shares still holding : 115
Company BBB :
Profit to-date : -3735 .2899999999995
Shares still holding : 175
Company CCC :
Profit to-date : 15712 .980000000003
Shares still holding : 340
Company DDD :
Profit to-date : 2355 .6000000000004
|
Shares
|
still holding : 123
|
Company
|
EEE :
Profit
Shares
|
to-date : 17877 .830000000005
still holding : 198
|
Company
|
FFF :
Profit
Shares
|
to-date : -639 .2
still holding : 87
|
Company
|
GGG :
Profit
|
to-date : 33356 .09
|
Shares still holding : 209
Total profits from 200 Trades : 77394 .92
|
Deliverables
You are responsible for submitting only Portfolio.java. All other files must remain unchanged.