本页源码来自 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/
// © JuanDeNogoya
//@version=6
indicator("Institutional Price Action Master [Engulfing + PinBar + Star + BOS]", shorttitle="PriceAction Master v2.1", overlay=true, max_labels_count=500, max_lines_count=500)
// ══════════════════════════════════════════════════════════════════════════════
// 1. INPUTS
// ══════════════════════════════════════════════════════════════════════════════
grp0 = "0. Engine Activation & Display"
langOpt = input.string("English", "Chart Label Language", options=["English", "Español"], group=grp0)
enableEngulf = input.bool(true, "Enable Engulfing Patterns (2-Bar)", group=grp0)
enablePinBar = input.bool(true, "Enable Pin Bar Patterns (1-Bar)", group=grp0)
enableStar = input.bool(true, "Enable Morning / Evening Star (3-Bar)", group=grp0)
showBosMarkers = input.bool(true, "Show Real-Time BOS (Blue)", group=grp0, tooltip="Plots Break of Structure only when price breaks in the direction of the prevailing structural trend.")
showChochRT = input.bool(false, "Show Real-Time CHoCH (Orange)", group=grp0, tooltip="Plots Change of Character when price breaks the last confirmed HL/LH against the structural trend.")
hideWeak = input.bool(false, "Hide Weak / Post-BOS Pause Signals", group=grp0, tooltip="Hides counter-trend signals without overextension or pause signals occurring right after a BOS.")
grpBos = "1. Real-Time Market Structure (BOS & Momentum)"
pivotLen = input.int(5, "Structural Pivot Length (PH/PL)", minval=2, maxval=30, group=grpBos)
bosBuffer = input.int(1, "BOS Anti-Repetition Buffer (bars)", minval=1, maxval=20, group=grpBos)
bosWindow = input.int(4, "Post-BOS Momentum Window (bars)", minval=1, maxval=10, group=grpBos, tooltip="If a valid BOS occurred within the last N bars, opposing signals are classified as Post-BOS Pause rather than Exhaustion.")
surfBars = input.int(12, "EMA 21 'Trend Surfing' Bars", minval=5, maxval=30, group=grpBos, tooltip="If price closes consecutively on the same side of EMA 21 for N bars, the first opposing signal is classified as a Trend Rest / Potential HL.")
grpEma = "2. Value Zones & Elasticity (EMAs)"
emaFastLen = input.int(21, "Fast EMA (Tactical Value)", minval=5, group=grpEma)
emaSlowLen = input.int(200, "Macro EMA (Regime Filter)", minval=50, group=grpEma)
emaTouchAtr = input.float(0.5, "EMA Value Zone Tolerance (ATR)", minval=0.1, maxval=1.5, step=0.1, group=grpEma, tooltip="Maximum distance between pattern extreme and EMA 21/200 to qualify as an EMA Value Zone setup.")
overExtAtr = input.float(2.0, "Overextension Threshold from EMA 21 (ATR)", minval=1.0, maxval=5.0, step=0.2, group=grpEma, tooltip="Minimum distance from EMA 21 (in ATRs) required to trigger Exhaustion / Mean Reversion alerts.")
grpAna = "3. Candle Anatomy Parameters"
strictGap = input.bool(false, "Strict Stock Mode (Require Gap)", group=grpAna)
minEngBodyAtr = input.float(0.6, "[Engulfing] Min Body Size (ATR)", minval=0.2, step=0.1, group=grpAna)
minCloseStr = input.float(0.65, "[Engulfing/Star] Min Close Strength (0.5-0.95)", minval=0.5, maxval=0.95, step=0.05, group=grpAna)
minPinRangeAtr = input.float(0.8, "[Pin Bar] Min Total Range (ATR)", minval=0.2, step=0.1, group=grpAna)
minWickRatio = input.float(0.60, "[Pin Bar] Min Rejection Wick Ratio (0.50-0.85)", minval=0.5, maxval=0.85, step=0.05, group=grpAna)
maxNoseRatio = input.float(0.15, "[Pin Bar] Max Nose Wick Ratio (0.05-0.30)", minval=0.05, maxval=0.30, step=0.05, group=grpAna)
maxStarBodyAtr = input.float(0.45, "[Star] Max Star Bar Body (ATR)", minval=0.1, step=0.05, group=grpAna)
minPenetration = input.float(0.50, "[Star] Min Bar 3 Penetration into Bar 1", minval=0.3, maxval=1.0, step=0.05, group=grpAna)
grpConf = "4. Global Confluences"
swingLookback = input.int(5, "Swing Lookback Bars", minval=2, group=grpConf)
useVolFilter = input.bool(true, "Require Volume > Average", group=grpConf)
volMult = input.float(1.1, "Volume Multiplier (vs SMA 20)", minval=0.5, step=0.1, group=grpConf)
requireSweep = input.bool(false, "Require Liquidity Sweep Globally", group=grpConf)
isEn = langOpt == "English"
// ══════════════════════════════════════════════════════════════════════════════
// 2. CORE CALCULATIONS & MOVING AVERAGES
// ══════════════════════════════════════════════════════════════════════════════
atr = ta.atr(14)
volSma = ta.sma(volume, 20)
emaFast = ta.ema(close, emaFastLen)
emaMacro = ta.ema(close, emaSlowLen)
lowest3 = ta.lowest(low, 3)
highest3 = ta.highest(high, 3)
lowest4 = ta.lowest(low, 4)
highest4 = ta.highest(high, 4)
hasVol = nz(volume) > 0
volBase = (useVolFilter and hasVol) ? (volume > volSma * volMult) : true
volAPlus = hasVol ? (volume > volSma * 1.2) : true
rangeCurr = high - low
bodyCurr = math.abs(close - open)
bodyPrev = math.abs(close[1] - open[1])
bullCloseStr = rangeCurr > 0 ? (close - low) / rangeCurr : 0.0
bearCloseStr = rangeCurr > 0 ? (high - close) / rangeCurr : 0.0
// EMA 21 Trend Surfing Detector (N consecutive closes on the same side of EMA 21)
bullSurfingEma21 = ta.lowest(close - emaFast, surfBars) > 0 and (emaFast > emaMacro)
bearSurfingEma21 = ta.highest(close - emaFast, surfBars) < 0 and (emaFast < emaMacro)
// ══════════════════════════════════════════════════════════════════════════════
// 3. REAL-TIME MARKET STRUCTURE & BOS ENGINE (BAR 0)
// ══════════════════════════════════════════════════════════════════════════════
ph = ta.pivothigh(high, pivotLen, pivotLen)
pl = ta.pivotlow(low, pivotLen, pivotLen)
var float lastPH = na
var int lastPHBar = na
var float lastPL = na
var int lastPLBar = na
var int structTrd = 0
var int lastBosBar = 0
var int lastBosDir = 0
var int lastChochBar = 0
// Evaluate crossovers BEFORE updating pivot memory to prevent false triggers
crossOverPH = ta.crossover(close, nz(lastPH))
crossUnderPL = ta.crossunder(close, nz(lastPL))
if not na(ph)
if not na(lastPH)
structTrd := ph > lastPH ? 1 : -1
lastPH := ph
lastPHBar := bar_index - pivotLen
if not na(pl)
lastPL := pl
lastPLBar := bar_index - pivotLen
bosUp = (structTrd == 1) and not na(lastPH) and crossOverPH
bosDown = (structTrd == -1) and not na(lastPL) and crossUnderPL
canShowBosUp = bosUp and (bar_index - lastBosBar >= bosBuffer) and (lastBosDir != 1 or close > lastPH * 1.001)
canShowBosDown = bosDown and (bar_index - lastBosBar >= bosBuffer) and (lastBosDir != -1 or close < lastPL * 0.999)
chochUpRT = (structTrd == -1) and not na(lastPH) and crossOverPH and (bar_index - lastChochBar >= bosBuffer)
chochDownRT = (structTrd == 1) and not na(lastPL) and crossUnderPL and (bar_index - lastChochBar >= bosBuffer)
if canShowBosUp
lastBosBar := bar_index
lastBosDir := 1
if showBosMarkers
line.new(lastPHBar, lastPH, bar_index, lastPH, color=color.new(color.blue, 20), style=line.style_dotted, width=1)
label.new(bar_index, high, "BOS", style=label.style_label_down, color=color.blue, textcolor=color.white, size=size.tiny)
if canShowBosDown
lastBosBar := bar_index
lastBosDir := -1
if showBosMarkers
line.new(lastPLBar, lastPL, bar_index, lastPL, color=color.new(color.blue, 20), style=line.style_dotted, width=1)
label.new(bar_index, low, "BOS", style=label.style_label_up, color=color.blue, textcolor=color.white, size=size.tiny)
if chochUpRT
lastChochBar := bar_index
structTrd := 1
if showChochRT
line.new(lastPHBar, lastPH, bar_index, lastPH, color=color.new(color.orange, 20), style=line.style_dashed, width=1)
label.new(bar_index, high, "CHoCH", style=label.style_label_down, color=color.orange, textcolor=color.white, size=size.tiny)
if chochDownRT
lastChochBar := bar_index
structTrd := -1
if showChochRT
line.new(lastPLBar, lastPL, bar_index, lastPL, color=color.new(color.orange, 20), style=line.style_dashed, width=1)
label.new(bar_index, low, "CHoCH", style=label.style_label_up, color=color.orange, textcolor=color.white, size=size.tiny)
recentBullBos = ((lastBosDir == 1 and (bar_index - lastBosBar <= bosWindow)) or (structTrd == 1 and (bar_index - lastChochBar <= bosWindow)))
recentBearBos = ((lastBosDir == -1 and (bar_index - lastBosBar <= bosWindow)) or (structTrd == -1 and (bar_index - lastChochBar <= bosWindow)))
// ══════════════════════════════════════════════════════════════════════════════
// 4. CANDLE PATTERN ENGINES (1D PIN BAR, 2D ENGULFING, 3D STAR)
// ══════════════════════════════════════════════════════════════════════════════
// --- 4.1 ENGULFING ---
bullEngulfGap = strictGap ? (open < close[1] and close > open[1]) : (open <= close[1] and close > open[1])
bearEngulfGap = strictGap ? (open > close[1] and close < open[1]) : (open >= close[1] and close < open[1])
isBullEngRaw = enableEngulf and (close[1] < open[1]) and (close > open) and bullEngulfGap and (bodyCurr > bodyPrev) and (bodyCurr >= atr * minEngBodyAtr) and (bodyCurr <= atr * 2.8) and (bullCloseStr >= minCloseStr)
isBearEngRaw = enableEngulf and (close[1] > open[1]) and (close < open) and bearEngulfGap and (bodyCurr > bodyPrev) and (bodyCurr >= atr * minEngBodyAtr) and (bodyCurr <= atr * 2.8) and (bearCloseStr >= minCloseStr)
engPrevDown = close[1] < close[swingLookback] and low <= lowest3
engPrevUp = close[1] > close[swingLookback] and high >= highest3
engSweepBull = low < low[1]
engSweepBear = high > high[1]
engVolCond = volBase and (useVolFilter and hasVol ? volume > volume[1] : true)
bullEngSig = isBullEngRaw and engPrevDown and (requireSweep ? engSweepBull : true) and engVolCond
bearEngSig = isBearEngRaw and engPrevUp and (requireSweep ? engSweepBear : true) and engVolCond
bullEngPlus = bullEngSig and engSweepBull and (close > high[1]) and volAPlus
bearEngPlus = bearEngSig and engSweepBear and (close < low[1]) and volAPlus
// --- 4.2 PIN BARS ---
bodyTop = math.max(open, close)
bodyBot = math.min(open, close)
upperWick = high - bodyTop
lowerWick = bodyBot - low
lowerWickRatio = rangeCurr > 0 ? lowerWick / rangeCurr : 0.0
upperWickRatio = rangeCurr > 0 ? upperWick / rangeCurr : 0.0
bullBodyPos = rangeCurr > 0 ? (high - bodyBot) / rangeCurr : 1.0
bearBodyPos = rangeCurr > 0 ? (bodyTop - low) / rangeCurr : 1.0
validPinSize = (rangeCurr >= atr * minPinRangeAtr) and (rangeCurr <= atr * 3.0)
isBullPinRaw = enablePinBar and validPinSize and (lowerWickRatio >= minWickRatio) and (upperWickRatio <= maxNoseRatio) and (bullBodyPos <= 0.35)
isBearPinRaw = enablePinBar and validPinSize and (upperWickRatio >= minWickRatio) and (lowerWickRatio <= maxNoseRatio) and (bearBodyPos <= 0.35)
bullPinSig = isBullPinRaw and engPrevDown and (requireSweep ? engSweepBull : true) and volBase
bearPinSig = isBearPinRaw and engPrevUp and (requireSweep ? engSweepBear : true) and volBase
bullPinPlus = bullPinSig and engSweepBull and (close > open) and volAPlus
bearPinPlus = bearPinSig and engSweepBear and (close < open) and volAPlus
// --- 4.3 MORNING & EVENING STAR ---
body1 = math.abs(close[2] - open[2])
body2 = math.abs(close[1] - open[1])
body2Top = math.max(open[1], close[1])
body2Bot = math.min(open[1], close[1])
bullPenTarget = close[2] + (body1 * minPenetration)
bearPenTarget = close[2] - (body1 * minPenetration)
starLow = math.min(low[2], math.min(low[1], low))
starHigh = math.max(high[2], math.max(high[1], high))
validV1Size = body1 >= atr[2] * minEngBodyAtr
validStarSize = (body2 <= atr[1] * maxStarBodyAtr) and (body2 < body1 * 0.6)
starPosBull = strictGap ? (body2Top <= close[2]) : (body2Bot <= close[2] and body2Top <= (close[2] + body1 * 0.5))
starPosBear = strictGap ? (body2Bot >= close[2]) : (body2Top >= close[2] and body2Bot >= (close[2] - body1 * 0.5))
validV3Bull = (close > open) and (close >= bullPenTarget) and (close > body2Top) and (bullCloseStr >= minCloseStr - 0.05)
validV3Bear = (close < open) and (close <= bearPenTarget) and (close < body2Bot) and (bearCloseStr >= minCloseStr - 0.05)
isMorningRaw = enableStar and (close[2] < open[2]) and validV1Size and validStarSize and starPosBull and validV3Bull
isEveningRaw = enableStar and (close[2] > open[2]) and validV1Size and validStarSize and starPosBear and validV3Bear
starPrevDown = close[2] < close[2 + swingLookback] and starLow <= lowest4
starPrevUp = close[2] > close[2 + swingLookback] and starHigh >= highest4
starSweepBull = math.min(low[1], low) < low[2]
starSweepBear = math.max(high[1], high) > high[2]
bullStarSig = isMorningRaw and starPrevDown and (requireSweep ? starSweepBull : true) and volBase
bearStarSig = isEveningRaw and starPrevUp and (requireSweep ? starSweepBear : true) and volBase
bullStarPlus = bullStarSig and starSweepBull and (close >= open[2]) and volAPlus
bearStarPlus = bearStarSig and starSweepBear and (close <= open[2]) and volAPlus
// ══════════════════════════════════════════════════════════════════════════════
// 5. CONTEXTUAL CLASSIFIER v2.1 (CANDLES + EMAs + BOS + TREND INERTIA)
// ══════════════════════════════════════════════════════════════════════════════
anyBull = bullEngSig or bullPinSig or bullStarSig
anyBear = bearEngSig or bearPinSig or bearStarSig
isAPlus = (anyBull and (bullEngPlus or bullPinPlus or bullStarPlus)) or (anyBear and (bearEngPlus or bearPinPlus or bearStarPlus))
patLow = bullStarSig ? starLow : math.min(low, low[1])
patHigh = bearStarSig ? starHigh : math.max(high, high[1])
string patName = ""
if anyBull
engTxt = isEn ? "ENGULFING" : "ENVOLVENTE"
patName := (bullEngSig ? engTxt : "") + (bullPinSig ? (patName != "" ? " + PIN BAR" : "PIN BAR") : "") + (bullStarSig ? (patName != "" ? " + MORNING STAR" : "MORNING STAR") : "")
if anyBear
engTxt = isEn ? "ENGULFING" : "ENVOLVENTE"
patName := (bearEngSig ? engTxt : "") + (bearPinSig ? (patName != "" ? " + PIN BAR" : "PIN BAR") : "") + (bearStarSig ? (patName != "" ? " + EVENING STAR" : "EVENING STAR") : "")
if isAPlus
patName := patName + " A+"
bullMacro = close > emaMacro
bearMacro = close < emaMacro
touchEma21Bull = (patLow <= emaFast + atr * emaTouchAtr) and (close >= emaFast - atr * 0.25)
touchEma200Bull = (patLow <= emaMacro + atr * emaTouchAtr) and (close >= emaMacro - atr * 0.25)
inBullValueZone = touchEma21Bull or touchEma200Bull
touchEma21Bear = (patHigh >= emaFast - atr * emaTouchAtr) and (close <= emaFast + atr * 0.25)
touchEma200Bear = (patHigh >= emaMacro - atr * emaTouchAtr) and (close <= emaMacro + atr * 0.25)
inBearValueZone = touchEma21Bear or touchEma200Bear
overExtAbove = (patHigh - emaFast) >= atr * overExtAtr
overExtBelow = (emaFast - patLow) >= atr * overExtAtr
isBullMarketShakeout = anyBear and bullMacro and (emaFast > emaMacro) and ((low <= emaFast + atr * 0.4 and close >= emaFast - atr * 0.35) or (low <= emaMacro + atr * 0.4 and close >= emaMacro - atr * 0.35))
isBearMarketShakeout = anyBull and bearMacro and (emaFast < emaMacro) and ((high >= emaFast - atr * 0.4 and close <= emaFast + atr * 0.35) or (high >= emaMacro - atr * 0.4 and close <= emaMacro + atr * 0.35))
string ctxTag = ""
color sigCol = color.gray
bool showSignal = false
// Alert flags for English alertconditions
bool alertBullGradeA = false
bool alertBearGradeA = false
bool alertBullShakeout = false
bool alertBearShakeout = false
bool alertExhaustion = false
bool alertBosBreakout = canShowBosUp or canShowBosDown
if anyBull
if bullMacro and inBullValueZone
ctxTag := canShowBosUp ? (isEn ? "\n[EMA VALUE ZONE + BOS]" : "\n[ZONA VALOR EMA + BOS]") : (isEn ? "\n[EMA VALUE ZONE]" : "\n[ZONA VALOR EMA]")
sigCol := isAPlus ? color.green : color.teal
showSignal := true
alertBullGradeA := true
else if canShowBosUp
ctxTag := isEn ? "\n[BULLISH BOS BREAKOUT]" : "\n[RUPTURA BOS ALCISTA]"
sigCol := color.blue
showSignal := true
alertBullGradeA := true
else if isBearMarketShakeout or bearSurfingEma21
ctxTag := isEn ? "\n[EMA CLASH / ?LH]" : "\n[CHOQUE TREN BAJISTA / ?LH]"
sigCol := color.purple
showSignal := true
alertBearShakeout := true
else if bearMacro and overExtBelow
if recentBearBos
ctxTag := isEn ? "\n[POST-BOS PAUSE]" : "\n[PAUSA POST-BOS BAJISTA]"
sigCol := color.new(color.gray, 20)
showSignal := not hideWeak
else
ctxTag := isEn ? "\n[MEAN REVERSION BOUNCE]" : "\n[REBOTE A LA MEDIA]"
sigCol := color.orange
showSignal := true
alertExhaustion := true
else
ctxTag := bullMacro and overExtAbove ? (isEn ? "\n[OVEREXTENDED]" : "\n[SOBREEXTENDIDA]") : (isEn ? "\n[COUNTER-TREND]" : "\n[CONTRATENDENCIA]")
sigCol := color.new(color.teal, 35)
showSignal := not hideWeak
if anyBear
if bearMacro and inBearValueZone
ctxTag := canShowBosDown ? (isEn ? "\n[EMA VALUE ZONE + BOS]" : "\n[ZONA VALOR EMA + BOS]") : (isEn ? "\n[EMA VALUE ZONE]" : "\n[ZONA VALOR EMA]")
sigCol := isAPlus ? color.red : color.maroon
showSignal := true
alertBearGradeA := true
else if canShowBosDown
ctxTag := isEn ? "\n[BEARISH BOS BREAKOUT]" : "\n[RUPTURA BOS BAJISTA]"
sigCol := color.blue
showSignal := true
alertBearGradeA := true
else if isBullMarketShakeout
ctxTag := isEn ? "\n[EMA SHAKEOUT / ?HL]" : "\n[SHAKEOUT EN EMA / ?HL]"
sigCol := color.purple
showSignal := true
alertBullShakeout := true
else if bullMacro and bullSurfingEma21
ctxTag := isEn ? "\n[EMA21 TREND REST / ?HL]" : "\n[DESCANSO TREN EMA21 / ?HL]"
sigCol := color.purple
showSignal := true
alertBullShakeout := true
else if bullMacro and overExtAbove
if recentBullBos
ctxTag := isEn ? "\n[POST-BOS PAUSE (NO SHORT)]" : "\n[PAUSA POST-BOS (NO VENDER)]"
sigCol := color.new(color.gray, 15)
showSignal := not hideWeak
else
ctxTag := isEn ? "\n[EXHAUSTION / PULLBACK]" : "\n[AGOTAMIENTO / PULLBACK]"
sigCol := color.orange
showSignal := true
alertExhaustion := true
else
ctxTag := recentBullBos ? (isEn ? "\n[POST-BOS PAUSE]" : "\n[PAUSA POST-BOS]") : (isEn ? "\n[COUNTER-TREND]" : "\n[CONTRATENDENCIA]")
sigCol := color.new(color.maroon, 35)
showSignal := not hideWeak
// ══════════════════════════════════════════════════════════════════════════════
// 6. PLOTS & ENGLISH ALERTCONDITIONS
// ══════════════════════════════════════════════════════════════════════════════
plot(emaFast, "Fast EMA (21)", color=color.new(color.aqua, 30), linewidth=1)
plot(emaMacro, "Macro EMA (200)", color=color.new(color.orange, 15), linewidth=2)
barcolor((anyBull or anyBear) and showSignal ? sigCol : na, title="Signal Bar Color")
if anyBull and showSignal
label.new(bar_index, patLow, patName + ctxTag, style=label.style_label_up, color=sigCol, textcolor=color.white, size=size.small)
if anyBear and showSignal
label.new(bar_index, patHigh, patName + ctxTag, style=label.style_label_down, color=sigCol, textcolor=color.white, size=size.small)
plot(anyBull and showSignal ? patLow : na, "Bullish Stop Loss", color=color.red, style=plot.style_circles, linewidth=2)
plot(anyBear and showSignal ? patHigh : na, "Bearish Stop Loss", color=color.red, style=plot.style_circles, linewidth=2)
// Standardized English Alerts for TradingView Publication
alertcondition(alertBullGradeA, title="1. Bullish Grade-A Setup (EMA Value Zone)", message="Bullish Grade-A Price Action signal in EMA Value Zone on {{ticker}} [{{interval}}]")
alertcondition(alertBearGradeA, title="2. Bearish Grade-A Setup (EMA Value Zone)", message="Bearish Grade-A Price Action signal in EMA Value Zone on {{ticker}} [{{interval}}]")
alertcondition(alertBullShakeout, title="3. Bull Market Shakeout / Trend Rest (Potential HL)", message="Bearish pattern absorbed at EMA / Trend Rest on {{ticker}} [{{interval}}] - Watch for potential Higher Low (HL)")
alertcondition(alertBearShakeout, title="4. Bear Market Clash / Trend Rest (Potential LH)", message="Bullish pattern rejected at EMA / Trend Rest on {{ticker}} [{{interval}}] - Watch for potential Lower High (LH)")
alertcondition(alertExhaustion, title="5. Overextension / Exhaustion Alert (Mean Reversion)", message="Price Action Exhaustion / Mean Reversion signal detected far from EMA 21 on {{ticker}} [{{interval}}]")
alertcondition(alertBosBreakout, title="6. Real-Time BOS Breakout", message="Real-Time Break of Structure (BOS) confirmed on {{ticker}} [{{interval}}]")
// END OF SCRIPT