rquantmodtechnical-indicatortidyquantttr

donchian low in the TTR is incorrect plot


I am plotting the donchian high and low using tidy packages. the low value does not look correct. I am probably not calling the donchian function properly as the donchian_100_low is the highest value of the row. I dont know how to fix it.

 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)

results:

 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

Solution

  • The problem is the input into DonchianChannel. The input needs to be a matrix of 2 columns, not 2 separate columns. If you check the help it says:

    Object that is coercible to xts or matrix and contains High-Low prices.

    But it is a bit unclear. The example with it shows it a bit better, either a data.frame, matrix or xts object is fine.

    Note that if you want a donchian channel with n = 100, you need to specify the n, default is: n = 10.

    To get it to work in your case, with 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
    

    which matches when you would do this via 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