简体   繁体   English

使用日内数据绘制烛台并添加移动平均线、chartSeries 或 geom_candlestick

[英]Plotting candlesticks with intraday data and adding moving averages, chartSeries or geom_candlestick

I am trying to plot candlesticks in R overlaid with moving averages, from my downloaded data for 30' SPY.我正在尝试使用移动平均线覆盖 R 中的 plot 个烛台,从我下载的 30' SPY 数据中。 I eventually want to plot one candlestick chart per day, with 14 day moving average overlaid, using a for-loop.我最终想要 plot 每天一张烛台图,使用 for 循环覆盖 14 天移动平均线。 Currently, I am not able to even plot candlesticks for the whole data.目前,我什至不能为整个数据提供 plot 个烛台。 (My version of RStudio is 2022.12.0 Build 353, Mac Darwin20, version of R is 4.2.2). (我的RStudio版本是2022.12.0 Build 353,Mac Darwin20,R版本是4.2.2)。

The last 10 lines of the data frame (spy30_reordered) look like this.:数据框 (spy30_reordered) 的最后 10 行如下所示:

structure(list(timestamp = structure(c(1643725800, 1643727600, 
1643729400, 1643731200, 1643733000, 1643734800, 1643736600, 1643738400, 
1643740200, 1643742000), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), open = c(450.69, 449.75, 448.785, 449.95, 449.89, 
450.76, 450.09, 450.45, 450.34, 450.47), high = c(451, 450.03, 
450.05, 450.91, 451.08, 450.97, 450.54, 450.55, 450.725, 450.88
), low = c(448.585, 446.885, 447.86, 449.4, 448.95, 449.52, 448.975, 
449.505, 449.575, 449.485), close = c(449.76, 448.88, 449.99, 
449.975, 450.635, 450.03, 450.41, 450.335, 450.395, 450.215), 
    ticker = c("SPY", "SPY", "SPY", "SPY", "SPY", "SPY", "SPY", 
    "SPY", "SPY", "SPY"), date = structure(c(19024, 19024, 19024, 
    19024, 19024, 19024, 19024, 19024, 19024, 19024), class = "Date"), 
    time = structure(c(52200, 54000, 55800, 57600, 59400, 61200, 
    63000, 64800, 66600, 68400), class = c("hms", "difftime"), units = "secs"), 
    dma14 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

I first tried chartSeries from TTR package:我首先尝试了来自 TTR package 的 chartSeries:

chartSeries(spy30_reordered, type = "candlesticks", theme = 'white')

#This gave the error, "Error in try.xts(x, error = "chartSeries requires an xtsible object"): chartSeries requires an xtsible object". #This 给出了错误,“try.xts 中的错误(x,error = “chartSeries 需要一个 xtsible 对象”):chartSeries 需要一个 xtsible 对象”。 My understanding was that the first column needs to be a POSIXct object, which my data has.我的理解是第一列需要是 POSIXct object,这是我的数据。 If I try to change my data frame to its object as follows:如果我尝试将我的数据框更改为其 object,如下所示:

spy30_reordered_xts <- xts(spy30_reordered, order.by=spy30_reordered[,1])

#I get the error, "Error in xts(spy30_reordered, order.by = spy30_reordered[, 1]): order.by requires an appropriate time-based object". #I 得到错误,“xts 中的错误(spy30_reordered,order.by = spy30_reordered[,1]):order.by 需要一个适当的基于时间的对象”。 Is my first column, a POSIXct object, not a time based object?我的第一列是 POSIXct object,而不是基于时间的 object 吗?

I also tried ggplot as follows:我也试过 ggplot 如下:

ggplot(spy30_reordered, aes(x = timestamp, open = open, high = high, low = low, close = close)) + geom_candlestick()

#This gives a plot, but the plot shows only vertical lines and not candlesticks. #This 给出了 plot,但是 plot 只显示垂直线而不是烛台。

在此处输入图像描述

I am not able to tell what I am doing wrong.我不知道我做错了什么。 Thanks for any help.谢谢你的帮助。

Have a look at the tidyquant library.看看tidyquant库。

Note that the library has excellent documentation, for example here .请注意,该库具有出色的文档,例如此处

For example use this:例如使用这个:

library(tidyquant)
library(ggplot2)
library(dplyr)

spy <- tq_get("SPY", from = "2020-01-01", to = "2022-12-31")

spy
#> # A tibble: 756 × 8
#>    symbol date        open  high   low close   volume adjusted
#>    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
#>  1 SPY    2020-01-02  324.  325.  323.  325. 59151200     310.
#>  2 SPY    2020-01-03  321.  324.  321.  322. 77709700     307.
#>  3 SPY    2020-01-06  320.  324.  320.  324. 55653900     309.
#>  4 SPY    2020-01-07  323.  324.  322.  323. 40496400     308.
#>  5 SPY    2020-01-08  323.  326.  323.  324. 68296000     309.
#>  6 SPY    2020-01-09  326.  327.  326.  327. 48473300     311.
#>  7 SPY    2020-01-10  327.  327.  325.  326. 53029300     310.
#>  8 SPY    2020-01-13  326.  328.  326.  328. 47086800     313.
#>  9 SPY    2020-01-14  327.  329.  327.  327. 62832800     312.
#> 10 SPY    2020-01-15  327.  329.  327.  328. 72056600     313.
#> # … with 746 more rows

spy %>%
  ggplot(aes(x = date, y = close)) +
  geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
  theme_tq()


# repeat with the first 20 days
spy %>%
  head(20) |> 
  ggplot(aes(x = date, y = close)) +
  geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
  theme_tq()


# add moving averages
spy %>%
  ggplot(aes(x = date, y = close)) +
  geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
  geom_ma(ma_fun = SMA, n = 50) +
  theme_tq()

Created on 2023-02-01 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2023-02-01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM