Overhead Resistance Boxes

austriancapitaltheory · study · 122 行 · 点赞 11 · TradingView 原页

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

Pine Script

//@version=6
indicator("Overhead Resistance Box", overlay = true, max_boxes_count = 500)
 
// ─── Inputs: Resistance Detection ────────────────────────────────────────────
string grpLogic = "Resistance Logic"
string mode = input.string("Pivot Highs", "Detection Method", 
     options = ["Pivot Highs", "Lookback Highest High", "Manual Price Range"], group = grpLogic)
 
int leftBars  = input.int(10, "Pivot Left Bars", minval = 1, group = grpLogic)
int rightBars = input.int(5, "Pivot Right Bars", minval = 1, group = grpLogic)
int lookback  = input.int(50, "Highest High Lookback Bars", minval = 1, group = grpLogic)
 
string zoneType = input.string("Candle Upper Wick", "Zone Thickness Method", 
     options = ["Candle Upper Wick", "ATR Multiplier", "Fixed Percentage"], group = grpLogic)
float atrMult   = input.float(0.5, "ATR Multiplier", minval = 0.05, step = 0.05, group = grpLogic)
float pctHeight = input.float(1.0, "Fixed Percentage (%)", minval = 0.05, step = 0.1, group = grpLogic)
 
float manualTop = input.price(105.0, "Manual Box Top", group = grpLogic)
float manualBot = input.price(100.0, "Manual Box Bottom", group = grpLogic)
 
// ─── Inputs: Box Management & Breakout Handling ──────────────────────────────
string grpManage = "Box Management"
int maxBoxes     = input.int(5, "Max Active Resistance Boxes", minval = 1, maxval = 100, group = grpManage)
string breakRule = input.string("Stop Extending on Close Above Top", "When Price Breaks Resistance", 
     options = ["Keep Extending Right", "Stop Extending on Close Above Top", "Delete Box on Close Above Top"], group = grpManage)
 
// ─── Inputs: Visual Styling ──────────────────────────────────────────────────
string grpStyle  = "Visual Styling"
color fillColor  = input.color(color.new(color.red, 80), "Fill Color & Opacity", group = grpStyle)
color brdColor   = input.color(color.new(color.red, 20), "Border Color & Opacity", group = grpStyle)
int brdWidth     = input.int(1, "Border Width", minval = 0, maxval = 10, group = grpStyle)
string brdStyleIn = input.string("Solid", "Border Style", options = ["Solid", "Dashed", "Dotted"], group = grpStyle)
 
// ─── Helper Functions ────────────────────────────────────────────────────────
getLineStyle(string s) =>
    switch s
        "Dashed" => line.style_dashed
        "Dotted" => line.style_dotted
        => line.style_solid
 
float atrVal = ta.atr(14)
 
calcBottom(float topPrice, int offset) =>
    float bodyTop = math.max(open[offset], close[offset])
    switch zoneType
        "Candle Upper Wick" => math.min(bodyTop, topPrice - syminfo.mintick)
        "ATR Multiplier"    => topPrice - (atrVal[offset] * atrMult)
        "Fixed Percentage"  => topPrice * (1.0 - pctHeight / 100.0)
        => bodyTop
 
// ─── Mode 1: Pivot Highs (Multiple Dynamic Boxes) ────────────────────────────
var array<box> pivotBoxes = array.new<box>()
float ph = ta.pivothigh(high, leftBars, rightBars)
 
if mode == "Pivot Highs"
    if not na(ph)
        int pivotBar   = bar_index - rightBars
        float topVal   = ph
        float botVal   = calcBottom(topVal, rightBars)
        box newBox     = box.new(
             left         = pivotBar, 
             top          = topVal, 
             right        = bar_index, 
             bottom       = botVal, 
             border_color = brdColor, 
             border_width = brdWidth, 
             border_style = getLineStyle(brdStyleIn), 
             bgcolor      = fillColor, 
             extend       = extend.right)
        pivotBoxes.push(newBox)
        if pivotBoxes.size() > maxBoxes
            box oldBox = pivotBoxes.shift()
            oldBox.delete()
 
    // Check existing boxes for breakouts
    if pivotBoxes.size() > 0 and breakRule != "Keep Extending Right"
        for i = pivotBoxes.size() - 1 to 0
            box b = pivotBoxes.get(i)
            float bTop = b.get_top()
            if close > bTop
                if breakRule == "Stop Extending on Close Above Top"
                    b.set_extend(extend.none)
                    b.set_right(bar_index)
                    pivotBoxes.remove(i)
                else if breakRule == "Delete Box on Close Above Top"
                    b.delete()
                    pivotBoxes.remove(i)
 
// ─── Mode 2 & 3: Single Rolling or Manual Box ────────────────────────────────
var box singleBox = na
 
int highestOffset = ta.highestbars(high, lookback)
float highestVal  = ta.highest(high, lookback)
 
if mode != "Pivot Highs" and barstate.islast
    if not na(singleBox)
        singleBox.delete()
    
    int leftIdx  = bar_index
    float topVal = na
    float botVal = na
 
    if mode == "Lookback Highest High"
        int lbOffset = -highestOffset
        leftIdx      := bar_index - lbOffset
        topVal       := highestVal
        botVal       := calcBottom(topVal, lbOffset)
    else if mode == "Manual Price Range"
        leftIdx      := math.max(0, bar_index - lookback)
        topVal       := math.max(manualTop, manualBot)
        botVal       := math.min(manualTop, manualBot)
 
    singleBox := box.new(
         left         = leftIdx, 
         top          = topVal, 
         right        = bar_index, 
         bottom       = botVal, 
         border_color = brdColor, 
         border_width = brdWidth, 
         border_style = getLineStyle(brdStyleIn), 
         bgcolor      = fillColor, 
         extend       = extend.right)

← 返回列表