[R-SIG-Finance] problem with add.indicators and ATR function

Brian G. Peterson brian at braverock.com
Tue Dec 4 13:54:57 CET 2012


On 12/04/2012 04:31 AM, Justin Lilly wrote:
> My problem as stated in the subject, is that when I add two ATR indicators
> to a strategy, the second one has invalid output regardless of the
> arguments.  As an example, I have added two lines to the 0.7.7 maCross
> demo.  Directly after the second SMA indicator is added, I add these two
> lines at line 26:
>
> stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "ATR",
> arguments = list(HLC=quote(HLC(mktdata)), n=20),label= "atr1" )
> stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "ATR",
> arguments = list(HLC=quote(HLC(mktdata)), n=20),label= "atr2")
>
> note that the period (n) is identical.  This is to illustrate the problem.
>   Once ran my output is as follows:
>
<...snip different example...>
>
> I expect the output here to be identical and clearly its not.  Can someone
> please take a look?
>

The problem here is with the use of HLC() from quantmod.

It calls Hi(0, then Lo(), then Cl(), and can return multiple columns for 
each.  In the case here, it does exactly that, and returns trueHigh and 
trueLow when called for the second indicator.  You can see the problem 
by calling

head(HLC(mktdata))

after the script runs.

On current svn code, this small modification to you indicators produces 
the results that you are looking for:

add.indicator(strat.st, name = "ATR", arguments = 
list(HLC=quote(HLC(mktdata)[,1:3]), n=20),label= "atr1" )
add.indicator(strat.st, name = "ATR", arguments = 
list(HLC=quote(HLC(mktdata)[,c(1,3,5)]), n=20),label= "atr2")

note that I take columns 1,3,5 from the output of HLC to construct atr2, 
which will skip over the trueHigh and trueLow that you got from the 
first ATR indicator.

Regards,

    - Brian

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list