Home > Mobile >  donchian low in the TTR is incorrect plot
donchian low in the TTR is incorrect plot

Time:01-02

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

CodePudding user response:

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

CodePudding user response:

quantmod also has a method to provide the donchian values directly.

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
  • Related