Calculate changing date for a Donchian Channel technical indicator

87 Views Asked by At

I am trying to create an indicator that has a dynamic n that changes each day. Basically I am making a strategy that enters a trade when a stocks price reaches its all time highest price.

The best way I can think to do this is by using a Donchian Chanel and entering when the closing price is equal to or greater than all previous DC highs. To do this I need:

n = (Current date of algo - start date).

This way the indicator will start working from day 1 and it won't "forget" about previous highs as the strategy runs through years of data. The problem I am having is that I don't know how to write a code/function that will express the current date of strategy in a way that I can turn it into a simple calculation. The best code I can come up with is:

##Problem in line below##
dcn <- difftime(initdate, as.Date(datePos), units = c("days"))

### This part will work fine once dcn is working
BuySig<-function(price,DC...)
{ifelse(price=>DC,1,0)}
add.indicator(strategy=strategyname,name="DonchianChannel",
          arguments=list(HL=quote(mktdata$Close),n=dcn),label="DC")

dcn of course is going to be my Donichan Channel n. The problem I am having is that no matter what I try to use in place of as.Date(datePos) it keeps telling me "object 'datePos' not found". I have tried using other things that I specify earlier in my code such as: Dates, timestamp.

Any advice would be really helpful.

1

There are 1 best solutions below

0
On

You can't use DonchianChannel with an n that varies. n must be a fixed integer for that function. You need to create your own function that trades 'highest highs' since the start of your data set.

This achieves what you want; just make a function out of it and supply it as a function for add.indicator

library(quantmod)
getSymbols("SPY")

SPY_max <- runMax(Cl(SPY), n = 1, cumulative = TRUE)

SPY$all_time_high <-  Cl(SPY) >= SPY_max

chart_Series(SPY["2018/", 1:4])

tail(SPY[SPY$all_time_high == 1,], 10)
#            SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted all_time_high
# 2018-01-19   279.80   280.41  279.14    280.41  140920100     273.9762             1
# 2018-01-22   280.17   282.69  280.11    282.69   91322400     276.2038             1
# 2018-01-23   282.74   283.62  282.37    283.29   97084700     276.7901             1
# 2018-01-25   284.16   284.27  282.40    283.30   84587300     276.7998             1
# 2018-01-26   284.25   286.63  283.96    286.58  107743100     280.0046             1
# 2018-08-24   286.44   287.67  286.38    287.51   57487400     283.3048             1
# 2018-08-27   288.86   289.90  288.68    289.78   57072400     285.5416             1
# 2018-08-28   290.30   290.42  289.40    289.92   46943500     285.6796             1
# 2018-08-29   290.16   291.74  289.89    291.48   61485500     287.2167             1
# 2018-09-20   292.64   293.94  291.24    293.58  100360600     289.2860             

When the column all_time_high returns 1, you're at an all time high for the time series in question.

enter image description here