1H & 4H Swing High-Low Zones icc

ericadanea · study · 128 行 · 点赞 286 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ MPL-2.0
//@version=6
indicator("1H & 4H Swing High-Low Zones", overlay = true, max_boxes_count = 4)
 
// --- Inputs ---
show1hInput = input.bool(true, "Show 1H swing zones", tooltip = "Show zones at the latest confirmed 1-hour swing high and swing low.", group = "Timeframes")
show4hInput = input.bool(true, "Show 4H swing zones", tooltip = "Show zones at the latest confirmed 4-hour swing high and swing low.", group = "Timeframes")
leftBarsInput = input.int(3, "Pivot bars left", minval = 1, tooltip = "Bars before a candidate pivot that must have lower highs or higher lows.", group = "Swing Detection")
rightBarsInput = input.int(3, "Pivot bars right", minval = 1, tooltip = "Bars after a candidate pivot required to confirm the swing. More bars means stronger but later confirmation.", group = "Swing Detection")
atrLengthInput = input.int(14, "ATR length", minval = 1, tooltip = "ATR lookback on each swing's timeframe; used to size the zones.", group = "Zone Size")
atrWidthInput = input.float(0.10, "Zone thickness (ATR multiple)", minval = 0.01, step = 0.01, tooltip = "Zone depth inward from each swing level, measured as a fraction of that timeframe's ATR.", group = "Zone Size")
high1hColorInput = input.color(color.new(#089981, 82), "1H swing high", tooltip = "Fill color for the 1-hour swing-high zone.", group = "Style")
low1hColorInput = input.color(color.new(#089981, 82), "1H swing low", tooltip = "Fill color for the 1-hour swing-low zone.", group = "Style")
high4hColorInput = input.color(color.new(#5b9cf6, 82), "4H swing high", tooltip = "Fill color for the 4-hour swing-high zone.", group = "Style")
low4hColorInput = input.color(color.new(#5b9cf6, 82), "4H swing low", tooltip = "Fill color for the 4-hour swing-low zone.", group = "Style")
 
// --- Confirmed 1H pivots; lower-timeframe arrays preserve all hourly pivots on a 4H chart ---
[pivotHigh1hArray, pivotLow1hArray, pivotTime1hArray, pivotAtr1hArray] = request.security_lower_tf(syminfo.tickerid, "60", [ta.pivothigh(high, leftBarsInput, rightBarsInput)[1], ta.pivotlow(low, leftBarsInput, rightBarsInput)[1], time[rightBarsInput + 1], ta.atr(atrLengthInput)[rightBarsInput + 1]], ignore_invalid_timeframe = true)
 
var float swingHigh1h = na
var float swingLow1h = na
var int swingHighTime1h = na
var int swingLowTime1h = na
var float swingHighAtr1h = na
var float swingLowAtr1h = na
 
if not na(pivotHigh1hArray)
    int hourlyCount = array.size(pivotHigh1hArray)
    if hourlyCount > 0
        for i = 0 to hourlyCount - 1
            float newHigh1h = array.get(pivotHigh1hArray, i)
            float newLow1h = array.get(pivotLow1hArray, i)
            int newPivotTime1h = array.get(pivotTime1hArray, i)
            float newPivotAtr1h = array.get(pivotAtr1hArray, i)
            if not na(newHigh1h) and (na(swingHighTime1h) or newPivotTime1h != swingHighTime1h)
                swingHigh1h := newHigh1h
                swingHighTime1h := newPivotTime1h
                swingHighAtr1h := newPivotAtr1h
            if not na(newLow1h) and (na(swingLowTime1h) or newPivotTime1h != swingLowTime1h)
                swingLow1h := newLow1h
                swingLowTime1h := newPivotTime1h
                swingLowAtr1h := newPivotAtr1h
 
// --- Confirmed 4H pivots ---
[pivotHigh4h, pivotLow4h, pivotTime4h, pivotAtr4h] = request.security(syminfo.tickerid, "240", [ta.pivothigh(high, leftBarsInput, rightBarsInput)[1], ta.pivotlow(low, leftBarsInput, rightBarsInput)[1], time[rightBarsInput + 1], ta.atr(atrLengthInput)[rightBarsInput + 1]], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
 
var float swingHigh4h = na
var float swingLow4h = na
var int swingHighTime4h = na
var int swingLowTime4h = na
var float swingHighAtr4h = na
var float swingLowAtr4h = na
 
if not na(pivotHigh4h) and (na(swingHighTime4h) or pivotTime4h != swingHighTime4h)
    swingHigh4h := pivotHigh4h
    swingHighTime4h := pivotTime4h
    swingHighAtr4h := pivotAtr4h
if not na(pivotLow4h) and (na(swingLowTime4h) or pivotTime4h != swingLowTime4h)
    swingLow4h := pivotLow4h
    swingLowTime4h := pivotTime4h
    swingLowAtr4h := pivotAtr4h
 
// --- Persistent zone drawings ---
var box high1hBox = na
var box low1hBox = na
var box high4hBox = na
var box low4hBox = na
 
if barstate.islast
    if show1hInput and not na(swingHigh1h) and not na(swingHighTime1h) and not na(swingHighAtr1h)
        float highWidth1h = math.max(swingHighAtr1h * atrWidthInput, syminfo.mintick)
        if na(high1hBox)
            high1hBox := box.new(swingHighTime1h, swingHigh1h, time, swingHigh1h - highWidth1h, xloc = xloc.bar_time, extend = extend.right, border_color = color.new(high1hColorInput, 0), bgcolor = high1hColorInput)
        else
            box.set_lefttop(high1hBox, swingHighTime1h, swingHigh1h)
            box.set_rightbottom(high1hBox, time, swingHigh1h - highWidth1h)
            box.set_bgcolor(high1hBox, high1hColorInput)
            box.set_border_color(high1hBox, color.new(high1hColorInput, 0))
        box.set_extend(high1hBox, extend.right)
    else
        if not na(high1hBox)
            box.delete(high1hBox)
            high1hBox := na
 
    if show1hInput and not na(swingLow1h) and not na(swingLowTime1h) and not na(swingLowAtr1h)
        float lowWidth1h = math.max(swingLowAtr1h * atrWidthInput, syminfo.mintick)
        if na(low1hBox)
            low1hBox := box.new(swingLowTime1h, swingLow1h + lowWidth1h, time, swingLow1h, xloc = xloc.bar_time, extend = extend.right, border_color = color.new(low1hColorInput, 0), bgcolor = low1hColorInput)
        else
            box.set_lefttop(low1hBox, swingLowTime1h, swingLow1h + lowWidth1h)
            box.set_rightbottom(low1hBox, time, swingLow1h)
            box.set_bgcolor(low1hBox, low1hColorInput)
            box.set_border_color(low1hBox, color.new(low1hColorInput, 0))
        box.set_extend(low1hBox, extend.right)
    else
        if not na(low1hBox)
            box.delete(low1hBox)
            low1hBox := na
 
    if show4hInput and not na(swingHigh4h) and not na(swingHighTime4h) and not na(swingHighAtr4h)
        float highWidth4h = math.max(swingHighAtr4h * atrWidthInput, syminfo.mintick)
        if na(high4hBox)
            high4hBox := box.new(swingHighTime4h, swingHigh4h, time, swingHigh4h - highWidth4h, xloc = xloc.bar_time, extend = extend.right, border_color = color.new(high4hColorInput, 0), bgcolor = high4hColorInput)
        else
            box.set_lefttop(high4hBox, swingHighTime4h, swingHigh4h)
            box.set_rightbottom(high4hBox, time, swingHigh4h - highWidth4h)
            box.set_bgcolor(high4hBox, high4hColorInput)
            box.set_border_color(high4hBox, color.new(high4hColorInput, 0))
        box.set_extend(high4hBox, extend.right)
    else
        if not na(high4hBox)
            box.delete(high4hBox)
            high4hBox := na
 
    if show4hInput and not na(swingLow4h) and not na(swingLowTime4h) and not na(swingLowAtr4h)
        float lowWidth4h = math.max(swingLowAtr4h * atrWidthInput, syminfo.mintick)
        if na(low4hBox)
            low4hBox := box.new(swingLowTime4h, swingLow4h + lowWidth4h, time, swingLow4h, xloc = xloc.bar_time, extend = extend.right, border_color = color.new(low4hColorInput, 0), bgcolor = low4hColorInput)
        else
            box.set_lefttop(low4hBox, swingLowTime4h, swingLow4h + lowWidth4h)
            box.set_rightbottom(low4hBox, time, swingLow4h)
            box.set_bgcolor(low4hBox, low4hColorInput)
            box.set_border_color(low4hBox, color.new(low4hColorInput, 0))
        box.set_extend(low4hBox, extend.right)
    else
        if not na(low4hBox)
            box.delete(low4hBox)
            low4hBox := na

← 返回列表