Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用r中的指数移动平均值绘制股票价格_R_Ggplot2_Dplyr_Time Series_Moving Average - Fatal编程技术网

用r中的指数移动平均值绘制股票价格

用r中的指数移动平均值绘制股票价格,r,ggplot2,dplyr,time-series,moving-average,R,Ggplot2,Dplyr,Time Series,Moving Average,我有以下df+代码示例:(我的df是从2016年1月至2020年4月) 我希望在我的函数图中显示每个“Axy_价格”的长短指数移动平均值。就像在图形中一样。如果我能以某种方式在ggplot中实现它,那就太好了,因为我想在plot中显示更多的变量。有人有主意吗?到目前为止,我已经试过了: library(plyr) library(tidyverse) library(quantmod) library(TTR) library(zoo) library(PerformanceAnalytics)

我有以下df+代码示例:(我的df是从2016年1月至2020年4月)

我希望在我的函数图中显示每个“Axy_价格”的长短指数移动平均值。就像在图形中一样。如果我能以某种方式在ggplot中实现它,那就太好了,因为我想在plot中显示更多的变量。有人有主意吗?到目前为止,我已经试过了:

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())
库(plyr)
图书馆(tidyverse)
图书馆(quantmod)
图书馆(TTR)
图书馆(动物园)
库(性能分析)
图书馆(xts)
####EMA函数####

EMAf您可以逐个添加行并使用TTR::EMA函数

代码:


如果你想用一个命令来画线,你应该使用df,这里有一些很好的方法。你可以一行一行地添加线,并使用TTR::EMA函数

代码:


如果你想用一个命令来绘制直线,你应该使用df,这里有一些很好的帖子,介绍如何做到这一点。

我认为问题可能在于使用了多个数据帧。将所有变量放在一个文件中。

我认为问题可能是使用了多个数据帧。将所有变量放在一个文件中。

谢谢。是的,也许我应该制作一个包含所有数据的xts文件。谢谢。是的,也许我应该制作一个包含所有数据的xts文件。
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())
#
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")
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))