本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// KP Trend Confluence v1.11 — indicator: 6 trend checks vote; signal when they agree (long and short)
// FAST TF : Chandelier Exit (22, 3, close) direction + Ultimate MA (SMA 20) slope
// MID TF : SuperTrend (10, 3) + Sling Shot (EMA 38 vs 62) + ADX (14) gate
// SLOW TF : close vs EMA 200 + AlphaTrend (14, 1) last signal
// v1.11: show/hide and position option for each table.
// v1.10: strategy -> indicator. Points table (each check +1 / -1, score -6..+6) and a signal log in price points
// (entry/exit = next bar open after the signal, no costs), counted from "Count signals from".
// Default = frozen audit config 2026-11-11: fast 30m / mid 1h / slow 1h, enter 6 of 6, exit below 5, ADX >= 20.
// Chart TF must be <= every group TF. Higher-TF values use closed bars only; signals fire on bar close.
indicator("KP Trend Confluence v1.11", shorttitle = "KP Confluence", overlay = true)
// ───────── Inputs ─────────
string tfFast = input.timeframe("5", "Fast TF (Chandelier + Ultimate MA)", group = "Timeframes")
string tfMid = input.timeframe("15", "Mid TF (SuperTrend + Sling Shot + ADX)", group = "Timeframes")
string tfSlow = input.timeframe("30", "Slow TF (EMA 200 + AlphaTrend)", group = "Timeframes")
int entryN = input.int(6, "Enter when at least N of 6 agree", 4, 6, group = "Rules")
int exitN = input.int(5, "Exit when agreement drops below", 1, 6, group = "Rules")
bool useAdx = input.bool(true, "ADX gate on entry (mid TF)", inline = "adx", group = "Rules")
float adxMin = input.float(20, "", 0, inline = "adx", group = "Rules")
bool fresh = input.bool(true, "Enter only on the bar agreement starts", group = "Rules")
bool doLong = input.bool(true, "Take longs", group = "Rules")
bool doShort = input.bool(true, "Take shorts", group = "Rules")
int startT = input.time(timestamp("2026-11-12T00:00:00"), "Count signals from", group = "Points log")
int logRows = input.int(8, "Signals shown in log", 1, 20, group = "Points log")
string P_TR = "Top right"
string P_TL = "Top left"
string P_BR = "Bottom right"
string P_BL = "Bottom left"
string P_MR = "Middle right"
string P_ML = "Middle left"
bool showPts = input.bool(true, "Show points table", inline = "tp", group = "Tables")
string posPts = input.string("Top right", "", [P_TR, P_TL, P_BR, P_BL, P_MR, P_ML], inline = "tp", group = "Tables")
bool showLog = input.bool(true, "Show signal log", inline = "tl", group = "Tables")
string posLog = input.string("Bottom right", "", [P_TR, P_TL, P_BR, P_BL, P_MR, P_ML], inline = "tl", group = "Tables")
f_pos(string p) =>
p == P_TL ? position.top_left : p == P_BR ? position.bottom_right : p == P_BL ? position.bottom_left : p == P_MR ? position.middle_right : p == P_ML ? position.middle_left : position.top_right
int ceLen = input.int(22, "Chandelier ATR period", 1, group = "Fast")
float ceMult = input.float(3.0, "Chandelier ATR mult", 0.1, step = 0.1, group = "Fast")
int umaLen = input.int(20, "Ultimate MA (SMA) length", 1, group = "Fast")
int umaSmth = input.int(2, "Ultimate MA slope lookback", 1, group = "Fast")
int stLen = input.int(10, "SuperTrend ATR period", 1, group = "Mid")
float stMult = input.float(3.0, "SuperTrend ATR mult", 0.1, step = 0.1, group = "Mid")
int ssFast = input.int(38, "Sling Shot fast EMA", 1, group = "Mid")
int ssSlow = input.int(62, "Sling Shot slow EMA", 1, group = "Mid")
int adxLen = input.int(14, "ADX length", 1, group = "Mid")
int emaLen = input.int(200, "Trend EMA", 1, group = "Slow")
int atAP = input.int(14, "AlphaTrend period", 1, group = "Slow")
float atCoeff = input.float(1.0, "AlphaTrend multiplier", 0.1, step = 0.1, group = "Slow")
bool atRsi = input.bool(false, "AlphaTrend: use RSI instead of MFI (auto when volume missing)", group = "Slow")
// ───────── Group calculations (votes: 1 up, -1 down) ─────────
f_fast(int sh) =>
float a = ceMult * ta.atr(ceLen)
float ls = ta.highest(close, ceLen) - a
float lsP = nz(ls[1], ls)
ls := close[1] > lsP ? math.max(ls, lsP) : ls
float ss = ta.lowest(close, ceLen) + a
float ssP = nz(ss[1], ss)
ss := close[1] < ssP ? math.min(ss, ssP) : ss
var int ce = 1
ce := close > ssP ? 1 : close < lsP ? -1 : ce
float m = ta.sma(close, umaLen)
int uma = m >= m[umaSmth] ? 1 : -1
[ce[sh], uma[sh]]
f_mid(int sh) =>
[stv, d] = ta.supertrend(stMult, stLen)
int st = d < 0 ? 1 : -1
int sl = ta.ema(close, ssFast) > ta.ema(close, ssSlow) ? 1 : -1
[dp, dm, adx] = ta.dmi(adxLen, adxLen)
[st[sh], sl[sh], adx[sh]]
f_slow(int sh) =>
int e = close > ta.ema(close, emaLen) ? 1 : -1
float atrA = ta.sma(ta.tr, atAP)
float upT = low - atrA * atCoeff
float dnT = high + atrA * atCoeff
float rsiV = ta.rsi(close, atAP)
float mfiV = ta.mfi(hlc3, atAP)
bool bull = atRsi or na(mfiV) ? rsiV >= 50 : mfiV >= 50
float at = 0.0
at := bull ? (upT < nz(at[1]) ? nz(at[1]) : upT) : (dnT > nz(at[1]) ? nz(at[1]) : dnT)
bool b = ta.crossover(at, at[2])
bool s = ta.crossunder(at, at[2])
int k1 = ta.barssince(b)
int k2 = ta.barssince(s)
int o1 = ta.barssince(b[1])
int o2 = ta.barssince(s[1])
var int atState = 0
if b and o1 > k2
atState := 1
if s and o2 > k1
atState := -1
[e[sh], atState[sh]]
// ───────── Higher-TF requests (closed bars only) ─────────
int chartSec = timeframe.in_seconds()
f_sh(simple string tf) => timeframe.in_seconds(tf) > chartSec ? 1 : 0
f_la(simple int sh) => sh == 1 ? barmerge.lookahead_on : barmerge.lookahead_off
if barstate.isfirst
if chartSec > math.min(timeframe.in_seconds(tfFast), timeframe.in_seconds(tfMid), timeframe.in_seconds(tfSlow))
runtime.error("KP Confluence: chart timeframe must be at or below every group TF (e.g. 15m chart).")
int shF = f_sh(tfFast)
int shM = f_sh(tfMid)
int shS = f_sh(tfSlow)
[vCE, vUMA] = request.security(syminfo.tickerid, tfFast, f_fast(shF), lookahead = f_la(shF))
[vST, vSL, adxV] = request.security(syminfo.tickerid, tfMid, f_mid(shM), lookahead = f_la(shM))
[vEMA, vAT] = request.security(syminfo.tickerid, tfSlow, f_slow(shS), lookahead = f_la(shS))
f_up(int v) => v == 1 ? 1 : 0
f_dn(int v) => v == -1 ? 1 : 0
int upN = f_up(vCE) + f_up(vUMA) + f_up(vST) + f_up(vSL) + f_up(vEMA) + f_up(vAT)
int dnN = f_dn(vCE) + f_dn(vUMA) + f_dn(vST) + f_dn(vSL) + f_dn(vEMA) + f_dn(vAT)
bool gate = not useAdx or adxV >= adxMin
bool longC = upN >= entryN and gate
bool shortC = dnN >= entryN and gate
bool longGo = fresh ? longC and not longC[1] : longC
bool shortGo = fresh ? shortC and not shortC[1] : shortC
// ───────── Signal tracking (no orders) ─────────
var int pos = 0 // 1 long, -1 short, 0 flat
var int pend = 0 // 1 / -1 = open at next bar open, 2 = close at next bar open
var float entryPx = na
var int entryT = 0
var float totPts = 0.0
var int nSig = 0
var int nWin = 0
var array<int> lgTime = array.new<int>()
var array<int> lgSide = array.new<int>()
var array<float> lgIn = array.new<float>()
var array<float> lgOut = array.new<float>()
var array<float> lgPts = array.new<float>()
// Fills at this bar's open
if pend == 2 and pos != 0
float pts = pos * (open - entryPx)
if entryT >= startT
totPts += pts
nSig += 1
nWin += pts > 0 ? 1 : 0
array.push(lgTime, entryT)
array.push(lgSide, pos)
array.push(lgIn, entryPx)
array.push(lgOut, open)
array.push(lgPts, pts)
if array.size(lgTime) > logRows
array.shift(lgTime)
array.shift(lgSide)
array.shift(lgIn)
array.shift(lgOut)
array.shift(lgPts)
pos := 0
else if pend == 1 or pend == -1
pos := pend
entryPx := open
entryT := time
pend := 0
bool sigLong = false
bool sigShort = false
bool sigExit = false
if barstate.isconfirmed
if pos == 0
if doLong and longGo
pend := 1
sigLong := true
else if doShort and shortGo
pend := -1
sigShort := true
else if (pos > 0 and upN < exitN) or (pos < 0 and dnN < exitN)
pend := 2
sigExit := true
plotshape(sigLong, "Long signal", shape.triangleup, location.belowbar, color.lime, size = size.small)
plotshape(sigShort, "Short signal", shape.triangledown, location.abovebar, color.red, size = size.small)
plotshape(sigExit, "Exit signal", shape.xcross, location.abovebar, color.yellow, size = size.tiny)
alertcondition(sigLong, "Confluence LONG", "KP Confluence: LONG on {{ticker}} at {{close}}")
alertcondition(sigShort, "Confluence SHORT", "KP Confluence: SHORT on {{ticker}} at {{close}}")
alertcondition(sigExit, "Confluence EXIT", "KP Confluence: EXIT on {{ticker}} at {{close}}")
// ───────── Visuals ─────────
bgcolor(upN >= entryN ? color.new(color.lime, 90) : dnN >= entryN ? color.new(color.red, 90) : na, title = "Full agreement")
color cBg = color.new(#131722, 5)
color cTxt = color.white
color cDim = color.silver
var table pts = table.new(f_pos(posPts), 4, 11, bgcolor = cBg, border_width = 1)
var table lg = table.new(f_pos(posLog), 5, 24, bgcolor = cBg, border_width = 1)
f_vote(int row, string name, string tf, int v) =>
color c = v == 1 ? color.lime : v == -1 ? color.red : color.gray
table.cell(pts, 0, row, name, text_color = cDim, text_size = size.small, text_halign = text.align_left)
table.cell(pts, 1, row, tf, text_color = cDim, text_size = size.small)
table.cell(pts, 2, row, v == 1 ? "up" : v == -1 ? "down" : "-", text_color = c, text_size = size.small)
table.cell(pts, 3, row, v == 1 ? "+1" : v == -1 ? "-1" : "0", text_color = c, text_size = size.small)
f_px(float x) => str.tostring(x, "#.00")
if barstate.islast and showPts
int score = upN - dnN
table.cell(pts, 0, 0, "KP Confluence v1.11", text_color = cTxt, text_size = size.small)
table.cell(pts, 1, 0, "TF", text_color = cDim, text_size = size.small)
table.cell(pts, 2, 0, "Vote", text_color = cDim, text_size = size.small)
table.cell(pts, 3, 0, "Pts", text_color = cDim, text_size = size.small)
f_vote(1, "Chandelier", tfFast, vCE)
f_vote(2, "Ultimate MA", tfFast, vUMA)
f_vote(3, "SuperTrend", tfMid, vST)
f_vote(4, "Sling Shot", tfMid, vSL)
f_vote(5, "EMA " + str.tostring(emaLen), tfSlow, vEMA)
f_vote(6, "AlphaTrend", tfSlow, vAT)
color sc = score > 0 ? color.lime : score < 0 ? color.red : color.gray
table.cell(pts, 0, 7, "Score", text_color = cTxt, text_size = size.small, text_halign = text.align_left)
table.cell(pts, 1, 7, str.tostring(upN) + " up", text_color = color.lime, text_size = size.small)
table.cell(pts, 2, 7, str.tostring(dnN) + " dn", text_color = color.red, text_size = size.small)
table.cell(pts, 3, 7, (score > 0 ? "+" : "") + str.tostring(score), text_color = sc, text_size = size.small)
table.cell(pts, 0, 8, "ADX", text_color = cDim, text_size = size.small, text_halign = text.align_left)
table.cell(pts, 1, 8, tfMid, text_color = cDim, text_size = size.small)
table.cell(pts, 2, 8, str.tostring(adxV, "#.0"), text_color = adxV >= adxMin ? color.lime : color.gray, text_size = size.small)
table.cell(pts, 3, 8, useAdx ? (gate ? "pass" : "block") : "off", text_color = gate ? color.lime : color.orange, text_size = size.small)
string zone = longC ? "LONG zone" : shortC ? "SHORT zone" : "no trade"
table.cell(pts, 0, 9, "Zone", text_color = cDim, text_size = size.small, text_halign = text.align_left)
table.cell(pts, 1, 9, zone, text_color = longC ? color.lime : shortC ? color.red : color.gray, text_size = size.small)
table.merge_cells(pts, 1, 9, 3, 9)
string ps = pos > 0 ? "long @ " + f_px(entryPx) : pos < 0 ? "short @ " + f_px(entryPx) : "flat"
float upl = pos != 0 ? pos * (close - entryPx) : na
table.cell(pts, 0, 10, "Signal", text_color = cDim, text_size = size.small, text_halign = text.align_left)
table.cell(pts, 1, 10, ps + (pos != 0 ? " " + (upl >= 0 ? "+" : "") + f_px(upl) : ""),
text_color = pos > 0 ? color.lime : pos < 0 ? color.red : color.gray, text_size = size.small)
table.merge_cells(pts, 1, 10, 3, 10)
// Signal log (points = price difference per 1 oz, no costs)
if barstate.islast and showLog
table.cell(lg, 0, 0, "Signals since " + str.format_time(startT, "yyyy-MM-dd"), text_color = cTxt, text_size = size.small)
table.merge_cells(lg, 0, 0, 4, 0)
table.cell(lg, 0, 1, "Entry time", text_color = cDim, text_size = size.small)
table.cell(lg, 1, 1, "Side", text_color = cDim, text_size = size.small)
table.cell(lg, 2, 1, "In", text_color = cDim, text_size = size.small)
table.cell(lg, 3, 1, "Out", text_color = cDim, text_size = size.small)
table.cell(lg, 4, 1, "Points", text_color = cDim, text_size = size.small)
int n = array.size(lgTime)
int row = 2
if n > 0
for k = n - 1 to 0
float p = array.get(lgPts, k)
int sd = array.get(lgSide, k)
table.cell(lg, 0, row, str.format_time(array.get(lgTime, k), "MM-dd HH:mm"), text_color = cTxt, text_size = size.small)
table.cell(lg, 1, row, sd > 0 ? "Long" : "Short", text_color = sd > 0 ? color.lime : color.red, text_size = size.small)
table.cell(lg, 2, row, f_px(array.get(lgIn, k)), text_color = cTxt, text_size = size.small)
table.cell(lg, 3, row, f_px(array.get(lgOut, k)), text_color = cTxt, text_size = size.small)
table.cell(lg, 4, row, (p >= 0 ? "+" : "") + f_px(p), text_color = p >= 0 ? color.lime : color.red, text_size = size.small)
row += 1
else
table.cell(lg, 0, row, "no closed signals yet", text_color = cDim, text_size = size.small)
table.merge_cells(lg, 0, row, 4, row)
row += 1
table.cell(lg, 0, row, "Signals " + str.tostring(nSig) + " win " + (nSig > 0 ? str.tostring(100.0 * nWin / nSig, "#") + "%" : "-"),
text_color = cTxt, text_size = size.small)
table.merge_cells(lg, 0, row, 3, row)
table.cell(lg, 4, row, (totPts >= 0 ? "+" : "") + f_px(totPts), text_color = totPts >= 0 ? color.lime : color.red, text_size = size.small)