Swing Low Stop Loss Levels

mohamedgr91 · study · 62 行 · 点赞 310 · TradingView 原页

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

Pine Script

//@version=6
indicator("Swing Low Stop Loss Levels", shorttitle = "Swing SL", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// ───── Inputs ─────
grpSwing = "Swing detection"
swingN    = input.int(5, "N (candles on each side)", minval = 1, group = grpSwing, tooltip = "A swing low needs N candles with higher lows on each side. It is confirmed N candles after it forms.")
numLevels = input.int(3, "Number of swing lows to show", minval = 1, maxval = 100, group = grpSwing)
 
grpAtr = "ATR buffer"
atrLen  = input.int(14, "ATR length", minval = 1, group = grpAtr)
atrMult = input.float(0.25, "ATR multiplier", minval = 0.0, step = 0.05, group = grpAtr, tooltip = "Stop level = swing low − (ATR at the swing bar × this multiplier)")
 
grpStyle = "Style"
lineCol      = input.color(color.red, "Line color", group = grpStyle)
lineStyleStr = input.string("Dashed", "Line style", options = ["Solid", "Dashed", "Dotted"], group = grpStyle)
lineWidth    = input.int(1, "Line width", minval = 1, maxval = 4, group = grpStyle)
showLabels   = input.bool(true, "Show price labels", group = grpStyle)
removeBroken = input.bool(false, "Remove a level once price closes below it", group = grpStyle)
 
lineStyle = switch lineStyleStr
    "Solid"  => line.style_solid
    "Dotted" => line.style_dotted
    => line.style_dashed
 
// ───── Calculations ─────
atrVal  = ta.atr(atrLen)
pivotLo = ta.pivotlow(low, swingN, swingN)
 
var array<line>  slLines  = array.new<line>()
var array<label> slLabels = array.new<label>()
var array<float> slPrices = array.new<float>()
 
// New confirmed swing low → create a stop level
if not na(pivotLo)
    pivotBar = bar_index - swingN
    slPrice  = pivotLo - atrMult * atrVal[swingN]
    ln = line.new(pivotBar, slPrice, bar_index, slPrice, color = lineCol, width = lineWidth, style = lineStyle)
    lb = showLabels ? label.new(bar_index, slPrice, "SL " + str.tostring(slPrice, format.mintick), style = label.style_label_left, color = color.new(color.black, 100), textcolor = lineCol, size = size.small) : na
    slLines.push(ln)
    slLabels.push(lb)
    slPrices.push(slPrice)
    // Keep only the most recent N levels
    if slLines.size() > numLevels
        line.delete(slLines.shift())
        label.delete(slLabels.shift())
        slPrices.shift()
 
// Remove levels that price has closed below (optional)
if removeBroken and slLines.size() > 0
    for i = slLines.size() - 1 to 0
        if close < slPrices.get(i)
            line.delete(slLines.remove(i))
            label.delete(slLabels.remove(i))
            slPrices.remove(i)
 
// Keep line ends and labels at the current bar
if slLines.size() > 0
    for i = 0 to slLines.size() - 1
        slLines.get(i).set_x2(bar_index)
        lb = slLabels.get(i)
        if not na(lb)
            lb.set_x(bar_index)

← 返回列表