Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Statistics: Average True Range Trailing Stop indicator

I am using the following article:

marintrading.com/106VERV.PDF

to create the Average True Range Trailing Stop indicator in R. I have tried various ways to do this, including for loops, pmin and creating a lagging time series, and nothing seems to work.

Could you help me out please?

like image 569
Jessica Avatar asked Apr 05 '11 15:04

Jessica


People also ask

How do you use ATR for trailing stop?

ATR Trailing Stops Formula Calculate Average True Range ("ATR") Multiply ATR by your selected multiple — in our case 3 x ATR. In an up-trend, subtract 3 x ATR from Closing Price and plot the result as the stop for the following day. If price closes below the ATR stop, add 3 x ATR to Closing Price — to track a Short ...

Which indicator works best with ATR?

What Is ATR? The average true range is a volatility indicator. Volatility measures the strength of the price action and is often overlooked for clues on market direction. A better known volatility indicator is Bollinger Bands.

What is average true range ATR indicator?

Average True Range (ATR) is the average of true ranges over the specified period. ATR measures volatility, taking into account any gaps in the price movement. Typically, the ATR calculation is based on 14 periods, which can be intraday, daily, weekly, or monthly.

Which is the best indicator for trailing stop-loss?

Chandelier Exits are another common ATR trailing stop-loss indicator that can be applied to price charts, as well as the Parabolic SAR stop-loss indicator, although it is not based on ATR. A moving average can also function as a trailing stop-loss indicator.


1 Answers

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

UPDATE (after reading the question more closely). I'm not exactly sure this is the solution, but I hope it 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:

I missed the trailing stop logic in the article. This code replicates it more closely. Note that the coredata calls are necessary because trail is path-dependent and we need to compare yesterday's values with today's. xts/zoo operations merge by index before the operation, so we need to drop the index before comparing.

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)
like image 82
Joshua Ulrich Avatar answered Nov 03 '22 17:11

Joshua Ulrich