Candle Body Pressure [Leonichev]

leonichevandrei · study · 112 行 · 点赞 7 · TradingView 原页

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

Pine Script

//@version=6
indicator("Candle Body Pressure [Leonichev]", shorttitle = "AL CBP", overlay = false, precision = 2)
 
// =============================================================================
// Candle Body Pressure  ·  v1.0.0  ·  Andrei Leonichev
//
// Measures who controlled recent candles - buyers or sellers - on a fixed
// scale from -1 (only sellers) to +1 (only buyers).
//
// 1. Candle force: the signed body (close - open) plus the wick balance
//    (lower wick - upper wick) × wick weight. A long lower wick counts as
//    buying, a long upper wick as selling.
// 2. Memory: the last N candles are summed with a weight that fades from 1
//    on the current candle to 0 on the oldest one. The decay power shapes the
//    fade: above 1 keeps older candles relevant longer, below 1 forgets faster.
// 3. Pressure = (bullish force - bearish force) / (bullish force + bearish force).
//    Because it is a ratio, it does not depend on the instrument's price or
//    volatility and can be compared across markets.
//
// Two lines are shown: fast pressure (short memory, columns) and slow pressure
// (long memory, line). The value of the current bar updates until the bar
// closes, as with any oscillator; closed bars never change.
// =============================================================================
 
// =============================================================================
// INPUTS
// =============================================================================
grpCalc = "Calculation"
fastMemoryLength = input.int(20, "Fast memory, bars", minval = 2, maxval = 500, group = grpCalc)
fastDecayPower   = input.float(1.0, "Fast decay power", minval = 0.1, maxval = 5.0, step = 0.1, group = grpCalc,
     tooltip = "1 = linear fade. Above 1 keeps older candles relevant longer, below 1 forgets them faster.")
slowMemoryLength = input.int(200, "Slow memory, bars", minval = 2, maxval = 500, group = grpCalc)
slowDecayPower   = input.float(1.0, "Slow decay power", minval = 0.1, maxval = 5.0, step = 0.1, group = grpCalc)
useWicks         = input.bool(true, "Include wicks", group = grpCalc)
wickWeight       = input.float(0.5, "Wick weight", minval = 0.0, maxval = 2.0, step = 0.05, group = grpCalc,
     tooltip = "How much a wick counts relative to a body of the same size.")
 
grpSignals = "Signals"
extremeLevel     = input.float(0.5, "Extreme level", minval = 0.05, maxval = 1.0, step = 0.05, group = grpSignals,
     tooltip = "Fast pressure beyond ±this level is considered one-sided.")
jumpThreshold    = input.float(0.2, "Sharp change, per bar", minval = 0.01, step = 0.01, group = grpSignals,
     tooltip = "A one-bar change of fast pressure at least this large is marked as a sharp change.")
showJumps        = input.bool(false, "Mark sharp changes", group = grpSignals)
 
grpStyle = "Style"
bullColor        = input.color(color.rgb(38, 166, 154), "Buyers", group = grpStyle, inline = "col")
bearColor        = input.color(color.rgb(239, 83, 80), "Sellers", group = grpStyle, inline = "col")
slowColor        = input.color(color.rgb(41, 98, 255), "Slow pressure", group = grpStyle)
 
// =============================================================================
// CALCULATION
// =============================================================================
// Weight of the candle at offset 0..length-1: 1 on the current candle, 0 on the oldest.
memoryWeight(int offset, int length, float power) =>
    x = length > 1 ? float(offset) / (length - 1) : 0.0
    math.pow(math.max(0.0, 1.0 - x), 1.0 / power)
 
// Signed force of the candle at the given offset: body plus wick balance.
candleForce(int offset) =>
    body       = close[offset] - open[offset]
    upperWick  = high[offset] - math.max(open[offset], close[offset])
    lowerWick  = math.min(open[offset], close[offset]) - low[offset]
    useWicks ? body + (lowerWick - upperWick) * wickWeight : body
 
// Weighted buyers-vs-sellers ratio over the last `length` candles, from -1 to +1.
pressure(int length, float power) =>
    bullishForce = 0.0
    bearishForce = 0.0
    for offset = 0 to length - 1
        force = candleForce(offset)
        if not na(force)
            weighted = math.abs(force) * memoryWeight(offset, length, power)
            if force > 0
                bullishForce += weighted
            else
                bearishForce += weighted
    total = bullishForce + bearishForce
    total > 0 ? (bullishForce - bearishForce) / total : 0.0
 
fastPressure = pressure(fastMemoryLength, fastDecayPower)
slowPressure = pressure(slowMemoryLength, slowDecayPower)
 
pressureChange = fastPressure - nz(fastPressure[1], fastPressure)
sharpRise      = pressureChange >= jumpThreshold
sharpDrop      = pressureChange <= -jumpThreshold
crossedUp      = ta.crossover(fastPressure, 0)
crossedDown    = ta.crossunder(fastPressure, 0)
 
// =============================================================================
// PLOTS
// =============================================================================
fastColor = fastPressure >= 0 ? color.from_gradient(fastPressure, 0, 1, color.new(bullColor, 70), bullColor) :
     color.from_gradient(fastPressure, -1, 0, bearColor, color.new(bearColor, 70))
 
plot(fastPressure, "Fast pressure", fastColor, style = plot.style_columns)
plot(slowPressure, "Slow pressure", slowColor, 2)
 
hline(0, "Zero", color.new(color.gray, 50), hline.style_solid)
hline(extremeLevel, "Upper extreme", color.new(color.gray, 60), hline.style_dashed)
hline(-extremeLevel, "Lower extreme", color.new(color.gray, 60), hline.style_dashed)
 
plotshape(showJumps and sharpRise, "Sharp rise", shape.triangleup, location.bottom, bullColor, size = size.tiny)
plotshape(showJumps and sharpDrop, "Sharp drop", shape.triangledown, location.top, bearColor, size = size.tiny)
 
// =============================================================================
// ALERTS
// =============================================================================
alertcondition(crossedUp, "Fast pressure crossed above zero", "Candle Body Pressure: buyers took control on {{ticker}} {{interval}}")
alertcondition(crossedDown, "Fast pressure crossed below zero", "Candle Body Pressure: sellers took control on {{ticker}} {{interval}}")
alertcondition(ta.crossover(fastPressure, extremeLevel), "Fast pressure above upper extreme", "Candle Body Pressure: one-sided buying on {{ticker}} {{interval}}")
alertcondition(ta.crossunder(fastPressure, -extremeLevel), "Fast pressure below lower extreme", "Candle Body Pressure: one-sided selling on {{ticker}} {{interval}}")
alertcondition(sharpRise or sharpDrop, "Sharp change", "Candle Body Pressure: sharp change of pressure on {{ticker}} {{interval}}")

← 返回列表