Candle Impedance

hasanaksoy199264 · study · 84 行 · 点赞 25 · TradingView 原页

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

Pine Script

//@version=6
indicator("Candle Impedance", overlay=true, max_boxes_count=200)
 
// --- Settings ---
int    avgLen       = input.int(20, "Resistance average period", minval=2)
string measure      = input.string("Body", "Move measurement", options=["Body", "Range (high-low)"])
float  threshold    = input.float(2.0, "Hard threshold (x times the average)", minval=1.0, step=0.1)
int    maxAge       = input.int(100, "Box lifetime (bars)", minval=10)
int    maxBoxes     = input.int(15, "Max simultaneous boxes", minval=1, maxval=100)
bool   deleteBroken = input.bool(false, "Delete broken boxes (if off, they stay gray)")
color  resColor     = input.color(color.red, "Resistance box color")
color  supColor     = input.color(color.green, "Support box color")
 
// --- Resistance calculation: R = volume / move, compared to its average ---
float move   = measure == "Body" ? math.abs(close - open) : high - low
float R      = nz(volume) / math.max(move, syminfo.mintick)
float Rratio = R / ta.sma(R, avgLen)
bool  hard   = Rratio > threshold
 
bool hardUp   = close > open and hard   // high volume, small rise -> resistance
bool hardDown = close < open and hard   // high volume, small drop -> support
 
// --- Box records ---
var array<box> boxList  = array.new<box>()
var array<int> typeList = array.new<int>()   // 1 = resistance, -1 = support
var array<int> ageList  = array.new<int>()
 
bool brokeRes = false
bool brokeSup = false
 
if barstate.isconfirmed
    // 1) Update existing boxes (from last to first)
    int i = array.size(boxList) - 1
    while i >= 0
        box b = array.get(boxList, i)
        int t = array.get(typeList, i)
        int a = array.get(ageList, i) + 1
        array.set(ageList, i, a)
 
        bool isBroken = t == 1 ? close > box.get_top(b) : close < box.get_bottom(b)
        bool expired  = a > maxAge
 
        if isBroken or expired
            if isBroken
                if t == 1
                    brokeRes := true
                else
                    brokeSup := true
            if expired or deleteBroken
                box.delete(b)
            else
                box.set_right(b, bar_index)
                box.set_bgcolor(b, color.new(color.gray, 85))
                box.set_border_color(b, color.gray)
            array.remove(boxList, i)
            array.remove(typeList, i)
            array.remove(ageList, i)
        else
            box.set_right(b, bar_index)
        i -= 1
 
    // 2) Open a new box if there is a new hard bar
    if hardUp or hardDown
        color clr = hardUp ? resColor : supColor
        box newBox = box.new(left=bar_index, top=high, right=bar_index, bottom=low,
                             border_color=clr, bgcolor=color.new(clr, 80))
        array.push(boxList, newBox)
        array.push(typeList, hardUp ? 1 : -1)
        array.push(ageList, 0)
 
    // 3) Limit the number of boxes (delete the oldest)
    while array.size(boxList) > maxBoxes
        box.delete(array.shift(boxList))
        array.shift(typeList)
        array.shift(ageList)
 
// --- Breakout markers and alerts ---
plotshape(brokeRes, "Resistance broken", shape.triangleup,
          location.belowbar, color.lime, size=size.tiny)
plotshape(brokeSup, "Support broken", shape.triangledown,
          location.abovebar, color.orange, size=size.tiny)
 
alertcondition(brokeRes, "Resistance broken", "Hard resistance box broken to the upside")
alertcondition(brokeSup, "Support broken", "Hard support box broken to the downside")

← 返回列表