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.

    Any bookkeeping setup from Milestone I should be called/used here.

    You must buy stock if the current day price is 20% lower than the moving average.
    You must sell stock if the current day price is 20% higher than the moving average.

    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. We 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. Do 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 buy or sell any amount of stock you wish when the 20% threshold is reached..
  5. Choose any stock price column you wish for a particular day you use (whether it’s the current day’s “open”, “close”, “high”, etc).