Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Instructions Choose 4 different stocks from any publicly listed companies Find the closing prices for these stocks on September 2 0 , 2 0 2
Instructions
Choose different stocks from any publicly listed companies
Find the closing prices for these stocks on September as the purchase price, and September as the current price
Use these stock prices to complete the following tasks
Task : Collect Stock Prices Data and Create Vectors
Choose different stocks and note their ticker symbols eg AAPL for Apple, MSFT for Microsoft, etc.
Use an online source such as Yahoo Finance or Google Finance to find the closing prices of these stocks on the following dates:
Purchase price: Closing price on September
Current price: Closing price on September
Create three vectors:
stocktickers: Stock ticker symbols egAAPLMSFTGOOGAMZN
purchaseprices: The closing prices on September
currentprices: The closing prices on September
# write your code
Task : Write a Function to Calculate Returns and Use a Loop
Define a function CalculateReturncurrentprice, purchaseprice that
Takes the current price and purchase price as inputs
Calculate the percentage return using the formula:
Percentage ReturnCurrent PricePurchase PricePurchase Pricetimes
Use a for loop to apply this function to each stock and create a new vector returns that stores the percentage return for each stock.
Task : DataFrame
Create a DataFrame portfoliodf with the following columns:
Stock: Stock ticker symbols.
PurchasePrice: Purchase prices from Task
CurrentPrice: Current prices from Task
Return: The percentage return from Task
Use the ifelse function to create a new column TradeDecision based on the following conditions:
Sell if the return is greater than
Sell if the return is less than
Hold otherwise.
Task :Calculate Realized ProfitLoss Based on Trade Decisions
Bouns Points: Vectorize the calculation for Task
Loop through the portfolio and check the trade decision. If the decision is Sell calculate the realized profit for that stock:
Realized ProfitLossCurrent PricePurchase Price
Sum the realized profitslosses for all stocks.
Print the total realized profitloss for the portfolio.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started