本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Price Travel Density", overlay=true)
// ── Inputs ──────────────────────────────────────────────
len = input.int(20, "Window", minval=3)
smoothLen = input.int(3, "Smoothing", minval=1)
hiThr = input.float(0.35, "Efficiency threshold", minval=0.05, maxval=0.95)
fastLen = input.int(2, "Line: fast length", minval=1)
slowLen = input.int(30, "Line: slow length", minval=2)
rankLen = input.int(200, "Percentile window", minval=20)
showBars = input.bool(true, "Color candles")
showSigs = input.bool(true, "Entry / exit signals")
showRank = input.bool(true, "Highlight top 20% density")
showTable = input.bool(true, "Show info table", group="Table")
tblPos = input.string(position.top_right, "Table position", group="Table",
options=[position.top_right, position.top_left, position.bottom_right, position.bottom_left, position.middle_right, position.middle_left])
tblSize = input.string(size.large, "Table text size", group="Table",
options=[size.small, size.normal, size.large, size.huge])
// ── Core calculation ────────────────────────────────────
// net distance and total path travelled
net = close - close[len]
path = ta.sma(ta.tr, len) * len
density = path > 0 ? net / path : 0.0
dens = ta.ema(density, smoothLen)
adens = math.min(math.abs(dens), 1.0)
efficient = adens >= hiThr
rank = ta.percentrank(adens, rankLen)
// ── Density-adaptive line ───────────────────────────────
// follows price closely when density is high, flattens when it is low
fastSC = 2.0 / (fastLen + 1)
slowSC = 2.0 / (slowLen + 1)
sc = math.pow(adens * (fastSC - slowSC) + slowSC, 2)
var float tdLine = na
tdLine := na(tdLine) ? close : tdLine + sc * (close - tdLine)
lineCol = efficient ? (dens > 0 ? color.teal : color.red) : color.gray
plot(tdLine, "Density line", color=lineCol, linewidth=3)
// ── Candle coloring ─────────────────────────────────────
barcolor(showBars and efficient ? (dens > 0 ? color.teal : color.red) : na)
// ── Top 20% density background ──────────────────────────
bgcolor(showRank and rank > 80 ? color.new(color.yellow, 90) : na)
// ── Signals ─────────────────────────────────────────────
up = ta.crossover(adens, hiThr) and dens > 0
down = ta.crossover(adens, hiThr) and dens < 0
fade = ta.crossunder(adens, hiThr)
plotshape(showSigs and up, "Efficient uptrend", style=shape.triangleup,
location=location.belowbar, color=color.teal, size=size.small)
plotshape(showSigs and down, "Efficient downtrend", style=shape.triangledown,
location=location.abovebar, color=color.red, size=size.small)
plotshape(showSigs and fade, "Efficiency lost", style=shape.xcross,
location=location.abovebar, color=color.gray, size=size.tiny)
// ── Info table (dark-theme friendly) ────────────────────
panelBg = #131722
headerBg = #2A2E39
frameCol = #787B86
textCol = #FFFFFF
upCol = #00E676
downCol = #FF5252
neutralBg = #4A4E5A
var table tb = table.new(tblPos, 2, 4,
bgcolor=panelBg, frame_color=frameCol, frame_width=2,
border_color=frameCol, border_width=1)
if barstate.islast and showTable
dirCol = dens > 0 ? upCol : downCol
stateBg = efficient ? (dens > 0 ? color.new(upCol, 25) : color.new(downCol, 25)) : neutralBg
state = efficient ? (dens > 0 ? "EFFICIENT ▲" : "EFFICIENT ▼") : "TIME WASTED"
table.cell(tb, 0, 0, "PRICE TRAVEL DENSITY", text_color=textCol, bgcolor=headerBg,
text_size=tblSize, text_font_family=font.family_monospace)
table.merge_cells(tb, 0, 0, 1, 0)
table.cell(tb, 0, 1, "Density", text_color=textCol, bgcolor=panelBg,
text_halign=text.align_left, text_size=tblSize)
table.cell(tb, 1, 1, str.tostring(dens, "#.##"), text_color=dirCol, bgcolor=panelBg,
text_halign=text.align_right, text_size=tblSize, text_font_family=font.family_monospace)
table.cell(tb, 0, 2, "Percentile", text_color=textCol, bgcolor=panelBg,
text_halign=text.align_left, text_size=tblSize)
table.cell(tb, 1, 2, str.tostring(rank, "#") + "%", text_color=rank > 80 ? color.yellow : textCol,
bgcolor=panelBg, text_halign=text.align_right, text_size=tblSize,
text_font_family=font.family_monospace)
table.cell(tb, 0, 3, "State", text_color=textCol, bgcolor=stateBg,
text_halign=text.align_left, text_size=tblSize)
table.cell(tb, 1, 3, state, text_color=textCol, bgcolor=stateBg,
text_halign=text.align_right, text_size=tblSize, text_font_family=font.family_monospace)