[R-SIG-Finance] Donchian Channel in TTR

Jeffrey Ryan jeff.a.ryan at gmail.com
Thu Apr 5 16:45:03 CEST 2012


As I think I am partly responsible for this code in TTR (at least the
initial rev), I'll explain my logic.

In general, more options/arguments that simply do what single functions
can do more explicitly isn't the best design strategy in many cases.
Secondly, most (all?) of the other functions in TTR behave by including
the 'current' price in the calculation.  In the case of DonchianChannel
the result of this is simply more obvious.

e.g. EMA, SMA, Bbands, runMax, cumsum (base R), cummin (base R), etc also
behave this way.

The fix, without any code changes in the related TTR functions is to use
the facility lag().  This is the canonical way to assure you aren't
introducing look-ahead bias in your results.

For what it is worth, this had been recently discussed in private.  Josh
may have a slightly different opinion, and I think the consensus was that
the documentation could be more explicit, but the behavior is consistent
with R and TTR as is.

HTH
Jeff

On 4/5/12 8:11 AM, "Andreas Voellenklee" <wotuzu17 at gmail.com> wrote:

>Hi,
>
>The DonchianChannel function in the package TTR calculates the highest
>high and lowest low of the *current price* and the last (n-1) prices.
>According to my sources, only the previous n prices should be
>regarded, not the current price.
>
>The following example shows that the current price is taken in the fifth
>line
>
>getSymbols("YHOO", adjust=TRUE)
>head(cbind((YHOO), DonchianChannel(cbind(Hi(YHOO),Lo(YHOO)),n=5)),n=10)
>
>Possibly both methods are valid, but there should be a (optional)
>parameter to the function so that users can choose whether they want
>to include the current price or not. As in here:
>http://www.linnsoft.com/tour/techind/donch.htm
>
>Regards,
>Andreas
>
>_______________________________________________
>R-SIG-Finance at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>-- Subscriber-posting only. If you want to post, subscribe first.
>-- Also note that this is not the r-help list where general R questions
>should go.



More information about the R-SIG-Finance mailing list