Path of least resistence

kiran-posani · study · 92 行 · 点赞 12 · TradingView 原页

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

Pine Script

//@version=6
indicator("Energy Tank v8 (range + time)", shorttitle="Tank8", overlay=false)
 
speed     = input.string("Medium", "Reaction speed", options=["Slow", "Medium"])
thr       = input.float(0.30, "Range threshold (efficiency below this = price going nowhere)", minval=0.05, maxval=0.9, step=0.05)
fullLevel = input.float(2.0, "Full-tank level (x normal)", minval=0.5, step=0.5)
relPct    = input.float(30.0, "Release = tank falls this % from its peak", minval=5, maxval=90, step=5)
addFloor  = input.float(0.5, "Minimum a quiet candle adds while in a range (x normal)", minval=0.0, maxval=1.0, step=0.1)
drainK    = input.float(0.5, "Drain speed when price starts moving", minval=0.05, step=0.05)
decay     = input.float(0.8, "Time-weight decay when price starts moving", minval=0.0, maxval=1.0, step=0.05)
baseDays  = input.int(20, "Normal energy: average of last N days (same candle of the day)", minval=5)
baseLen   = input.int(500, "Tank baseline length (candles)", minval=50)
 
// 1) Energy of each candle = range x volume, compared with the same candle of earlier days
energy = ta.tr(true) * volume
newDay = timeframe.change("D")
var int slot = 0
slot := newDay ? 0 : math.min(slot + 1, 499)
 
var slotAvg = array.new<float>(500, na)
normal  = array.get(slotAvg, slot)
seed    = na(normal) ? energy : normal
updated = seed + (nz(energy, seed) - seed) / baseDays
array.set(slotAvg, slot, updated)
 
ratio = energy / normal
add   = na(ratio) ? 0.0 : math.min(ratio, 5.0)
 
// 2) Is price going nowhere? Kaufman efficiency ratio over 5 lengths (in candles; 1 day to 16 days on a 47-min chart)
er(n) => math.abs(close - close[n]) / math.sum(math.abs(close - close[1]), n)
e1 = er(8)
e2 = er(16)
e3 = er(32)
e4 = er(64)
e5 = er(128)
 
// Slow = all lengths count equally. Medium = recent days count more, so it reacts earlier.
isSlow = speed == "Slow"
erSlow = (e1 + e2 + e3 + e4 + e5) / 5
erMed  = (5 * e1 + 4 * e2 + 3 * e3 + 2 * e4 + e5) / 15
avgEr  = isSlow ? erSlow : erMed
thrUse = isSlow ? thr : thr + 0.02
 
// x: +1 = fully trapped, 0 = at the threshold, negative = price is travelling
x       = nz((thrUse - avgEr) / thrUse)
trapped = x > 0
 
// 3) Time weight: grows by 1 for every candle spent in the range, fades when price travels
var float age = 0.0
age := trapped ? age + 1 : age * decay
 
// 4) Tank: in a range each candle adds energy x tightness x time weight; when price travels the tank drains
//    A quiet candle in a range still adds at least the floor, so tight, low-volume ranges keep filling
var float tank = 0.0
addFill = math.max(add, addFloor)
fill    = addFill * x * age
drainF  = math.max(0.0, 1 - drainK * (-x) * add)
tank := trapped ? tank + fill : tank * drainF
 
// 5) Normalise: tank divided by its own long-run average (1 = normal)
var float base = na
base := na(base) ? tank : base + (tank - base) / baseLen
norm = base > 0 ? tank / base : 0.0
 
// 6) Full, and release (tank falls sharply from its peak after being full)
isFull    = norm >= fullLevel
fullStart = isFull and not isFull[1]
 
var float peak = 0.0
peak := fullStart ? tank : math.max(peak, tank)
 
var bool armed = false
armed := fullStart ? true : armed
armed := (isFull and tank >= peak) ? true : armed
release = armed and tank < peak * (1 - relPct / 100)
armed := release ? false : armed
 
plot(norm, "Tank (x normal)", style=plot.style_columns, color=isFull ? color.orange : color.gray)
hline(fullLevel, "Full line", color=color.red, linestyle=hline.style_solid)
plot(avgEr, "Efficiency (low = range)", display=display.data_window)
plot(age, "Candles in range (time weight)", display=display.data_window)
bgcolor(isFull ? color.new(color.orange, 90) : na, title="Tank full")
bgcolor(release ? color.new(color.aqua, 50) : na, title="Release")
plotshape(release, "Release", shape.diamond, location.top, color.aqua, size=size.small)
 
alertcondition(fullStart, "Tank became FULL", "Energy tank is now FULL")
alertcondition(release, "Tank RELEASE", "Energy tank is releasing")
 
// Warn if the chart has no volume (e.g. NIFTY spot index)
volSum = ta.cum(nz(volume))
if barstate.islast and volSum == 0
    runtime.error("No volume on this symbol. Use NIFTY futures (NIFTY1!).")

← 返回列表