ATR Trailing Stop v5

TradeAutomation · study · 34 行 · 点赞 1,381 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version = 5
// Author = TradeAutomation
 
 
indicator(title="ATR Trailing Stop v5", shorttitle="ATR Trailing Stop", overlay=true, timeframe="", timeframe_gaps=false)
 
 
ATRPeriod = input.int(12, "ATR Period", tooltip = "This is the number of bars back that the script uses to calculate the Average True Range.")
ATRMultiplier = input.float(3, "ATR Multiplier", step=.1, tooltip = "This is the multiple of the ATR average that will function as the trail.")
 
ATR = ta.atr(ATRPeriod)
Stop = ATRMultiplier*ATR
 
var ATRTrailingStop = 0.0
ATRTrailingStop := if close>ATRTrailingStop[1] and close[1]>ATRTrailingStop[1]
    math.max(ATRTrailingStop[1], close-Stop)
else if close<ATRTrailingStop[1] and close[1]<ATRTrailingStop[1]
    math.min(ATRTrailingStop[1], close+Stop)
else if close>ATRTrailingStop[1]
    close-Stop 
else 
    close+Stop
 
var Position = 0.0
Position := if close[1]<ATRTrailingStop[1] and close>ATRTrailingStop[1]
    1
else if close[1]>ATRTrailingStop[1] and close<ATRTrailingStop[1]
    -1
else
    Position[1]
 
PlotColor = Position == -1 ? color.red: Position == 1 ? color.green : color.navy
plot(ATRTrailingStop, color=PlotColor, linewidth=input(1, "Line Width"), title="ATR Trailing Stop")

← 返回列表