197k views
1 vote
USE R PROGRAMMING.

Give an example. Calculate simple returns for multiple stocks.
Share the r programming code and formula. Compare the simple
returns of all the stocks?

User Alex Mckay
by
7.9k points

1 Answer

5 votes

Final answer:

In R programming, you can calculate simple returns for multiple stocks using the 'quantmod' package. Here's an example: library(quantmod)# Define the stock symbols symbols <- c('AAPL', 'GOOGL', 'MSFT')# Get the stock price data getSymbols(symbols)# Calculate the simple returns returns <- diff(log(Cl(GOOGL)))# Compare the simple returns barplot(returns, names.arg = symbols, ylab = 'Simple Returns')

Step-by-step explanation:

In R programming, you can calculate simple returns for multiple stocks using the 'quantmod' package. Here's an example:

library(quantmod)

# Define the stock symbols
symbols <- c('AAPL', 'GOOGL', 'MSFT')

# Get the stock price data
getSymbols(symbols)

# Calculate the simple returns
returns <- diff(log(Cl(GOOGL)))

# Compare the simple returns
barplot(returns, names.arg = symbols, ylab = 'Simple Returns')

In this example, we define the stock symbols of 'AAPL', 'GOOGL', and 'MSFT'. We then use the 'getSymbols' function to fetch the stock price data from Yahoo Finance. Finally, we calculate the simple returns using the 'diff' and 'log' functions, and compare them using the 'barplot' function.

User Ronald Pauffert
by
8.1k points