Sunday, October 31, 2010

HW 6: Finance

In this homework you will practice using exceptions and files by writing a short program that reads in stock prices and prints out, and saves to a file, some information about these prices.

If you go to this page you will see how Google makes available historical prices for all stocks, in this case GOOG. On that page there is a link called download to spreadsheet which will download these price to a CSV file. Download that file and open it in a text editor so you can see what it looks like. You will be writing a program that can read these files (CSV files of historical stock prices from finance.google) and parse them into an array.

One thing you will notice is that some of the days a trading volume of 0. These are holidays. These days will mess up our later calculations so when you read from a file you must skip these days.

Also, assume that there are at most 1000 days of data. If there are more in the data file, your program should throw a FileTooBigException.

Once you have parsed a file, your program will then calculate the volatility of the stock for the last X days, where X is an argument to your function. To calculate volatility for the last X days you

  1. Calculate the average close price for the last X days. Call this average-close.
  2. Add up the square-difference = (closing-price - average-close) ^ 2 for every closing price in the last X days, then divide this number by X and then take its square root. That's the volatility.

The volatility tells us how much a stock price has moved, but it is only one number. We want to get a better picture of how the stock has moved. What we need is a distribution of the closing prices and the number of days, in the last X days, that the stock has closed at that price (where all prices are truncated to their integer value).

Implement a method called int[] getPriceDistribution(int X) which returns an array whose values are the number of days the stock has closed at that price (like a grade distribution, but for prices). Also implement getMinClose(int X) which returns the minimum closing price in the last X days. Then, the index 0 on the array returned by getPriceDistribution will be the number of days the stock closed at the price getMinClose(X).

Finally, now that you have this price distribution, we'll want to save it to a CSV file. Implement a method called savePriceDistribution(String outFile, int X which writes the price distribution of the last X days to outfile, in CSV format.

Below is a concrete example of a main using the new Security class and the output it generates.

 public static void main(String[] args) {
  Scanner istream = null;
  String inputFile = "/Users/jmvidal/data.csv";
  try {
   istream = new Scanner(new File(inputFile));
  } catch (FileNotFoundException e) {
   System.out.println("Ooops, no such input file:" + inputFile);
   System.exit(1);
  }

  Security s = new Security(); //Security class will hold all the data
  try {
   s.readFromStream(istream); //read the contents from istream
  } catch (FileTooBigException e1) { //file is to big to read
   System.out.println(e1.getMessage());
   System.exit(1);
  }

  System.out.println(s); //print out all the contents. Note: no days with 0 volume.

  try {
   System.out.println("Volatility in the last 10 days = " + s.getVolatility(10));
   System.out.println("Volatility in the last 1000 days = " + s.getVolatility(1000));

  } catch (NotEnoughDataException e) {
   System.out.println("Sorry, not enough data." + e.getMessage());
  }

  System.out.println("Price Distribution for the last 10 days.");
  System.out.println("price\tnumber of days");
  int[] priceDistribution = s.getPriceDistribution(10); //get the distribution array
  int minClose = (int)s.getMinClose(10);
  for (int i=0; i < priceDistribution.length; i++) { //we need this loop to print it out
   int p = minClose + i;
   System.out.println(p + "\t" + priceDistribution[i]);
  }

  try {
   //now, save the distribtion for the last 20 days to a file.
   s.savePriceDistribution("/Users/jmvidal/distribution-20.csv",20); 
  } catch (FileNotFoundException e) {
   System.out.println("Ooops, bad output file name.");
   System.exit(1);
  }

 }

And here is the output it generates:

28-Oct-10 618.58 2187396
27-Oct-10 616.47 2242414
26-Oct-10 618.6 2513633
25-Oct-10 616.5 3158700
...well you get the idea...
...notice, no days with 0 volume...
5-Nov-09 548.65 1848039
4-Nov-09 540.33 2333629
3-Nov-09 537.29 2382892
2-Nov-09 533.99 3202875
30-Oct-09 536.12 3469738

Volatility in the last 10 days = 5.4362785064784855
Sorry, not enough data.Need more historical data
Price Distribution for the last 10 days.
price number of days
601 1
602 0
603 0
604 0
605 0
606 0
607 2
608 0
609 0
610 0
611 1
612 1
613 0
614 0
615 0
616 2
617 1
618 2

This homework is due Tuesday, November 9 @9:00am

1 comment:

Jose M Vidal said...

I forgot a hint: you might find

istream.useDelimiter("[,\\s]");

which separates on , or on whitespace, useful.