本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Original base concept © orbitonetv | Enhanced & adapted by PremiumTrader57
//@version=6
indicator("Institutional Trend & Liquidity Targets", "Trend Targets Pro", overlay = true, max_lines_count = 500, max_labels_count = 500)
// ======== 1. TREND & FILTER SETTINGS ========
gT = "1. Trend & Confluence"
len = input.int(10, "Channel Length", minval = 2, maxval = 200, group = gT, tooltip = "SMA period of highs/lows forming the dynamic structure channel.")
atrLen = input.int(200, "ATR Volatility Period", minval = 1, group = gT)
bandMult = input.float(0.8, "Channel Expansion (ATR Mult)", minval = 0, step = 0.1, group = gT, tooltip = "Volatility buffer outside the SMA channel to filter minor fakeouts.")
useHtf = input.bool(true, "Filter Signals with HTF Trend EMA", group = gT, tooltip = "Only take Longs above the HTF EMA and Shorts below it.")
htfEmaLen = input.int(200, "HTF Trend EMA Length", minval = 10, group = gT)
confirm = input.bool(true, "Confirm on Bar Close (No Repaint)", group = gT)
sideOpt = input.string("Both", "Allowed Direction", options = ["Both", "Long only", "Short only"], group = gT)
// ======== 2. RISK & TARGET PARAMETERS ========
gH = "2. Targets & Risk Management"
tMode = input.string("ATR", "Distance Unit", options = ["ATR", "Price"], group = gH)
nT = input.int(3, "Active Take-Profit Levels", minval = 1, maxval = 3, group = gH)
tStep = input.float(1.0, "Target Spacing (R / Unit)", minval = 0.01, step = 0.05, group = gH)
slMode = input.string("Fixed distance", "Stop Loss Logic", options = ["Trend line", "Fixed distance"], group = gH)
slDist = input.float(1.0, "Fixed Stop Distance", minval = 0.01, step = 0.05, group = gH)
showLvl = input.bool(true, "Draw Entry / SL / TP Levels", group = gH)
lineLen = input.int(18, "Level Line Projection (Bars)", minval = 3, maxval = 300, group = gH)
onlyLast = input.bool(true, "Show Only Latest Setup Levels", group = gH)
costPct = input.float(0.05, "Est. Round-Trip Fee/Slippage %", minval = 0, step = 0.01, group = gH)
// ======== 3. VISUALS & DASHBOARD ========
gG = "3. Visuals & Analytics"
upCol = input.color(#089981, "Bullish Accent", group = gG)
dnCol = input.color(#F23645, "Bearish Accent", group = gG)
entCol = input.color(#2962FF, "Entry Level Color", group = gG)
lnW = input.int(1, "Level Line Width", minval = 1, maxval = 4, group = gG)
showTL = input.bool(true, "Plot Dynamic Trend Line", group = gG)
showFill = input.bool(true, "Show Structure Fill", group = gG)
showSig = input.bool(true, "Plot Setup Labels", group = gG)
ctrOn = input.bool(false, "Highlight Pullback Candles", group = gG)
ctrCol = input.color(#787B86, "Pullback Candle Color", group = gG)
showTbl = input.bool(true, "Show Performance Dashboard", group = gG)
tblPos = input.string("Bottom right", "Dashboard Position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = gG)
tblSize = input.string("Small", "Dashboard Text Size", options = ["Tiny", "Small", "Normal"], group = gG)
// ======== CORE CALCULATIONS ========
atrRaw = ta.atr(atrLen)
band = atrRaw * bandMult
upper = ta.sma(high, len) + band
lower = ta.sma(low, len) - band
htfEma = ta.ema(close, htfEmaLen)
unit = tMode == "ATR" ? atrRaw : 1.0
okBar = not confirm or barstate.isconfirmed
upX = ta.crossover(close, upper)
dnX = ta.crossunder(close, lower)
htfBull = not useHtf or close > htfEma
htfBear = not useHtf or close < htfEma
var int trend = 0
buySig = false
sellSig = false
if upX and okBar and trend != 1
trend := 1
buySig := sideOpt != "Short only" and htfBull
if dnX and okBar and trend != -1
trend := -1
sellSig := sideOpt != "Long only" and htfBear
trendLine = trend == 1 ? lower : trend == -1 ? upper : na
// ======== SETUP & PERFORMANCE TRACKING ========
var array<line> lns = array.new<line>()
var array<label> lbs = array.new<label>()
var int dir = 0
var int entBar = na
var float ent = na
var float slP = na
var float tp1 = na
var float tp2 = na
var float tp3 = na
var bool h1 = false
var bool h2 = false
var bool h3 = false
var bool done = true
var label lbT1 = na
var label lbT2 = na
var label lbT3 = na
var label lbS = na
var int nSig = 0
var int c1 = 0
var int c2 = 0
var int c3 = 0
var int cSl = 0
var bool open1 = false
var int w1 = 0
var int l1 = 0
var float pnlPct = 0.0
f_mark(label lb, string m) =>
if not na(lb)
label.set_text(lb, label.get_text(lb) + m)
hit1Now = false
slNow = false
if not done and bar_index > entBar
if dir == 1
if low <= slP
done := true
slNow := true
if not h1
cSl += 1
f_mark(lbS, " [SL]")
else
if not h1 and high >= tp1
h1 := true
c1 += 1
hit1Now := true
f_mark(lbT1, " [HIT]")
if nT >= 2 and not h2 and high >= tp2
h2 := true
c2 += 1
f_mark(lbT2, " [HIT]")
if nT >= 3 and not h3 and high >= tp3
h3 := true
c3 += 1
f_mark(lbT3, " [HIT]")
else
if high >= slP
done := true
slNow := true
if not h1
cSl += 1
f_mark(lbS, " [SL]")
else
if not h1 and low <= tp1
h1 := true
c1 += 1
hit1Now := true
f_mark(lbT1, " [HIT]")
if nT >= 2 and not h2 and low <= tp2
h2 := true
c2 += 1
f_mark(lbT2, " [HIT]")
if nT >= 3 and not h3 and low <= tp3
h3 := true
c3 += 1
f_mark(lbT3, " [HIT]")
if (nT == 1 and h1) or (nT == 2 and h2) or (nT == 3 and h3)
done := true
if open1 and slNow and not h1
pnlPct += dir * (slP - ent) / ent * 100 - costPct
l1 += 1
open1 := false
else if open1 and hit1Now
pnlPct += dir * (tp1 - ent) / ent * 100 - costPct
w1 += 1
open1 := false
f_level(float y, color col, string sty, string txt) =>
s = sty == "solid" ? line.style_solid : sty == "dashed" ? line.style_dashed : line.style_dotted
ln = line.new(bar_index, y, bar_index + lineLen, y, color = col, width = sty == "solid" ? lnW + 1 : lnW, style = s)
lb = label.new(bar_index + lineLen, y, txt, style = label.style_label_left, color = color.new(col, 15), textcolor = color.white, size = size.tiny)
array.push(lns, ln)
array.push(lbs, lb)
lb
if buySig or sellSig
if open1
r = dir * (close - ent) / ent * 100 - costPct
pnlPct += r
if r > 0
w1 += 1
else
l1 += 1
open1 := true
if onlyLast
while array.size(lns) > 0
line.delete(array.shift(lns))
while array.size(lbs) > 0
label.delete(array.shift(lbs))
dir := buySig ? 1 : -1
ent := close
entBar := bar_index
slP := slMode == "Trend line" ? (dir == 1 ? lower : upper) : ent - dir * slDist * unit
tp1 := ent + dir * tStep * unit
tp2 := ent + dir * 2 * tStep * unit
tp3 := ent + dir * 3 * tStep * unit
h1 := false
h2 := false
h3 := false
done := false
nSig += 1
sideTxt = dir == 1 ? "ENTRY LONG " : "ENTRY SHORT "
lbS := na
lbT1 := na
lbT2 := na
lbT3 := na
if showLvl
f_level(ent, entCol, "solid", sideTxt + str.tostring(ent, format.mintick))
lbS := f_level(slP, dnCol, "dashed", "SL " + str.tostring(slP, format.mintick))
lbT1 := f_level(tp1, upCol, "dotted", "TP1 " + str.tostring(tp1, format.mintick))
lbT2 := nT >= 2 ? f_level(tp2, upCol, "dotted", "TP2 " + str.tostring(tp2, format.mintick)) : na
lbT3 := nT >= 3 ? f_level(tp3, upCol, "dotted", "TP3 " + str.tostring(tp3, format.mintick)) : na
if showSig
if dir == 1
label.new(bar_index, low, "BUY", yloc = yloc.belowbar, style = label.style_label_up, color = upCol, textcolor = color.white, size = size.small)
else
label.new(bar_index, high, "SELL", yloc = yloc.abovebar, style = label.style_label_down, color = dnCol, textcolor = color.white, size = size.small)
while array.size(lns) > 450
line.delete(array.shift(lns))
while array.size(lbs) > 400
label.delete(array.shift(lbs))
// ======== PLOTS ========
tCol = trend == 1 ? upCol : trend == -1 ? dnCol : color.gray
pTL = plot(showTL ? trendLine : na, "Dynamic Trend Line", color = tCol, linewidth = 2, style = plot.style_linebr)
pMid = plot(hl2, "Mid", display = display.none)
fill(pTL, pMid, color = showFill and showTL ? color.new(tCol, 90) : na, title = "Trend Zone Fill")
plot(useHtf ? htfEma : na, "HTF Trend EMA", color = color.new(#FF9800, 35), linewidth = 1)
ctrBar = (trend == -1 and close > open) or (trend == 1 and close < open)
barcolor(ctrOn and ctrBar ? ctrCol : na)
// ======== PERFORMANCE DASHBOARD ========
var table tb = table.new(tblPos == "Top right" ? position.top_right : tblPos == "Bottom right" ? position.bottom_right : tblPos == "Top left" ? position.top_left : position.bottom_left, 2, 9, border_width = 1, border_color = color.new(color.gray, 60))
tSz = tblSize == "Tiny" ? size.tiny : tblSize == "Small" ? size.small : size.normal
f_pct(int c) =>
nSig > 0 ? str.tostring(float(c) / float(nSig) * 100, "#") + "% (" + str.tostring(c) + ")" : "-"
if showTbl and barstate.islast
bgHdr = color.new(#1E222D, 15)
bgVal = color.new(#2A2E39, 15)
string biasTxt = trend == 1 ? "BULLISH" : trend == -1 ? "BEARISH" : "NEUTRAL"
string dirTxt = dir == 1 ? "LONG @ " : "SHORT @ "
string liveTxt = done ? "" : " (LIVE)"
string setupTxt = dir == 0 ? "-" : dirTxt + str.tostring(ent, format.mintick) + liveTxt
color setupCol = dir == 1 ? upCol : dir == -1 ? dnCol : bgVal
table.cell(tb, 0, 0, "Market Bias (" + str.tostring(len) + ")", bgcolor = bgHdr, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 0, biasTxt, bgcolor = tCol, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 1, "Active Setup", bgcolor = bgHdr, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 1, setupTxt, bgcolor = setupCol, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 2, "Total Setups", bgcolor = bgHdr, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 2, str.tostring(nSig), bgcolor = bgVal, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 3, "TP1 Hit Rate", bgcolor = bgHdr, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 3, f_pct(c1), bgcolor = bgVal, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 4, "TP2 Hit Rate", bgcolor = bgHdr, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 4, nT >= 2 ? f_pct(c2) : "-", bgcolor = bgVal, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 5, "TP3 Hit Rate", bgcolor = bgHdr, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 5, nT >= 3 ? f_pct(c3) : "-", bgcolor = bgVal, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 6, "Stopped Out", bgcolor = bgHdr, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 6, f_pct(cSl), bgcolor = color.new(dnCol, 25), text_color = color.white, text_size = tSz)
tot1 = w1 + l1
string wlExtra = tot1 > 0 ? " (" + str.tostring(float(w1) / float(tot1) * 100, "#") + "%)" : ""
table.cell(tb, 0, 7, "TP1 W/L Ratio", bgcolor = bgHdr, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 7, str.tostring(w1) + "W / " + str.tostring(l1) + "L" + wlExtra, bgcolor = bgVal, text_color = color.white, text_size = tSz)
string pnlSign = pnlPct > 0 ? "+" : ""
table.cell(tb, 0, 8, "Net Return (TP1)", bgcolor = bgHdr, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 8, pnlSign + str.tostring(pnlPct, "#.##") + "%", bgcolor = pnlPct >= 0 ? upCol : dnCol, text_color = color.white, text_size = tSz)
// ======== ALERTS ========
alertcondition(buySig, "Bullish Setup", "BUY SETUP | {{ticker}} | {{interval}} | {{close}}")
alertcondition(sellSig, "Bearish Setup", "SELL SETUP | {{ticker}} | {{interval}} | {{close}}")
alertcondition(hit1Now, "TP1 Reached", "TP1 HIT | {{ticker}} | {{interval}} | {{close}}")
alertcondition(slNow, "Stop Loss Hit", "SL HIT | {{ticker}} | {{interval}} | {{close}}")