Protected Highs & Lows

Too-Short · study · 184 行 · 点赞 170 · TradingView 原页

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

Pine Script

//@version=6
indicator("Protected Highs & Lows", overlay=true, max_labels_count=500, max_lines_count=500, max_bars_back=2000)
 
// Protected high / low only. A protected high (downtrend) / protected low
// (uptrend) is set on a close beyond the last swing, flips on a close through
// it, and ratchets further out on same-direction advances. The swing candle's
// own high/low only counts if 1m data shows it formed after the opposite
// extreme (falls back to excluding it when the chart is 1m or lower).
 
nPivLeft  = input.int(5, "Left Bars", minval=1, maxval=20)
nPivRight = input.int(5, "Right Bars", minval=1, maxval=20)
 
nLookback = 1500
nKeep     = 40
 
// --- 1m DATA FOR SWING-CANDLE ORDERING --------------------------------------------
string nResTf = timeframe.in_seconds("1") < timeframe.in_seconds(timeframe.period) ? "1" : timeframe.period
[nLtfH, nLtfL] = request.security_lower_tf(syminfo.tickerid, nResTf, [high, low])
 
f_nHiAfterLo(hArr, lArr, H, L) =>
    bool res = false
    int  iLo = -1
    int  n = array.size(hArr)
    if n > 0
        for i = 0 to n - 1
            if iLo < 0
                if array.get(lArr, i) <= L
                    iLo := i
            else if array.get(hArr, i) >= H
                res := true
                break
    res
 
f_nLoAfterHi(hArr, lArr, H, L) =>
    bool res = false
    int  iHi = -1
    int  n = array.size(hArr)
    if n > 0
        for i = 0 to n - 1
            if iHi < 0
                if array.get(hArr, i) >= H
                    iHi := i
            else if array.get(lArr, i) <= L
                res := true
                break
    res
 
f_highestSinceCapped(fromBar, cap, inclStart) =>
    int full = bar_index - fromBar
    int n = math.max(0, math.min(full, cap))
    int last = not inclStart and n == full and n > 0 ? n - 1 : n
    float hi = high
    int hb = bar_index
    for i = 0 to last
        if high[i] > hi
            hi := high[i]
            hb := bar_index - i
    [hi, hb]
 
f_lowestSinceCapped(fromBar, cap, inclStart) =>
    int full = bar_index - fromBar
    int n = math.max(0, math.min(full, cap))
    int last = not inclStart and n == full and n > 0 ? n - 1 : n
    float lo = low
    int lb = bar_index
    for i = 0 to last
        if low[i] < lo
            lo := low[i]
            lb := bar_index - i
    [lo, lb]
 
bool nHiAfterLo = f_nHiAfterLo(nLtfH, nLtfL, high, low)
bool nLoAfterHi = f_nLoAfterHi(nLtfH, nLtfL, high, low)
f_nInclHi(fromBar) =>
    nHiAfterLo[math.max(0, math.min(bar_index - fromBar, 1999))]
f_nInclLo(fromBar) =>
    nLoAfterHi[math.max(0, math.min(bar_index - fromBar, 1999))]
 
// --- DRAWING ----------------------------------------------------------------------
var array<label> nPhLabels = array.new<label>()
var array<line>  nPhLines  = array.new<line>()
var array<label> nPlLabels = array.new<label>()
var array<line>  nPlLines  = array.new<line>()
 
f_nDrawLevel(barIdx, price, isPH) =>
    label l = label.new(barIdx, price, isPH ? "PH" : "PL", xloc=xloc.bar_index, yloc=yloc.price, style=isPH ? label.style_label_down : label.style_label_up, color=isPH ? color.new(color.red, 20) : color.new(color.lime, 20), textcolor=isPH ? color.white : color.black, size=size.small)
    line ln = line.new(barIdx, price, bar_index, price, xloc=xloc.bar_index, color=isPH ? color.new(color.red, 40) : color.new(color.lime, 40), width=1, style=line.style_dashed)
    if isPH
        array.push(nPhLabels, l)
        array.push(nPhLines, ln)
        while array.size(nPhLabels) > nKeep
            label.delete(array.shift(nPhLabels))
            line.delete(array.shift(nPhLines))
    else
        array.push(nPlLabels, l)
        array.push(nPlLines, ln)
        while array.size(nPlLabels) > nKeep
            label.delete(array.shift(nPlLabels))
            line.delete(array.shift(nPlLines))
 
// --- SWINGS + TRACKER ---------------------------------------------------------------
nSwingHi = ta.pivothigh(high, nPivLeft, nPivRight)
nSwingLo = ta.pivotlow(low, nPivLeft, nPivRight)
nPivBar  = bar_index - nPivRight
 
var int   nTrend          = 0
var float nProtHi         = na
var float nProtLo         = na
var int   nProtHiBar      = na
var int   nProtLoBar      = na
var float nLastSwingHi    = na
var float nLastSwingLo    = na
var int   nLastSwingHiBar = na
var int   nLastSwingLoBar = na
var float nRefPx          = na
var int   nRefBar         = na
var int   nUpdBar         = na
 
if not na(nSwingHi)
    nLastSwingHi    := nSwingHi
    nLastSwingHiBar := nPivBar
if not na(nSwingLo)
    nLastSwingLo    := nSwingLo
    nLastSwingLoBar := nPivBar
 
if nTrend == 0 and not na(nLastSwingHi) and not na(nLastSwingLo)
    if close > nLastSwingHi
        nTrend := 1
        [lo, lb] = f_lowestSinceCapped(nLastSwingHiBar, nLookback, f_nInclLo(nLastSwingHiBar))
        nProtLo    := lo
        nProtLoBar := lb
        f_nDrawLevel(lb, lo, false)
        nUpdBar    := bar_index
    else if close < nLastSwingLo
        nTrend := -1
        [hi, hb] = f_highestSinceCapped(nLastSwingLoBar, nLookback, f_nInclHi(nLastSwingLoBar))
        nProtHi    := hi
        nProtHiBar := hb
        f_nDrawLevel(hb, hi, true)
        nUpdBar    := bar_index
 
if nTrend == -1 and not na(nSwingLo) and nPivBar >= nUpdBar
    if na(nRefPx) or nSwingLo < nRefPx
        nRefPx  := nSwingLo
        nRefBar := nPivBar
if nTrend == 1 and not na(nSwingHi) and nPivBar >= nUpdBar
    if na(nRefPx) or nSwingHi > nRefPx
        nRefPx  := nSwingHi
        nRefBar := nPivBar
 
if nTrend == -1
    if not na(nProtHi) and close > nProtHi
        nTrend := 1
        [lo, lb] = f_lowestSinceCapped(nProtHiBar, nLookback, f_nInclLo(nProtHiBar))
        nProtLo    := lo
        nProtLoBar := lb
        f_nDrawLevel(lb, lo, false)
        nProtHi    := na
        nRefPx     := na
        nUpdBar    := bar_index
    else if not na(nRefPx) and close < nRefPx
        [hi, hb] = f_highestSinceCapped(nRefBar, nLookback, f_nInclHi(nRefBar))
        nProtHi    := hi
        nProtHiBar := hb
        f_nDrawLevel(hb, hi, true)
        nRefPx     := na
        nUpdBar    := bar_index
else if nTrend == 1
    if not na(nProtLo) and close < nProtLo
        nTrend := -1
        [hi, hb] = f_highestSinceCapped(nProtLoBar, nLookback, f_nInclHi(nProtLoBar))
        nProtHi    := hi
        nProtHiBar := hb
        f_nDrawLevel(hb, hi, true)
        nProtLo    := na
        nRefPx     := na
        nUpdBar    := bar_index
    else if not na(nRefPx) and close > nRefPx
        [lo, lb] = f_lowestSinceCapped(nRefBar, nLookback, f_nInclLo(nRefBar))
        nProtLo    := lo
        nProtLoBar := lb
        f_nDrawLevel(lb, lo, false)
        nRefPx     := na
        nUpdBar    := bar_index

← 返回列表