Recent Swing Highs & Lows

DenjiTNQ · study · 62 行 · 点赞 52 · TradingView 原页

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

Pine Script

//@version=6
indicator("Recent Swing Highs & Lows", overlay=true, max_labels_count=500, max_lines_count=500)
 
leftLen  = input.int(5, "Pivot Left Bars", minval=1)
rightLen = input.int(5, "Pivot Right Bars (confirmation)", minval=1)
keepN    = input.int(3, "Recent Swings to Show (each side)", minval=1, maxval=50)
lineLen  = input.int(8, "Marker Line Length (bars)", minval=1)
offsetM  = input.float(1.0, "Marker Offset (x ATR)", minval=0.0, step=0.1)
mCol     = input.color(color.black, "Marker Color")
 
var array<label> hiLabels = array.new<label>()
var array<line>  hiVert   = array.new<line>()
var array<line>  hiHorz   = array.new<line>()
var array<label> loLabels = array.new<label>()
var array<line>  loVert   = array.new<line>()
var array<line>  loHorz   = array.new<line>()
var float lastHigh = na
var float lastLow  = na
 
atr = ta.atr(14)
ph  = ta.pivothigh(high, leftLen, rightLen)
pl  = ta.pivotlow(low, leftLen, rightLen)
 
bool newHH = false
bool newLH = false
bool newHL = false
bool newLL = false
 
if not na(ph)
    string txt = na(lastHigh) ? "High" : ph > lastHigh ? "Higher High" : "Lower High"
    newHH := txt == "Higher High"
    newLH := txt == "Lower High"
    int   x  = bar_index - rightLen
    float y  = ph + atr[rightLen] * offsetM
    hiVert.push(line.new(x, ph, x, y, color=mCol, width=1))
    hiHorz.push(line.new(x, y, x + lineLen, y, color=mCol, width=1))
    hiLabels.push(label.new(x + 1, y, txt, style=label.style_label_lower_left, color=color.new(color.white, 100), textcolor=mCol, size=size.small))
    lastHigh := ph
    if hiLabels.size() > keepN
        label.delete(hiLabels.shift())
        line.delete(hiVert.shift())
        line.delete(hiHorz.shift())
 
if not na(pl)
    string txt = na(lastLow) ? "Low" : pl > lastLow ? "Higher Low" : "Lower Low"
    newHL := txt == "Higher Low"
    newLL := txt == "Lower Low"
    int   x  = bar_index - rightLen
    float y  = pl - atr[rightLen] * offsetM
    loVert.push(line.new(x, pl, x, y, color=mCol, width=1))
    loHorz.push(line.new(x, y, x + lineLen, y, color=mCol, width=1))
    loLabels.push(label.new(x + 1, y, txt, style=label.style_label_upper_left, color=color.new(color.white, 100), textcolor=mCol, size=size.small))
    lastLow := pl
    if loLabels.size() > keepN
        label.delete(loLabels.shift())
        line.delete(loVert.shift())
        line.delete(loHorz.shift())
 
alertcondition(newHH, "Higher High", "New Higher High confirmed")
alertcondition(newLH, "Lower High", "New Lower High confirmed")
alertcondition(newHL, "Higher Low", "New Higher Low confirmed")
alertcondition(newLL, "Lower Low", "New Lower Low confirmed")

← 返回列表