Python Homework Compute the Holding-Period Return
This course project is to develop a financial app. An investment is the current commitment of money or other resources in the expectation of reaping future benefits [Bodie et al., 2018]. You were considering investing in a stock, say, Oracle Corporation (ORCL) in 2020. The stock sold for $53.95 per share at year’s start. With an investment horizon of one year, the realized rate of return on your investment depended on: (1) the price per share at year’s end, and (2) the cash dividends you collected over the year. The price per share at year’s end was $64.69 and cash dividends over the year amounted to $0.96. The realized return, called holding-period return, or HPR (in this case, the holding period is one year), is defined as
Here we have
The project has three steps. First, each student will be assigned a stock (Refer to the Company List for the details). Go to https://finance.yahoo.com, type your stock symbol, and find the beginning price, ending price, and dividends of the last calendar year under the Historical Data tab. Note: The beginning price is the open price of the first trading day, and the ending price is the close price of the last trading day. Also, the dividends are zero, if a stock does not pay them.
Second, using the data obtained from Yahoo! Finance, design a class named Stock that contains:
- A str data attribute named symbol for the stock’s symbol.
- A str data attribute named name for the stock’s name.
- A float data attribute named beginning_price that stores the stock price at year’s start.
- A float data attribute named ending_price that stores the stock price at year’s end.
- A float data attribute named dividend that stores the cash dividends over the year.
- A _ _ init_ _ method that creates a stock with the specified symbol and name.
- A method named get_holding_period_return that computes the holding-period return, using the above formula.
Write a test program that creates a Stock object with the specified symbol and the name, and displays its holding-period return of the last calendar year.
Third, prepare a report that describes the main idea of your app, including your source codes and input and output screenshots. The report should be in Times New Roman, 12-point, single column format, 1.08 line spacing, and 3-4 pages. Please upload the report and your source files (i.e., the .py files) on Canvas by the due date. Hint: Refer to Program 10-7 bankaccount.py and Program 10-8 account_test.py, and modify the class, data attributes, and methods to solve your problem.
Sample Solution
Design the Stock Class in Python
- Define a Python class called “Stock.”
- Add the following data attributes to the class:
- symbol (str): the stock’s symbol
- name (str): the stock’s name
- beginning_price (float): the stock price at the year’s start
- ending_price (float): the stock price at the year’s end
- dividend (float): the cash dividends over the year
- Implement a constructor method “init” that initializes the Stock object with the specified symbol and name.
- Implement a method named “get_holding_period_return” that computes and returns the holding-period return using the formula provided in the project description.
Step 3: Write a Test Program
- Write a Python test program to create a Stock object with the specified symbol and name.
- Display the holding-period return of the last calendar year using the “get_holding_period_return” method.
Do you need help with Python Homework? Get help with your Python Assignment now
Python homework Free Sample Code
- class Stock:
def __init__(self, symbol, name, beginning_price, ending_price, dividend):
self.symbol = symbol
self.name = name
self.beginning_price = beginning_price
self.ending_price = ending_price
self.dividend = dividend - def get_holding_period_return(self):
total_return = (self.ending_price – self.beginning_price + self.dividend) / self.beginning_price
return total_return -
# Test program
if __name__ == “__main__”:
# Replace the values below with the data for your assigned stock
symbol = “ORCL”
name = “Oracle Corporation”
beginning_price = 53.95
ending_price = 64.69
dividend = 0.96 - # Create a Stock object
stock_obj = Stock(symbol, name, beginning_price, ending_price, dividend) - # Calculate and display the Holding-Period Return
hpr = stock_obj.get_holding_period_return()
print(f”Holding-Period Return for {name} ({symbol}): {hpr:.2%}”)