在 r 中用指数移动平均线绘制股票价格

plot stockprice with exponential moving average in r

我有以下 df + 代码示例:(我的 df 来自 01/2016 - 04/2020)

         Date A01_Price A02_Price A03_Price A04_Price A05_Price A06_Price A07_Price A08_Price A09_Price A10_Price A11_Price A12_Price A13_Price A14_Price A15_Price
1  2016-01-04   49.5010   21.6400   90.0100    93.676   81.6110   27.6450   28.4600   44.1930   33.0140  216.5460    36.201   41.7360    25.495    16.200    69.197
2  2016-01-05   49.7855   21.9870   88.5695    92.329   82.4590   28.2235   28.2790   44.8180   34.0180  218.5370    35.929   40.8520    25.431    16.157    69.828
3  2016-01-06   49.0595   21.5060   87.4735    88.601   81.4320   28.1725   27.4720   43.5065   36.2670  207.5960    35.256   39.9220    25.076    16.061    66.947
4  2016-01-07   47.7785   20.8415   82.8735    83.725   78.9340   26.7820   26.3485   39.0500   34.3570  203.4775    33.967   38.2115    24.062    15.738    64.135
5  2016-01-08   47.7435   20.2600   82.9275    82.609   79.0000   26.4495   26.1980   41.0055   33.1400  199.9250    33.361   37.3630    23.993    15.641    63.434
6  2016-01-09   47.8160   20.3800   83.0530    83.503   79.3925   26.4900   26.2460   41.0680   33.1910  200.9835    33.447   37.4530    24.110    15.734    63.510
7  2016-01-10   47.7770   20.4750   82.9860    83.325   79.6450   26.4680   26.2200   41.0340   33.1640  201.3680    33.401   37.4050    23.998    15.713    63.469
8  2016-01-11   48.8095   20.8440   83.0320    83.513   78.6720   26.1275   26.5150   40.3455   33.9770  202.6630    33.516   37.5030    23.947    15.753    65.583
9  2016-01-12   48.9545   21.0340   83.7325    85.732   81.0900   27.0205   27.5920   41.3570   35.6450  205.9610    34.443   38.0980    24.004    15.939    66.032
10 2016-01-13   48.0195   20.4640   82.6305    81.151   81.1780   27.1925   26.8050   39.2130   35.3825  197.9070    33.023   37.5945    23.423    15.737    64.682

我希望在我的函数的图中显示每个 "Axy_Price" 的多头和空头指数移动平均线。就像在 this 图形中一样。如果我能以某种方式在 ggplot 中实现它,那就太好了,因为我想在图中显示更多变量。有人知道吗?到目前为止我已经试过了:

library(plyr)
library(tidyverse)
library(quantmod)
library(TTR)
library(zoo)
library(PerformanceAnalytics)
library(xts)

#### EMA function ####
EMAf <- function (price,n){
  ema <- c()
  ema[1:(n-1)] <- NA
  ema[n]<- mean(price[1:n])
  beta <- 2/(n+1)
  for (i in (n+1):length(price)){
    ema[i]<-beta * price[i] + 
      (1-beta) * ema[i-1]
  }
  ema <- reclass(ema,price)
  return(ema)
}
#
df_A01 <- df %>%
  dplyr::select(Date, A01_Price)
#
EMAf_A01 <- ddply(df_A01, "A01", f)
#
library(ggplot2)
ggplot(df_A01, aes(x=Year, y=Value)) +
  geom_line(mapping=aes(shape=Type), size=0.5) +
  theme_bw() +
  geom_line(data = madf, mapping=aes(x = Date, y = EMAf_A01, linetype=Type,
                                     color = A01), size = 1) +
  ylab(expression(paste("mean",mu, "g",C~L^{-1}, day^{-1}))) +
  theme(legend.key = element_blank())

我也用 ChartSeries 试过,但我得到一个动物园错误:

#
xts_stock01L <- as.xts(df_A01[, c(2)], order.by=df_A01[[1]])
#
chartSeries(xts_stock01L,
            subset="2016-01::2020-04",
            theme=chartTheme("white"))
addEMA(n=30,on=1,col = "blue")
addEMA(n=200,on=1,col = "red")

也许我需要以不同的方式处理这整件事

您可以逐行添加并使用TTR::EMA功能。

代码:

library(TTR)
library(quantmod)

getSymbols("AAPL")

df = as.data.frame(AAPL)

library(ggplot2)


df$EMA_short = EMA(df$AAPL.Close,1000)
df$EMA_long = EMA(df$AAPL.Close,100)
df$time = rownames(df) %>% as.POSIXct()

df = na.omit(df)
ggplot(df) + geom_line(aes(y=EMA_short,x =time),col="green") + geom_line(aes(y=EMA_long,x=time),col="red") + geom_line(aes(y=AAPL.Close,x=time))

如果你想通过一个命令绘制线条,你应该 melt df,有一些很好的帖子如何做到这一点。

我认为问题可能出在使用多个数据框。让所有变量在一个文件中。