COMP 142: Project 2

You will write a program that lets the user calculate some statistics and run a simulation involving stock prices.

Background

In the stock market (or our simplified version), every stock has a price. This price is the cost of one single share of the stock. For example, right now the price of the FedEx stock is about $188.20. This means if I had $188.20, I could buy one share of this stock. Similarly, if I owned a share of this stock, I could sell it for $188.20.

The price of a stock fluctuates as people buy and sell it, as a response to supply and demand. In general, if more people are buying a stock than selling it, the price will increase, and if more people are selling than buying, the price will decrease. One way that people try to make money in the stock market is by purchasing a stock at one price and waiting for the stock to increase in price, then selling it. For instance, if a stock is priced at $10, and I buy one share of it, then some time later the price goes up to $12 and I sell my single share, my profit is $2. Obviously profits are magnified the more shares I buy at a time --- if I had bought 10 shares (paying $100 total) and then sold them (making $120 total), then my profit would have been $20. (We ignore transaction costs for this project.)

When the stock market opens for the day, the initial price that someone can buy or sell a given stock at is called the stock's opening price. The last price of the day, right before the market closes, is called the stock's closing price. The price fluctuates throughout the day; this project is only concerned with opening and closing prices.

What you need to do

You will write code to let the user type in stock prices as doubles, calculate the maximum closing price of the stock, calculate the largest daily swing in stock price, the largest overnight swing in price, and run a simulation using a specific stock trading pattern.

I highly suggest you write and test the pieces in this order:

  1. Download the starter code and paste it into a new CLion project. You will fill in some functions, as well as make some changes to the main() function. Before you begin writing the functions, look over the whole program to get a sense of how it is supposed to work.

    The program should compile and run right out of the box, even before you change anything. You should write each piece, test it, then move on to the next piece.

  2. Fill in the input_prices function. This function should use cin to have the user type in a certain number of stock prices, specified by "numdays." Don't put any cout statements in here; main takes care of that. Notice the function comment header block is already filled in for you.

    Test this code by using the print_prices function. There is a section of code in main that you can uncomment to see if what you wrote is working.

  3. Fill in the maximum_price function. This function calculates and returns the maximum price in the vector that is passed into it. You must also write the function comment header block.

  4. Fill in the largest_daily_swing function (and its comment header block). This function should calculate the largest difference (in absolute value) of prices from the opening of the day to the closing of the day.

    Notice that this function has two pass-by-reference parameters, called swing and day, that you will need to set: the calling function expects it. Because C++ can only return a single value from a function, we use these pass-by-reference parameters to send a pair of values back to main. These parameters should be set to the largest difference (in absolute value), and the day on which that swing in price occurred.

  5. Fill in the largest_overnight_swing function (and its comment header block). This function should calculate and return the largest difference (in absolute value) of prices from the closing of one day to the opening of the following day.

    Notice that this function has two pass-by-reference parameters, called swing and day, that you will need to set. These parameters should be set to the largest difference (in absolute value), and the day on which that swing in price occurred (day here means the day of the closing price, not the day of the opening price).

  6. Fill in the simulate function (and its comment header block). This function should run a stock price simulation that simulates investing $100 into the stock. At the beginning of each day, you invest all your money into the stock, and at the end of the day, you sell all your shares. Naturally, the amount of money you end up with at the end of each day will not necessarily be the amount of money you started with each day. For each day of the simulation, you should print the amount of money you start the day with, the number of shares you purchase (fractional shares are OK), the cost of each share at the opening, the cost of each share at the closing of the day, and the amount of money you end the day with. If this is confusing, see the sample output below for how your program should work.
Note that you should only modify the starter code in very specific ways --- only modify the functions you are told to, and only modify main() as specified.

Also note that there is a to_money function I've given you that is useful for printing prices; it converts a double into a string with a leading dollar sign and always two decimal places.

Testing your program

Sample interactions

Run 1:
Enter number of days to track prices for: 2
Enter the 2 opening prices, all at once: 10 11.50
Enter the 2 closing prices, all at once: 10.75 9.25

The maximum closing price of the stock during these days was: $10.75.

The largest daily swing was $2.25 on day 1.
The largest overnight swing was $0.75 from day 0 to day 1.

Day 0: you start with $100.00.
Day 0: you buy 10 shares at $10.00 per share.
Day 0: you sell your shares at $10.75 per share.
Day 0: you end with $107.50.

Day 1: you start with $107.50.
Day 1: you buy 9.34783 shares at $11.50 per share.
Day 1: you sell your shares at $9.25 per share.
Day 1: you end with $86.47.
Run 2:
Enter number of days to track prices for: 5
Enter the 5 opening prices, all at once: 20 21 21.6 22.8 19.9
Enter the 5 closing prices, all at once: 19 21.1 22.1 19.8 20.1

The maximum closing price of the stock during these days was: $22.10.

The largest daily swing was $3.00 on day 3.
The largest overnight swing was $2.00 from day 0 to day 1.

Day 0: you start with $100.00.
Day 0: you buy 5 shares at $20.00 per share.
Day 0: you sell your shares at $19.00 per share.
Day 0: you end with $95.00.

Day 1: you start with $95.00.
Day 1: you buy 4.52381 shares at $21.00 per share.
Day 1: you sell your shares at $21.10 per share.
Day 1: you end with $95.45.

Day 2: you start with $95.45.
Day 2: you buy 4.41909 shares at $21.60 per share.
Day 2: you sell your shares at $22.10 per share.
Day 2: you end with $97.66.

Day 3: you start with $97.66.
Day 3: you buy 4.28342 shares at $22.80 per share.
Day 3: you sell your shares at $19.80 per share.
Day 3: you end with $84.81.

Day 4: you start with $84.81.
Day 4: you buy 4.26189 shares at $19.90 per share.
Day 4: you sell your shares at $20.10 per share.
Day 4: you end with $85.66.

Reminders

Use good coding style and comments throughout your program. See the guide.

What to turn in

Through Moodle, turn in your code as a file called main.cpp or stocks.cpp.

Challenge Problem

To attempt this challenge, add a section to the end of your main() function which determines, given the specified opening and closing prices, the best opening price at which to buy the stock, and the best closing price at which to sell the stock, assuming you must own the stock continuously during all days in between. Note that this is not as simple as finding the lowest opening price and the highest closing price, because the lowest opening price might occur on a day after the highest closing price.

As an example, consider 4 days of prices:

Day:    0     1     2     3
Open:  11    10    12     9
Close: 16    14    15    10
The best choice here is to buy on the opening of day 1 at $10/share, then sell at the close of day 2 at $15/share. Notice that neither of these prices correspond to the minimum opening price (which happens on day 3) nor the maximum closing price (which happens on day 0).