R 统计:平均真实范围追踪止损指标

发布于 2024-10-31 03:46:26 字数 218 浏览 1 评论 0原文

我正在使用以下文章:

marintrading.com/106VERV.PDF

创建平均真实范围追踪止损指标在 R 中,我尝试了各种方法来做到这一点,包括 for 循环、pmin 和创建滞后时间序列,但似乎没有任何效果。

你能帮我一下吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄司 2024-11-07 03:46:26

你为什么要尝试创建它?它可以在 TTR 包ATR 函数中找到

更新(更仔细地阅读问题后)。我不确定这是否是解决方案,但我希望它能帮助您朝正确的方向发展。

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)

更新#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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文