Table of Contents

# I. Objective

This project aims to provide you with an opportunity to analyze the historical prices of a stock. You are required to calculate the 3-month moving average prices of the stock over a given period and determine the best and worst months for the stock in terms of its 3-month moving average prices. To complete this project, you need to apply the knowledge and skills about files, lists, and functions.

## II. Specification

__Data File__

You will be given a CSV data file of the historical prices of a stock. This file could be opened by WordPad and data fields in each row are delimited by comma. The data fields include:

**Date**: the day of trading**Open**: the stock price at the beginning of the trading day**High**: the highest price the stock achieved on the trading day**Low**: The lowest price the stock achieved on the trading day**Close**: the stock price at the end of the trading day**Adj Close:**the adjusted closing price of the trading day (reflecting the stock’s value after accounting for any corporate actions like dividends, stock splits, and new stock offerings)**Volume**: the total number of shares were traded on the trading day

__Functions__

Given the file of stock prices, you are asked to develop a Python program to process the data by designing appropriate functions. At minimum you need to implement and call the following three functions:

## · get_data_list(csv_file_name)

This function has one parameter, namely csv_file_name. When the function is called, you need to pass along a CSV file name which is used inside the function to open and read the CSV file. After reading each row, it will be split into a list. The list will then be appended into the main list (a list of lists), namely data_list. The data_list will be returned at the end of the function.

## · get_monthly_averages(data_list)

This function has one parameter, namely data_list. You need to pass the data_list generated by the get_data_list() function as the argument to this function and then calculate the monthly average prices of the stock. The average monthly prices are calculated in the following way. Suppose the volume and adjusted closing price of a trading day are V_{1} and C_{1}, respectively. The total sale of that day equals V_{1} x C_{1}. Now, suppose the volume and adjusted closing price of another trading day are V_{2} and C_{2}, respectively. The average of these two trading days is the sum of the total sales divided by the total volume:

Average price = (V_{1} x C_{1} + V_{2} x C_{2}) / (V_{1} + V_{2})

To average a whole month, you need to add up the total sales (V_{1} x C_{1} + V_{2} x C_{2} + … + V_{n} x C_{n}) for each day and divide it by the sum of all volumes (V_{1} + V_{2} + … + V_{n}) where n is the number of trading days in the month.

A tuple with 2 items, including the date (year and month only) and the average for that month, will be generated for each month. The tuple for each month will be appended to the main list, namely monthly_averages_list. The monthly_averages_list will be returned at the end of the function. Programming Problem Solving for Mathematics

## · get_moving_averages(monthly_averages_list)

This function has one parameter, namely monthly_averages_list. You need to pass the monthly_averages_list generated by the get_monthly_averages() function as the argument to this function and then calculate the 3-month moving average prices of the stock. In general, a 3-month moving average is the average value of the 3 most recent monthly observations taken from a time series.

For example, the following lists the monthly prices of stock between October 2012 and July 2013:

Month |
Average Price |

October 2020 | 100 |

November 2020 | 101 |

December 2020 | 103 |

January 2021 | 99 |

February 2021 | 97 |

March 2021 | 102 |

To construct a 3-month moving average, take the average of the first three observations, in this case, October 2020, November 2020, and December 2020 prices:

Then find the average of the next three observations, starting with the second observation, so you’re finding the average of the second, third, and fourth observations (or November 2020, December 2020, and January 2021):

Continue the process for the entire sample. The resulting 3-month moving averages are shown below:

Month |
Average Price |
3-Month Moving Average |

October 2020 | 100 | – |

November 2020 | 101 | 101.33 |

December 2020 | 103 | 101.00 |

January 2021 | 99 | 99.67 |

February 2021 | 97 | 99.33 |

March 2021 | 102 | – |

The first 3-month moving average is listed next to November 2020, even though it represents the average of October 2020, November 2020, and December 2020. This shows that November 2020 is the “center” of the moving average. Similarly, the 3-month moving average constructed from November 2020, December 2020, and January 2021 prices are shown next to December 2020, indicating that it’s the center of the second average. Programming Problem Solving for Mathematics

A tuple with 2 items, including the date (year and month only) and the 3-month moving average for that month, will be generated for each month except the first and the last ones. Each tuple will be appended to the main list, namely moving_averages_list. The moving_averages_list will be returned at the end of the function.

__Program Input and Output__

At the outset, your program needs to ask the user for a CSV file name:

Based on the CSV file name, a corresponding output text file (e.g. “Google_output.txt” for this case) will be generated. In the output file, you are eventually required to print the best month (with the highest moving average price) and the worst month (with the lowest moving average price) for the stock. You need to first print a header line for the stock, and then print a date (MM-YYYY), a comma followed by a moving average price (in 2 decimal places) on another line. You must follow the output format as shown below:

## III. Deliverable

You have to include your student name and ID in your source code and name your project solution as “XXXXXXXX_project.py” (where XXXXXXXX is your 8-digit student ID). Please remember to upload your source code solution to Moodle by the submission deadline.

## IV. Evaluation Criteria (40% of Overall Course Assessment)

The project will be graded using the following criteria:

- 15% – Correctness of program execution and output data
- 10% – Modularization (e.g. dividing the program functionality into different functions)
- 5% – Error handling
- 5% – Consistent style (e.g., capitalization, indenting, )
- 5% – Appropriate comments

## V. Plagiarism Policy

Your project should represent your work. **Do not include any code not written by you in your project**. **You are NOT allowed to import any Python libraries in your solution except the modules namely **

**os**(

__https://docs.python.org/3/library/os.html__),

**sys**(

__https://docs.python.org/3/library/sys.html__), and

**CSV**(

__https://docs.python.org/3/library/csv.html__). If cheating is found or the import requirement is violated, you will receive a zero mark.

INT3075 Programming and Problem Solving for Mathematics Project Description.