COS 126

Stock Market
Programming Assignment 1

Due: Wednesday, 11:59pm

Write a program to predict the performance of various stocks.

(There's a typo in the course packet: the first stock price below should be 26.375, not 23.375.)

Part 0: read in the data. The following program reads in a sequence of stock prices and prints them out.

#include <stdio.h>
void main()
    float price;
    while (scanf("%f", &price) != EOF)
      printf("%7.3f\n", price);
Type the program exactly as is; save as input.c; and compile with "lcc input.c". To read in the data from a file named stock15.txt, type "a.out < stock15.txt".

Part 1: plot. Write a program plot.c to display the stock values graphically instead of just printing the values. Round the stock price down to the nearest integer, and print that number of *'s. Your output will look something like:


Part 2: detect a pattern. Write a program to identify specific trends in the data. Dilbert the Day Trader believes that if the stock goes up 3 (or more) consecutive time periods and then down in the next period, then it is a good time to sell the stock. Analogously, Dilbert believes that if the stock goes down 3 consecutive time periods and then up in the next period, then it is a good time to buy the stock. Write a program pattern.c that prints out the time period and stock price, along with the word buy or sell according to Dilbert's rule. The output should look like this:

   1    26.375
   2    25.500
   3    25.125
   4    25.000
   5    25.250    buy
   6    27.125
   7    28.250
   8    26.000    sell
   9    25.500
  10    25.000
  11    25.125    buy
  12    25.250
  13    26.375
  14    25.500    sell
  15    25.500

Part 3: invest. Write a program invest.c to determine how much money you would have won or lost using Dilbert's rule. You start with $10,000.00 cash. Assume that you will convert all of your cash to stock when Dilbert's rule signals you to buy, and that you will convert all of your stock to cash when Dilbert's rule signals you to sell. For each time period, print out the price, cash, shares owned, and portfolio value. The value of your portfolio is cash plus the number of shares multiplied by the price per share. (For simplicity, assume that you can buy fractional amounts of stock, and there are no transaction fees.) For stock15.txt, the correct output is:

period   price     cash       shares    value
   1    26.375   10000.00       0.00   10000.00
   2    25.500   10000.00       0.00   10000.00
   3    25.125   10000.00       0.00   10000.00
   4    25.000   10000.00       0.00   10000.00
   5    25.250       0.00     396.04   10000.00
   6    27.125       0.00     396.04   10742.57
   7    28.250       0.00     396.04   11188.12
   8    26.000   10297.03       0.00   10297.03
   9    25.500   10297.03       0.00   10297.03
  10    25.000   10297.03       0.00   10297.03
  11    25.125       0.00     409.83   10297.03
  12    25.250       0.00     409.83   10348.26  
  13    26.375       0.00     409.83   10809.32
  14    25.500   10450.72       0.00   10450.72
  15    25.500   10450.72       0.00   10450.72

Part 4: 3-term moving average. The stock market is subject to noise which may obscure potential trends. To smooth out the data, write a program to compute a moving average of the data. The p-term moving average is the average price of the stock over the last p time periods. Write a program 3term.c to compute the 3-term moving average. Use this averaged data to determine the buy/sell signals according to Dilbert's rule, and print out a table as in Part 3, but with two fewer lines. (There is no 3-term average for the first two lines.)

Part 5: 100-term moving average. Redo Part 4, but now use a 100-term moving average. To avoid the tediousness of creating a hundred different variables, use a 100-element array. Run your program on the file stock1000.txt to see how it does on realistic data. The file is large, so the output will scroll through your window, but you will see the lines that tell you how you made out in the end. Save your program as 100term.c.

Part 6: develop your own strategy. Develop your own rule for when to buy and sell stock based on the stock price history. Your goal is the maximize the final value of the portfolio. Use any method you like, but do not "cheat" by having your method depend on knowing the stock price in the future. Unlike with Dilbert's rule, your rule may allow you to own cash and stock at the same time, for example, you may wish to spend only half of your cash on stock when you buy.

Save your program as contest.c. Test your rule on various real world stock data. We will award extra credit to the top 10 performers on our secret basket of several real stocks.

What to submit. Submit a readme file along with the programs from Parts 1, 3, 4, 5, and 6. Use the given filenames, and submit with the command:

/u/cs126/bin/submit 1 readme plot.c invest.c 3term.c 100term.c contest.c

This assignment was developed by Adam Finkelstein, Robert Sedgewick, and Kevin Wayne.
Copyright © 1999 Robert Sedgewick