本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("30s Pattern → HTF Projection [Pro]", overlay = true, max_lines_count = 500, max_labels_count = 500)
// ───────────────────────── Inputs ─────────────────────────
gTF = "Timeframes"
lowTF = input.timeframe("30S", "Scan Timeframe (Low)", group=gTF)
gDir = "Direction"
tradeDir = input.string("Bearish", "Search For", options=["Bullish", "Bearish"], group=gDir)
gPat = "Pattern Settings"
patStrBull = input.string("LL, LH, HL, HH", "Bullish Sequence", group=gPat)
patStrBear = input.string("HH, HL, LH, LL", "Bearish Sequence", group=gPat)
pivotLen = input.int(3, "Pivot Length", group=gPat)
gProj = "Projection Settings"
projLen = input.int(20, "Projection Length (Bars)", minval=5, maxval=100, group=gProj)
cooldown = input.int(12, "Cooldown (Bars)", minval=1, group=gProj, tooltip="Wait X bars before triggering another signal.")
targetAtr = input.float(2.0, "Target ATR Multiplier", step=0.1, group=gProj)
stopAtr = input.float(1.5, "Stop Loss ATR Multiplier", step=0.1, group=gProj)
useTrendFilter = input.bool(true, "Only project if HTF trend aligns", group=gProj)
htfTrendRes = input.timeframe("15", "Trend Filter Timeframe", group=gProj)
bullCol = input.color(#089981, "Bullish Color", group=gProj)
bearCol = input.color(#F23645, "Bearish Color", group=gProj)
gTbl = "Dashboard Table"
showTable = input.bool(true, "Show Table", group=gTbl)
tablePos = input.string("Top Right", "Position", options=["Top Right", "Top Center", "Top Left", "Middle Right", "Middle Center", "Middle Left", "Bottom Right", "Bottom Center", "Bottom Left"], group=gTbl)
tableSize = input.string("Small", "Text Size", options=["Tiny", "Small", "Normal", "Large"], group=gTbl)
tableBg = input.bool(true, "Solid Dark Background", group=gTbl)
// ───────────────────────── 30s Logic (Background) ─────────────────────────
detect30sPattern(string targetStr) =>
var array<string> types = array.new<string>()
var float lastHighPrice = na
var float lastLowPrice = na
var string[] patSeq = str.split(targetStr, ",")
for i = 0 to array.size(patSeq) - 1
patSeq.set(i, str.trim(patSeq.get(i)))
ph = ta.pivothigh(high, pivotLen, pivotLen)
pl = ta.pivotlow(low, pivotLen, pivotLen)
if not na(ph)
string t = na(lastHighPrice) ? "H" : (ph > lastHighPrice ? "HH" : "LH")
types.push(t)
lastHighPrice := ph
if not na(pl)
string t = na(lastLowPrice) ? "L" : (pl < lastLowPrice ? "LL" : "HL")
types.push(t)
lastLowPrice := pl
if types.size() > 50
types.shift()
int patLen = array.size(patSeq)
bool matched = false
if types.size() >= patLen
bool match = true
for i = 0 to patLen - 1
if types.get(types.size() - patLen + i) != patSeq.get(i)
match := false
break
if match
matched := true
matched ? 1 : 0
lowTFSignal = request.security(syminfo.tickerid, lowTF, detect30sPattern(tradeDir == "Bullish" ? patStrBull : patStrBear), lookahead=barmerge.lookahead_off)
lowTFSignalConfirmed = request.security(syminfo.tickerid, lowTF, detect30sPattern(tradeDir == "Bullish" ? patStrBull : patStrBear)[1], lookahead=barmerge.lookahead_off)
// ───────────────────────── Drawing & Table Logic ─────────────────────────
var array<line> projLines = array.new<line>()
var array<label> projLbls = array.new<label>()
var int lastTriggerBar = -9999
htfEma50 = request.security(syminfo.tickerid, htfTrendRes, ta.ema(close, 50), lookahead=barmerge.lookahead_off)
bool isAlignedTrend = not useTrendFilter or (tradeDir == "Bullish" ? close > htfEma50 : close < htfEma50)
bool rawTrigger = lowTFSignal == 1 and lowTFSignalConfirmed == 0
bool trigger = rawTrigger and (bar_index - lastTriggerBar > cooldown) and isAlignedTrend
var array<string> logTime = array.new<string>()
var array<string> logDir = array.new<string>()
var array<float> logEntry = array.new<float>()
var array<float> logTarget = array.new<float>()
var array<float> logStop = array.new<float>()
var array<string> logStatus = array.new<string>()
var array<int> logTimeMs = array.new<int>()
if trigger and barstate.isconfirmed
float entryPx = close
float atr = ta.atr(14)
float targetPx = tradeDir == "Bullish" ? entryPx + (atr * targetAtr) : entryPx - (atr * targetAtr)
float stopPx = tradeDir == "Bullish" ? entryPx - (atr * stopAtr) : entryPx + (atr * stopAtr)
// Prevent logging historical NaN data
if not na(targetPx) and not na(stopPx) and not na(entryPx)
lastTriggerBar := bar_index
color c = tradeDir == "Bullish" ? bullCol : bearCol
string txt = (tradeDir == "Bullish" ? "30s BULL\nTarget: " : "30s BEAR\nTarget: ") + str.tostring(targetPx, format.mintick)
line newLine = line.new(bar_index, entryPx, bar_index + projLen, targetPx, xloc=xloc.bar_index, color=c, width=2, style=line.style_dashed)
label newLbl = label.new(bar_index + projLen, targetPx, txt, xloc=xloc.bar_index, style=label.style_label_left, color=c, textcolor=color.white, size=size.small)
projLines.push(newLine)
projLbls.push(newLbl)
if projLines.size() > 5
line.delete(projLines.shift())
label.delete(projLbls.shift())
logTime.push(str.format_time(time, "MM-dd HH:mm", syminfo.timezone))
logDir.push(tradeDir == "Bullish" ? "BULL" : "BEAR")
logEntry.push(entryPx)
logTarget.push(targetPx)
logStop.push(stopPx)
logStatus.push("PENDING")
logTimeMs.push(time)
if logTime.size() > 5
logTime.shift()
logDir.shift()
logEntry.shift()
logTarget.shift()
logStop.shift()
logStatus.shift()
logTimeMs.shift()
string alertMsg = "HTF PROJECTION [" + tradeDir + "]\n" +
"Symbol: " + syminfo.ticker + " | TF: " + timeframe.period + "\n" +
"Entry: " + str.tostring(entryPx, format.mintick) + "\n" +
"Target: " + str.tostring(targetPx, format.mintick) + "\n" +
"Stop: " + str.tostring(stopPx, format.mintick)
alert(alertMsg, alert.freq_once_per_bar_close)
// ───────────────────────── Update Table Statuses ─────────────────────────
if barstate.isconfirmed and logStatus.size() > 0
for i = 0 to logStatus.size() - 1
if logStatus.get(i) == "PENDING"
bool isBull = logDir.get(i) == "BULL"
float tgt = logTarget.get(i)
float stp = logStop.get(i)
if (isBull and high >= tgt) or (not isBull and low <= tgt)
logStatus.set(i, "HIT ✅")
else if (isBull and low <= stp) or (not isBull and high >= stp)
logStatus.set(i, "FAIL ❌")
// ───────────────────────── Fancy Draw Table ─────────────────────────
pos = tablePos == "Top Left" ? position.top_left : tablePos == "Bottom Left" ? position.bottom_left : tablePos == "Middle Left" ? position.middle_left : tablePos == "Top Center" ? position.top_center : tablePos == "Bottom Center" ? position.bottom_center : tablePos == "Middle Center" ? position.middle_center : tablePos == "Bottom Right" ? position.bottom_right : tablePos == "Middle Right" ? position.middle_right : position.top_right
tSize = tableSize == "Tiny" ? size.tiny : tableSize == "Normal" ? size.normal : tableSize == "Large" ? size.large : size.small
color bgMain = tableBg ? #131722 : color.new(color.black, 100)
color borderC = color.new(#363A45, 0)
color textMain = #D1D4DC
color textSub = #787B86
color hdrBg = tradeDir == "Bullish" ? #1E3A2F : #3A1E22
var table dash = na
if showTable and na(dash)
dash := table.new(pos, 6, 9, bgcolor=bgMain, border_width=1, border_color=borderC, frame_color=borderC, frame_width=2)
if barstate.islast and showTable and not na(dash)
table.cell(dash, 0, 0, "", width=9)
table.cell(dash, 1, 0, "", width=4)
table.cell(dash, 2, 0, "", width=7)
table.cell(dash, 3, 0, "", width=7)
table.cell(dash, 4, 0, "", width=6)
table.cell(dash, 5, 0, "", width=6)
string titleTxt = tradeDir == "Bullish" ? "▲ BULLISH SETUPS" : "▼ BEARISH SETUPS"
table.cell(dash, 0, 0, titleTxt, bgcolor=hdrBg, text_color=textMain, text_size=size.normal, text_halign=text.align_left, text_formatting=text.format_bold)
table.merge_cells(dash, 0, 0, 4, 0)
table.cell(dash, 5, 0, "TF: " + lowTF, bgcolor=hdrBg, text_color=textSub, text_size=tSize, text_halign=text.align_right)
table.cell(dash, 0, 1, "DATE", text_color=textSub, text_size=tSize, text_halign=text.align_left, tooltip="Date and time the setup triggered.")
table.cell(dash, 1, 1, "DIR", text_color=textSub, text_size=tSize, text_halign=text.align_center)
table.cell(dash, 2, 1, "ENTRY", text_color=textSub, text_size=tSize, text_halign=text.align_right)
table.cell(dash, 3, 1, "TARGET", text_color=textSub, text_size=tSize, text_halign=text.align_right)
table.cell(dash, 4, 1, "AGE", text_color=textSub, text_size=tSize, text_halign=text.align_center, tooltip="Time elapsed since the setup triggered.")
table.cell(dash, 5, 1, "STATUS", text_color=textSub, text_size=tSize, text_halign=text.align_right)
if logTime.size() == 0
table.cell(dash, 0, 2, "Waiting for first signal...", text_color=textSub, text_size=tSize)
table.merge_cells(dash, 0, 2, 5, 2)
else
int wins = 0
int losses = 0
for i = 0 to logTime.size() - 1
int row = logTime.size() - i + 1
bool isBull = logDir.get(i) == "BULL"
color dirCol = isBull ? bullCol : bearCol
string dirText = isBull ? "LONG" : "SHORT"
color dirBg = color.new(dirCol, 85)
string stat = logStatus.get(i)
color stCol = stat == "HIT ✅" ? #089981 : stat == "FAIL ❌" ? #F23645 : textSub
if stat == "HIT ✅"
wins += 1
if stat == "FAIL ❌"
losses += 1
int msElapsed = time - logTimeMs.get(i)
int hoursElapsed = int(msElapsed / 3600000)
int minsElapsed = int((msElapsed % 3600000) / 60000)
string ageStr = str.tostring(hoursElapsed) + "h " + str.tostring(minsElapsed) + "m"
table.cell(dash, 0, row, logTime.get(i), text_color=textMain, text_size=tSize, text_halign=text.align_left)
table.cell(dash, 1, row, dirText, text_color=dirCol, bgcolor=dirBg, text_size=tSize, text_halign=text.align_center)
table.cell(dash, 2, row, str.tostring(logEntry.get(i), format.mintick), text_color=textMain, text_size=tSize, text_halign=text.align_right)
table.cell(dash, 3, row, str.tostring(logTarget.get(i), format.mintick), text_color=textMain, text_size=tSize, text_halign=text.align_right)
table.cell(dash, 4, row, ageStr, text_color=textSub, text_size=tSize, text_halign=text.align_center)
table.cell(dash, 5, row, stat, text_color=stCol, text_size=tSize, text_halign=text.align_right, text_formatting=text.format_bold)
int totalTrades = wins + losses
string wrStr = "—"
color wrCol = textSub
if totalTrades > 0
float wr = (wins / totalTrades) * 100
wrStr := str.tostring(wr, "#.#") + "% (" + str.tostring(wins) + "/" + str.tostring(totalTrades) + ")"
wrCol := wr >= 50 ? #089981 : #F23645
// Locked footer at row 7 (leaves row 8 blank if fewer than 5 trades, preventing overlap)
table.cell(dash, 0, 7, "WIN RATE", text_color=textSub, text_size=tSize, text_halign=text.align_left, text_formatting=text.format_bold)
table.merge_cells(dash, 0, 7, 3, 7)
table.cell(dash, 4, 7, wrStr, text_color=wrCol, text_size=tSize, text_halign=text.align_right, text_formatting=text.format_bold)
table.merge_cells(dash, 4, 7, 5, 7)
// Optional: Highlight background briefly when a signal fires
bgcolor(trigger ? color.new(tradeDir == "Bullish" ? bullCol : bearCol, 85) : na, title="Signal Background")