美文网首页
R语言预测美国股票走势

R语言预测美国股票走势

作者: 落寞的橙子 | 来源:发表于2021-03-11 12:01 被阅读0次

    Introduction to Stock Analysis with R
    移动平均线
    简单来说短线趋势在长线之上的时候可以考虑买,在我的例子中则是红色开始蓝线之上的时候可以买

    rm(list=ls())
    # install.packages("quantmod")
    # install.packages("ggplot2")
    
    predict_stock_30day<-function(stock_code,data_start,data_end,data_tracking_from){
      suppressMessages(library(quantmod))
      suppressMessages(library(ggplot2))
      
      pbr <- getSymbols(stock_code, src = "yahoo", from = data_start, to =data_end , auto.assign = FALSE)
      pbr_mm <- subset(pbr, index(pbr) >=data_tracking_from )
      
      pbr_mm10 <- rollmean(pbr_mm[,6], 10, fill = list(NA, NULL, NA), align = "right")
      pbr_mm30 <- rollmean(pbr_mm[,6], 30, fill = list(NA, NULL, NA), align = "right")
      
      pbr_mm$mm10 <- coredata(pbr_mm10)
      pbr_mm$mm30 <- coredata(pbr_mm30)
      
      p<-ggplot(pbr_mm, aes(x = index(pbr_mm))) +
        geom_line(aes(y = pbr_mm[,6], color = "PBR")) + ggtitle("Petrobras prices series") +
        geom_line(aes(y = pbr_mm$mm10, color = "MM10")) +
        geom_line(aes(y = pbr_mm$mm30, color = "MM30")) + xlab("Date") + ylab(paste0(stock_code," Price")) +
        theme(plot.title = element_text(hjust = 0.5), panel.border = element_blank()) +
        scale_x_date(date_labels = "%b %y", date_breaks = "3 months") +
        scale_colour_manual("Series", values=c("PBR"="gray70", "MM10"="#DF3D8C", "MM30"="#0A5EB9"))
      p<-p+theme_bw()+theme_classic()
      return(p)
    }
    
    stock_code="AAL"
    data_start="2018-01-01"
    data_end=Sys.Date()
    data_tracking_from="2020-01-01"
    plot(predict_stock_30day(stock_code=stock_code,data_start=data_start,data_end=data_end,data_tracking_from=data_tracking_from))
    
    image.png

    相关文章

      网友评论

          本文标题:R语言预测美国股票走势

          本文链接:https://www.haomeiwen.com/subject/zwbnqltx.html