COSC 071: Computer Science I

Project 2
Spring 2002

C++ Implementation due: Mar 1 @ 5 PM
5 points

You thought you had the stock market all figured out---that is, until the Enron debacle. As a result, you've decided to put your newly found programming talents to good use by writing an automatic stock trading program. You figure, since chimps throwing darts at stock picks can perform as well as analysts with MBAs from Harvard, how hard can it possibly be?

The most common and the simplest way these programs work is they let you set a sell price and a buy price. If the stock rises above your sell price, then program sells a certain number of shares at the stock's current price. Conversely, if the stock falls below your buy price, then it buys a certain number of shares at the stock's current price.

In terms of complexity, at the other end of the spectrum, people have tried using neural networks. These are computational models based on the neural architecture of the brain that learn from inputs (e.g., last Monday's stock performance) and outputs (e.g., last Tuesday's stock performance) and predicting (e.g., tomorrow's stock performance) based on new input (e.g., today's stock performance). They've also looked at using chaos theory.

But you know folks, if these approaches really worked, they wouldn't be selling them. They'd be using them, right? Why would I sell a 30-dollar piece of software when I can tap into the unlimited riches of the world's stock markets? So maybe with this project, you'll be able to invest next semester's tuition, make tons of money, and laugh whilst your feeble-minded peers and professors sit around and study C++ and Foucault.

I retrieved a little more than three years of historical data for Microsoft (MSFT), Cisco Systems (CSCO), and Sun Microsystems (SUNW) from Yahoo! Finance.

Your program should start by prompting for the name of a data file and any other information, like your buy price and the number of shares to buy. Starting with 100 shares of stock, it should then process each record consisting of a date and closing price, and determine whether to buy or sell based on the day's closing price. You can assume that one day's closing price will be the next day's opening price. Your program should keep running totals of the number of shares bought and sold and your net profit or loss, which the program should print when it finishes processing the file. Although it would be nice to assume that you have an unlimited supply of money with which to buy stock, which would be kind of like ignoring friction or volume, you should start with $10,000 of Monopoly money, subtracting from and adding to that account as you buy and sell shares.

Here is a hypothetical sample run with the user's reponses in boldface type:

gusun% a.out

Hoya's On-line Stock Trader

Enter name of data file: enrnq.dta
Enter buy price: 44.00
Enter shares to buy: 20
Enter sell price: 63.05
Enter shares to sell: 20

Shares owned: 100
Shares bought: 0
Shares sold: 0
Portfolio value: $   440.00
Account balance: $ 10000.00

Processing transactions...

 1-Apr-98: Account balance too low to execute transaction.
21-Aug-00: Too few shares to execute transaction.

  ** Period Summary **

Shares owned: 20
Shares bought: 540
Shares sold: 620
Portfolio value: $8.00
Account balance: $0.00
Profit/loss over period: $ -9992.00 

Make sure your programs checks whether the data file is present. Output should go to the console. It should also ensure that there is enough money or stock to complete a transaction, although the specific way in which you handle this is up to you.

Development Hints

  1. Eat, sleep, breathe, and become stepwise refinement and incremental development.

  2. If you found it useful, develop a numerical solution, which you can use to check your program's output. You may also want to go through a design step by developing an algorithm. However, for this project, you won't have to submit these.

  3. Create a small data file that is representative of the types of transactions your program will encounter. Enter, say, ten transactions, making sure that there are one or two that will trigger a buy order and one or two that will trigger a sell order.

  4. Start by writing a program that prints each date and closing price to the screen.

  5. Once the program is printing the file's contents, incrementally develop the program by dealing with a sell transaction. Once that works, handle a buy transaction. You may need to modify your data file, but then handle the case when a transaction would cause the account balance to go below zero. Continue in this fashion until the program is finished.

  6. While you're developing, hard code the values that will eventually come from the keyboard, like the buy price and the number of shares to sell. You can add the cin statements last, once everything is working properly.

Instructions for Electronic Submission: At the top of the file containing your source code (i.e., the file containing the C++ instructions), place the following header comment, with the appropriate modifications:

//
// Project 2
// Name: <your name>
// E-mail: <e-mail address>
// Instructor: Maloof
// TA: <TA's name>
// COSC 071
//
// In accordance with the class policies and Georgetown's Honor Code,
// I certify that, with the exceptions of the lecture notes and those
// items noted below, I have neither given nor received any assistance
// on this project.
//
// Description: <Describe your program>
//

Although you may use any C++ compiler to develop your program, it must run under UNIX and must compile using GNU g++. When you are ready to submit your program for grading, if necessary, use ws-FTP to transfer your source and data file from your PC to gusun. Use SSH to logon to gusun, and use pine to e-mail it to your TA. Use your netid and the suffix ``.cc'' as the subject.

gusun% pine

When the menu appears, select the item for composing e-mail. Assume that your netid is ab123, the name of your source file is proj1.cpp, and your TA's e-mail address is ``imagoodtamaloof@cs''.

Type your TA's e-mail address in the To field, and type your netid with the .cc suffix in the Subject field (no spaces before or after). Move the cursor down into the MESSAGE TEXT screen, and type the ^R command. Pine will ask for a file name (e.g., proj1.cpp), which it will then load as your message text. At this point, your screen should look something like the following:

Finally, type ^X to send the e-mail to your TA.

IMPORTANT: Do not send your source code as an attachment. Do not use a mail client other than pine.

If you need to include a message to you TA about your submission, then type the message as a comment in the program.

Once you've submitted your project, it is important to keep an electronic copy on a university machine (e.g., gusun or cssun) that preserves the modification date and time. If we lose your project or the e-mail system breaks, then we will need to look at the modification date and time of your project to ensure that you submitted it before it was due.

The TAs who will be grading your projects this semester are listed on the main page. You must e-mail your project before 5 PM on the due date.