1

我正在使用整洁的包来绘制唐奇安的高低。低值看起来不正确。我可能没有正确调用 donchian 函数,因为 donchian_100_low 是该行的最大值。我不知道如何解决它。

 library(tidyverse)
library(tidyquant) 
library(ggthemes)
startdt <- "2021-02-01"
AMC <- tq_get(
  "AMC",
  get = "stock.prices",
  from = startdt
)

AMC_values <- AMC %>%
  mutate(
  #  EMA_20 = EMA(close, n = 20),
   # EMA_50 = EMA(close, n = 50),
    Don_100=DonchianChannel(high,low)
  ) %>%
  na.omit()

head(AMC_values)

结果:

 head(AMC_values)
# A tibble: 6 x 9
  symbol date        open  high   low close    volume adjusted Don_100[,"high"] [,"mid"] [,"low"]
  <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>            <dbl>    <dbl>    <dbl>
1 AMC    2021-02-17  5.58  5.62  5.32  5.55  38849000     5.55            17.2     11.4      5.62
2 AMC    2021-02-18  5.84  6.25  5.46  5.51 130540800     5.51            10.1      7.86     5.62
3 AMC    2021-02-19  5.54  5.77  5.51  5.7   40249100     5.7              9.77     7.70     5.62
4 AMC    2021-02-22  5.93  6.68  5.75  6.55 173409000     6.55             8.74     7.18     5.62
5 AMC    2021-02-23  6.97  7.86  6.01  7.7  264876400     7.7              8.27     6.94     5.62
6 AMC    2021-02-24  7.23  9.83  6.99  9.09 376881800     9.09             9.83     7.72     5.62
4

2 回答 2

0

问题是输入到DonchianChannel. 输入需要是 2 列的矩阵,而不是 2 个单独的列。如果您查看帮助,它会说:

可强制转换为 xts 或矩阵并包含高低价格的对象。

但这有点不清楚。带有它的示例显示它更好一点,data.frame、matrix 或 xts 对象都可以。

请注意,如果您想要一个带有 的 donchian 频道n = 100,您需要指定n,默认为:n = 10

为了让它在你的情况下工作,使用 tidyquant:

AMC_values <- AMC %>%
  mutate(
    Don_100=DonchianChannel(cbind(high,low))
  )
%>%
  na.omit()

head(AMC_values)

# A tibble: 6 x 9
  symbol date        open  high   low close    volume adjusted Don_100[,"high"] [,"mid"] [,"low"]
  <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>            <dbl>    <dbl>    <dbl>
1 AMC    2021-02-12  5.72  5.97  5.52  5.59  46773000     5.59            17.2     11.3      5.26
2 AMC    2021-02-16  6.03  6.05  5.49  5.65  61165700     5.65            10.1      7.68     5.26
3 AMC    2021-02-17  5.58  5.62  5.32  5.55  38849000     5.55             9.77     7.52     5.26
4 AMC    2021-02-18  5.84  6.25  5.46  5.51 130540800     5.51             8.74     7        5.26
5 AMC    2021-02-19  5.54  5.77  5.51  5.7   40249100     5.7              8.27     6.76     5.26
6 AMC    2021-02-22  5.93  6.68  5.75  6.55 173409000     6.55             6.89     6.07     5.26

当您通过 quantmod 执行此操作时匹配:

library(quantmod)
startdt <- "2021-02-01"

AMC <- tq_get(
  "AMC",
  get = "stock.prices",
  from = startdt
)

AMC <- getSymbols("AMC", from = startdt, auto.assign = FALSE)

head(na.omit(DonchianChannel(merge(Hi(AMC),Lo(AMC)))))
            high    mid  low
2021-02-12 17.25 11.255 5.26
2021-02-16 10.10  7.680 5.26
2021-02-17  9.77  7.515 5.26
2021-02-18  8.74  7.000 5.26
2021-02-19  8.27  6.765 5.26
2021-02-22  6.89  6.075 5.26
于 2021-12-31T09:44:10.620 回答
0

quantmod 还有一种方法可以直接提供 donchian 值。

library(tidyverse)
library(tidyquant) 
#> Loading required package: lubridate
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
#> Loading required package: PerformanceAnalytics
#> Loading required package: xts
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> 
#> Attaching package: 'xts'
#> The following objects are masked from 'package:dplyr':
#> 
#>     first, last
#> 
#> Attaching package: 'PerformanceAnalytics'
#> The following object is masked from 'package:graphics':
#> 
#>     legend
#> Loading required package: quantmod
#> Loading required package: TTR
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> == Need to Learn tidyquant? ====================================================
#> Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
#> </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
library(ggthemes)
startdt <- "2021-02-01"
AMC <- tq_get(
  "AMC",
  get = "stock.prices",
  from = startdt
)
test1<-AMC %>% 
tq_mutate(select = c("high", "low"), mutate_fun = DonchianChannel,n=20,col_rename = c("DonH","DonM","DonL" ))
head(test1,30) %>%
  na.omit()
#> # A tibble: 11 x 11
#>    symbol date        open  high   low close   volume adjusted  DonH  DonM  DonL
#>    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl> <dbl> <dbl> <dbl>
#>  1 AMC    2021-03-01  8.86  9.45  8.42  9.18   1.44e8     9.18  17.2 11.3   5.26
#>  2 AMC    2021-03-02  9.14  9.4   8.51  8.93   7.81e7     8.93  11    8.13  5.26
#>  3 AMC    2021-03-03  8.95  9.14  8.5   8.58   5.57e7     8.58  11    8.13  5.26
#>  4 AMC    2021-03-04  8.25  8.59  7.5   8.03   7.78e7     8.03  11    8.13  5.26
#>  5 AMC    2021-03-05  8.08  8.27  7.63  8.05   5.97e7     8.05  11    8.13  5.26
#>  6 AMC    2021-03-08  8.53  9.48  8.31  9.29   1.14e8     9.29  11    8.13  5.26
#>  7 AMC    2021-03-09  9.38 10.8   9.22 10.5    1.50e8    10.5   11    8.13  5.26
#>  8 AMC    2021-03-10 11.0  12.5   9.51  9.85   2.62e8     9.85  12.5  8.90  5.32
#>  9 AMC    2021-03-11 10.6  10.9   9.9  10.3    8.39e7    10.3   12.5  8.90  5.32
#> 10 AMC    2021-03-12 10.2  11.4   9.94 11.2    1.11e8    11.2   12.5  8.90  5.32
#> 11 AMC    2021-03-15 12.2  14.5  11.8  14.0    2.78e8    14.0   14.5  9.91  5.32
于 2022-01-01T06:13:36.367 回答