ATR Trailing Band Reversals | Buy & Sell Signals

TalentedInvestor · study · 59 行 · 点赞 12 · TradingView 原页

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

Pine Script

//@version=6
// v1.3
// ATR trailing bands mark trend reversals.
indicator("ATR Trailing Band Reversals | Buy & Sell Signals",
     shorttitle = "ATR Flip", overlay = true, behind_chart = false,
     timeframe = "", timeframe_gaps = true, format = format.price, precision = 2)
 
const string ATR_SETTINGS = "ATR Settings"
const color BUY_COLOR = #4CAF50
const color SELL_COLOR = #FF5252
 
int atrLength = input.int(10, "ATR Period", minval = 1, group = ATR_SETTINGS, display = display.none)
float priceSource = input.source(hl2, "Source", group = ATR_SETTINGS, display = display.none)
float multiplier = input.float(3.0, "ATR Multiplier", step = 0.1, group = ATR_SETTINGS, display = display.none)
string smoothing = input.string("Wilder (ATR)", "ATR Smoothing",
     options = ["Wilder (ATR)", "Simple (SMA of True Range)"], group = ATR_SETTINGS, display = display.none)
bool showSignals = input.bool(true, "Show Buy/Sell Labels", group = "Markers", display = display.none)
 
trailBand(float rawBand, bool isLower) =>
    float band = rawBand
    float previousBand = nz(band[1], rawBand)
    if isLower and close[1] > previousBand
        band := math.max(rawBand, previousBand)
    else if not isLower and close[1] < previousBand
        band := math.min(rawBand, previousBand)
    [band, previousBand]
 
float simpleAtr = ta.sma(ta.tr, atrLength)
float wilderAtr = ta.atr(atrLength)
float bandWidth = multiplier * (smoothing == "Wilder (ATR)" ? wilderAtr : simpleAtr)
[lowerBand, previousLower] = trailBand(priceSource - bandWidth, true)
[upperBand, previousUpper] = trailBand(priceSource + bandWidth, false)
 
// Today's close is tested against the prior band's level.
int trend = 1
int previousTrend = nz(trend[1], trend)
bool brokeBand = previousTrend == 1 ? close < previousLower : close > previousUpper
trend := brokeBand ? -previousTrend : previousTrend
 
bool buySignal = trend == 1 and trend[1] == -1
bool sellSignal = trend == -1 and trend[1] == 1
float buyMarker = buySignal ? lowerBand : na
float sellMarker = sellSignal ? upperBand : na
 
plotshape(buyMarker, title = "UpTrend Begins", style = shape.circle,
     location = location.absolute, size = size.tiny, color = BUY_COLOR, force_overlay = true)
plotshape(showSignals ? buyMarker : na, title = "Buy", text = "Buy", style = shape.labelup,
     location = location.absolute, size = size.tiny, color = BUY_COLOR,
     textcolor = color.white, force_overlay = true)
plotshape(sellMarker, title = "DownTrend Begins", style = shape.circle,
     location = location.absolute, size = size.tiny, color = SELL_COLOR, force_overlay = true)
plotshape(showSignals ? sellMarker : na, title = "Sell", text = "Sell", style = shape.labeldown,
     location = location.absolute, size = size.tiny, color = SELL_COLOR,
     textcolor = color.white, force_overlay = true)
 
alertcondition(buySignal, title = "ATR Flip Buy", message = "ATR Flip Buy!")
alertcondition(sellSignal, title = "ATR Flip Sell", message = "ATR Flip Sell!")
alertcondition(trend != trend[1], title = "ATR Flip Direction Change",
     message = "ATR Flip has changed direction!")

← 返回列表