INT3075 Programming and Problem Solving for Mathematics
Creation date:2024-04-19 15:17:36
Programming and Problem Solving for Mathematics
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
INT3075 Programming and Problem Solving for Mathematics
Project Description
I. Objective
This project aims to provide you with an opportunity to analyse the historical prices of a stock. You are
required to calculate the 3-month moving average prices of the stock over a given period of time 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 in relation to 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 a main
list (a list of lists), namely data_list. The data_list will be returned at the end of the
function.
2
• 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 V1 and C1,
respectively. The total sale of that day equals V1 x C1. Now, suppose the volume and adjusted
closing price of another trading day are V2 and C2, respectively. The average of these two trading
days is the sum of the total sales divided by the total volume:
Average price = (V1 x C1 + V2 x C2) / (V1 + V2)
To average a whole month, you need to add up the total sales (V1 x C1 + V2 x C2 + … +
Vn x Cn) for each day and divide it by the sum of all volumes (V1 + V2 + … + Vn) 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 a main list,
namely monthly_averages_list. The monthly_averages_list will be returned at
the end of the function.
• 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 a 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
3
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
the November 2020, December 2020, and January 2021 prices is shown next to December 2020,
indicating that it’s the center of the second average.
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 a 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:
4
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:
The best month for Google:
12-2007, 669.23
The worst month for Google:
09-2004, 128.52
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, etc.)
• 5% - Appropriate comments
V. Plagiarism Policy
Your project should represent your own work. Do not include any code not written by you in your
project.