undstanding donchian functions.

library(quantmod)
library(PerformanceAnalytics)
s <- get(getSymbols('SPY'))["2012::"]
s$sma20 <- SMA(Cl(s) , 20)
head(s,3)
s$dc <- DonchianChannel(s[,c("SPY.High","SPY.Low")])
head(s,3)
Question 1: i get the following and last column is named dc instead of low. why and how do i fix it?
SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted sma20 high mid dc
2012-01-03 127.76 128.38 127.43 127.50 193697900 105.2762 NA NA NA NA
2012-01-04 127.20 127.81 126.71 127.70 127186500 105.4413 NA NA NA NA
2012-01-05 127.01 128.23 126.43 128.04 173895000 105.7221 NA NA NA NA

when i rerun the following command,
s$dc <- DonchianChannel(s[,c("SPY.High","SPY.Low")])

i get Error in NextMethod(.Generic) :
number of items to replace is not a multiple of replacement length
i have to remove the last 3 columns before rerunning the donchian indicator method. why?
s<-subset(s,select=-c(high,mid,dc))

I might be missing something but I can't find the DonchianChannel function in quantmod or PerformanceAnalytics. Are you using the one from TTR?

Creating a reprex would probably help here.

Here you go

library(reprex)
library(quantmod)
#> Loading required package: xts
#> Loading required package: zoo
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
#> Loading required package: TTR
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
library(PerformanceAnalytics)
#>
#> Attaching package: 'PerformanceAnalytics'
#> The following object is masked from 'package:graphics':
#>
#> legend
s <- get(getSymbols('SPY'))["2012::"]
#> 'getSymbols' currently uses auto.assign=TRUE by default, but will
#> use auto.assign=FALSE in 0.5-0. You will still be able to use
#> 'loadSymbols' to automatically load data. getOption("getSymbols.env")
#> and getOption("getSymbols.auto.assign") will still be checked for
#> alternate defaults.
#>
#> This message is shown once per session and may be disabled by setting
#> options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
s$sma20 <- SMA(Cl(s) , 20)
head(s,3)
#> SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted sma20
#> 2012-01-03 127.76 128.38 127.43 127.50 193697900 105.2762 NA
#> 2012-01-04 127.20 127.81 126.71 127.70 127186500 105.4413 NA
#> 2012-01-05 127.01 128.23 126.43 128.04 173895000 105.7220 NA
s$dc <- DonchianChannel(s[,c("SPY.High","SPY.Low")])
head(s,3)
#> SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted sma20
#> 2012-01-03 127.76 128.38 127.43 127.50 193697900 105.2762 NA
#> 2012-01-04 127.20 127.81 126.71 127.70 127186500 105.4413 NA
#> 2012-01-05 127.01 128.23 126.43 128.04 173895000 105.7220 NA
#> high mid dc
#> 2012-01-03 NA NA NA
#> 2012-01-04 NA NA NA
#> 2012-01-05 NA NA NA

head(s,3)
#> SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted sma20
#> 2012-01-03 127.76 128.38 127.43 127.50 193697900 105.2762 NA
#> 2012-01-04 127.20 127.81 126.71 127.70 127186500 105.4413 NA
#> 2012-01-05 127.01 128.23 126.43 128.04 173895000 105.7220 NA
#> high mid dc
#> 2012-01-03 NA NA NA
#> 2012-01-04 NA NA NA
#> 2012-01-05 NA NA NA
s$dc <- DonchianChannel(s[,c("SPY.High","SPY.Low")])
#> Error in NextMethod(.Generic): number of items to replace is not a multiple of replacement length
s$dc <- DonchianChannel(s[,c("SPY.High","SPY.Low")])
#> Error in NextMethod(.Generic): number of items to replace is not a multiple of replacement length
Created on 2021-12-24 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.