R Statistics: Average True Trailing Stop

I am using the following article:

marintrading.com/106VERV.PDF

to create an indicator of the average trading of True Range Range in R. I tried various ways to do this, including for loops, pmin and creating a time delay, and nothing works.

Could you help me?

+5
source share
1 answer

Why are you trying to create it? It is available in the TTR package in function ATR.

UPDATE (after reading the question more carefully). I'm not quite sure what this solution is, but I hope this helps you in the right direction.

library(quantmod)
getSymbols("AMD", from="2005-11-01", to="2006-08-01")
AMD$stopLongATR <- -3.5*ATR(HLC(AMD),5)[,"atr"]
AMD$stopShortATR <- 3.5*ATR(HLC(AMD),5)[,"atr"]

chartSeries(AMD, TA=NULL)
addTA(runMax(Cl(AMD)+AMD$stopLongATR,10), on=1)
addTA(runMin(Cl(AMD)+AMD$stopShortATR,10), on=1)

UPDATE # 2:

- . . , coredata , trail , . xts/zoo , .

AMD$trail <- 0
AMD$AMD.lagCl <- lag(Cl(AMD))

for(i in 6:NROW(AMD)) {
  trail1 <- coredata(AMD$trail[i-1])

  if(Cl(AMD)[i] > trail1 && AMD$AMD.lagCl[i] > trail1) {
    AMD$trail[i] <- max(trail1,coredata(Cl(AMD)[i]+AMD$stopLongATR[i]))
  } else
  if(Cl(AMD)[i] < trail1 && AMD$AMD.lagCl[i] < trail1) {
    AMD$trail[i] <- min(trail1,coredata(Cl(AMD)[i]+AMD$stopShortATR[i]))
  } else
  if(Cl(AMD)[i] > trail1) {
    AMD$trail[i] <- coredata(Cl(AMD)[i]+AMD$stopLongATR[i])
  } else {
    AMD$trail[i] <- coredata(Cl(AMD)[i]+AMD$stopShortATR[i])
  }
}

chartSeries(AMD)
addTA(AMD$trail, on=1)
+7

All Articles