如何使用 R 中的 Quantmod 包获得一些股票收益

问题描述 投票:0回答:1

我想获得特定股票(例如苹果股票)的收盘价(例如每日、每周、每月或每年)的一些回报。我使用下面的 R 代码获得了收盘价:

# import data
library(quantmod)
getSymbols("AAPL", src ="yahoo", from=Sys.Date()- 365, to=Sys.Date())
#View(AAPL)
APPLE <- AAPL$AAPL.Close
head(APPLE)
#                AAPL.Close
# 2023-03-13     150.47
# 2023-03-14     152.59
# 2023-03-15     152.99
# 2023-03-16     155.85
# 2023-03-17     155.00
# 2023-03-20     157.40

请告诉我如何使用如下示例的函数最方便地获取Apple股票的股票收益。

APPLE_ret <- return()
r quantmod
1个回答
0
投票

如果你想在 R 中使用相同的库,那么:

# Calculate daily returns with Delt
apple_returns_delt <- Delt(APPLE)
head(apple_returns_delt)

这将返回从一个时期到下一时期的百分比变化,如果您想更改不同的时间间隔,您可以调整其参数:

# Convert the daily closing prices into monthly prices (last of each month)
monthly_prices <- to.monthly(APPLE, indexAt = "lastof", OHLC = FALSE)

# Now, calculate the monthly returns using delt()
monthly_returns <- delt(Cl(monthly_prices))
head(monthly_returns)
© www.soinside.com 2019 - 2024. All rights reserved.