Search code examples
rquantmod

How can I avoid including same day HLC prices in ADX function


I would like to use the ADX function (TTR package). I use the following syntax but I'm not sure if the current day prices are part of the formula.If they are then I can't use this function for prediction.Is there a way to avoid the current day prices with ADX. Here is the line of code I use:

AMZN$ADX14<-ADX(HLC(AMZN),n = 14, maType="EMA", wilder=TRUE, na.rm=TRUE)

Solution

  • Looking at the source code, yes the close price on a given timestamp is used in calculating the corresponding ADX value on that timestamp.

    Based on your description, it seems like you want to use lag 1 values of the ADX, which you could achieve this way (for n = 6 for simplicity):

    AMZN$ADX6 <- ADX(HLC(AMZN),n = 6, maType="EMA", wilder=TRUE, na.rm=TRUE)
    AMZN$ADX6lag1 <- lag.xts(ADX(HLC(AMZN),n = 6, maType="EMA", wilder=TRUE, na.rm=TRUE))
    
    head(AMZN, 13)
               AMZN.Open AMZN.High AMZN.Low AMZN.Close AMZN.Volume AMZN.Adjusted      DIp      DIn        DX     ADX6    DIp.1    DIn.1      DX.1 ADX6lag1
    2007-01-03     38.68     39.06    38.05      38.70    12405100         38.70       NA       NA        NA       NA       NA       NA        NA       NA
    2007-01-04     38.59     39.14    38.26      38.90     6318400         38.90       NA       NA        NA       NA       NA       NA        NA       NA
    2007-01-05     38.72     38.79    37.60      38.37     6619700         38.37       NA       NA        NA       NA       NA       NA        NA       NA
    2007-01-08     38.22     38.31    37.17      37.50     6783000         37.50       NA       NA        NA       NA       NA       NA        NA       NA
    2007-01-09     37.60     38.06    37.34      37.78     5703000         37.78       NA       NA        NA       NA       NA       NA        NA       NA
    2007-01-10     37.49     37.70    37.07      37.15     6527500         37.15       NA       NA        NA       NA       NA       NA        NA       NA
    2007-01-11     37.17     38.00    37.17      37.40     6465600         37.40  7.54711 23.32760 51.111380       NA       NA       NA        NA       NA
    2007-01-12     37.36     38.21    37.27      38.20     4466400         38.20 10.33458 18.93201 29.376237       NA  7.54711 23.32760 51.111380       NA
    2007-01-16     38.40     38.89    37.97      38.66     5643700         38.66 21.85520 15.50147 17.008279       NA 10.33458 18.93201 29.376237       NA
    2007-01-17     38.70     39.00    37.78      37.88     5026800         37.88 16.96372 15.51769  4.451876       NA 21.85520 15.50147 17.008279       NA
    2007-01-18     37.50     37.65    36.72      36.98     9105000         36.98 13.51296 30.94944 39.216234       NA 16.96372 15.51769  4.451876       NA
    2007-01-19     36.69     37.48    36.60      37.02     6095900         37.02 11.40158 28.24434 42.482962 30.60783 13.51296 30.94944 39.216234       NA
    2007-01-22     37.65     37.90    36.80      36.95     8317000         36.95 16.48641 22.88154 16.244505 28.21394 11.40158 28.24434 42.482962 30.60783
    

    On timestamp 2007-01-22, you could use ADX6lag1 = 30.60783 (which was actually computed on 2007-01-19 to predict the close on that bar of 36.95.