Milestone II: The “Moving Average” Trading Algorithm

You’ve read in the stock data from the CSV file I provided you. Also, you should have some data structures setup to hold that data and also any bookkeeping information about your available cash and the number of stocks you own.

It’s time to start trading.

Starting Point

You will start with your project.py file submitted for Milestone I. You may find your data loading or bookkeeping code doesn’t work with the requirements of Milestone II. No worries, you are allowed to change your code for Milestone I as you write code for this Milestone to your project.py file.

Of course, this won’t change your grade for Milestone I, but at least you can move forward with working milestone submissions!

Algorithm 1: Moving Average

The first analysis you will perform will be a moving average of the stock data. For this analysis assign yourself 1,000 fake US dollars to use to invest as your initial principle amount. Your program should decide to buy stock, sell stock, or take no action based on the average of the stock prices for the previous twenty days.

Your program should not take any action during the first twenty days so that it can use them to calculate your first average. You will use this average to compare to the 21st day. Then your program should recalculate the average of the 20 days starting at 2 and ending at day 21. This pattern should be repeated until the last day of data.

../_images/project-moving-average.png

A visual display of the moving average algorithm.

For example: if you notice that the current day stock price is 20% lower than the average of the previous 20 days, you might decide that your program should choose to purchase some stock because you believe it will rise above that price soon. Then, if your algorithm observes that the current stock price is higher by a certain percent than the average, you might decided to have your algorithm sell. The time period of 20 days and other parameters are for you to decide and play with to maximize the amount of money your algorithm generates.

When your algorithm reaches the last day of data, have it sell all remaining stock. Your function will return the number of stocks you own (should be zero, at this point), and your cash balance.

def alg_moving_average(filename):
    """This function implements the moving average stock trading algorithm.

    Using the CSV stock data that should be loaded into your program, use
    that data to make decisions using the moving average algorithm.

    Also, any bookkeeping setup in Milestone I should be called/used here.

    Args:
        A filename, as a string.

    Returns:
        Two values, stocks and balance OF THE APPROPRIATE DATA TYPE.

    Prints:
        Nothing.
    """

    # Last thing to do, return two values: one for the number of stocks you end up
    # owning after the simulation, and the amount of money you have after the simulation.
    # Remember, all your stocks should be sold at the end!
    return stocks_owned, cash_balance

At this point, you can start building up your main() function.

# Don't forget the required "__main__" check!
def main():
    # My testing will use AAPL.csv or MSFT.csv
    filename = input("Enter a filename for stock data (CSV format): ")

    # Call your moving average algorithm, with the filename to open.
    alg1_stocks, alg1_balance = alg_moving_average(filename)

    # Print results of the moving average algorithm, returned above:
    print("The results are...")

Output

I don’t care what gets printed to the screen, so long as alg_moving_average(filename) returns the correct data.

Helpful Hints

  1. There should be no user input in this milestone beyond asking for the filename. Any decisions your program has to make should be done within the program without user direction.
  2. I need to be able to alone call alg_moving_average(filename) to test it. So make sure alg_moving_average(filename) does everything it needs to run when called directly (e.g. like loading your CSV data.)
  3. You should not use test_data() directly in Milestone II (you will lose points), so be sure to write new functions that use what you learned from writing that function.
  4. You can choose any threshold value you want, the value of 20% was just an example.
  5. You can buy or sell any amount of stock you wish.
  6. You can buy stock “on margin”, if that’s interesting to you.
  7. I don’t really care which stock price for a particular day you use (whether it’s the current day’s “open”, “close”, “high”, etc).

Milestone Submission Requirements

  1. Simulation Code (18 points) Your moving average trading simulation should be written according to the specification given. Some decisions are left to you, but the core algorithm should be honored. Your algorithm will not at any point look ahead at ‘future’ stock data. You can only use data for the days gone for making decisions in your simulation’s ‘current day’
  2. Data Handling (5 points) Is your algorithm appropriately using the data you loaded from the CSV file? You will lose points if you use test_data() from Milestone I (it needs to be include, but not used.)
  3. Bookkeeping (5 points) Does your trading algorithm make the right decisions when buying and selling, and does it track this information appropriately. For example, you can’t sell if you own zero stocks! Things like this should be covered.
  4. Testing (5 points) I will test your code by calling your alg_moving_average(filename) function directly. It should run smoothly and return the two values specified of the correct data types (stocks: integer, cash: float).

Points may be generally deducted across all categories in cases of syntax, style or running issues. Some areas of deduction incuded:

  1. Calling test_data() inside alg_moving_average(filename).