ATR Trailing + Alerts + Price Labels

vineeth059 · study · 76 行 · 点赞 151 · TradingView 原页

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

Pine Script

//License: Released under the Mozilla Public License 2.0 (Open Source)
//🔗 https://www.mozilla.org/en-US/MPL/2.0/
// vineeth059
 
//@version=6
indicator('ATR Trailing + Alerts + Price Labels', overlay = true)
 
atrLen = input.int(14, 'ATR Period')
mult = input.float(2.0, 'ATR Multiplier')
sensitivity = input.int(3, 'Flip Sensitivity (Bars Confirming)', minval = 1)
 
atr = ta.atr(atrLen)
longStop = low - mult * atr
shortStop = high + mult * atr
 
var float trendLine = na
var bool isUptrend = false
var int barsInState = 0
var bool flippedUp = false
var bool flippedDown = false
 
if na(trendLine)
    isUptrend := close > open
    trendLine := isUptrend ? longStop : shortStop
    barsInState := 1
    barsInState
 
confirmDown = close < trendLine
confirmUp = close > trendLine
flippedUp := false
flippedDown := false
 
if isUptrend
    if confirmDown
        barsInState := barsInState + 1
        if barsInState >= sensitivity
            isUptrend := false
            trendLine := shortStop
            barsInState := 1
            flippedDown := true
            flippedDown
        else
            trendLine := math.max(trendLine, longStop)
            trendLine
    else
        trendLine := math.max(trendLine, longStop)
        barsInState := 1
        barsInState
else
    if confirmUp
        barsInState := barsInState + 1
        if barsInState >= sensitivity
            isUptrend := true
            trendLine := longStop
            barsInState := 1
            flippedUp := true
            flippedUp
        else
            trendLine := math.min(trendLine, shortStop)
            trendLine
    else
        trendLine := math.min(trendLine, shortStop)
        barsInState := 1
        barsInState
 
plot(trendLine, title = 'Trend Catcher', color = isUptrend ? color.blue : color.black, linewidth = 2)
 
alertcondition(flippedUp, title = 'Trend Changed to Uptrend', message = 'Trend flipped to UP (🔵)')
alertcondition(flippedDown, title = 'Trend Changed to Downtrend', message = 'Trend flipped to DOWN (⚫)')
 
labelOffset = atr * 01
 
if flippedUp
    label.new(bar_index, low - labelOffset, '🔼 ' + str.tostring(close, format.mintick), style = label.style_label_up, color = color.gray, textcolor = color.white)
if flippedDown
    label.new(bar_index, high + labelOffset, '🔽 ' + str.tostring(close, format.mintick), style = label.style_label_down, color = color.gray, textcolor = color.white)

← 返回列表