PrivatePlork BBMA BIAS V4

hengkimlong · study · 364 行 · 点赞 11 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("PrivatePlork BBMA BIAS V4", shorttitle="PrivatePlork BIAS V4", overlay=true)
 
// =====================================================
// 1. INPUTS
// =====================================================
grp_vis    = "Visuals & Dashboard"
showDash   = input.bool(true, "Show MTF Dashboard", group=grp_vis)
showPlot   = input.bool(true, "Show Season Labels (S1/S2/S3)", group=grp_vis)
showRePlot = input.bool(true, "Show Clean Re-Entry Labels", group=grp_vis)
showExtPlot= input.bool(true, "Show Extreme Labels (EXT)", group=grp_vis)
 
grp_sync   = "V4 MTF Synchronization Windows"
sync4H     = input.int(3,  "4H Setup Window (4H Bars)",   minval=1, maxval=10, tooltip="3 bars = 12 hours validity", group=grp_sync)
sync1H     = input.int(4,  "1H Setup Window (1H Bars)",   minval=1, maxval=12, tooltip="4 bars = 4 hours validity", group=grp_sync)
sync15M    = input.int(12, "15M Setup Window (15M Bars)", minval=1, maxval=24, tooltip="12 bars = 3 hours validity", group=grp_sync)
 
grp_re     = "Sniper Re-Entry Filter (Anti-Spam)"
reTFMode   = input.string("Current Chart", "Re-Entry Timeframe", options=["Current Chart", "15M", "1H", "4H"], group=grp_re)
reCooldown = input.int(6, "Re-Entry Cooldown (Bars)", minval=2, maxval=20, tooltip="Prevents spamming multiple RE labels on the same pullback wave.", group=grp_re)
useCsmFlt  = input.bool(true, "Require Prior CSM / CSAK Momentum", group=grp_re)
useEma50   = input.bool(true, "Require EMA 50 Trend Alignment", group=grp_re)
useBiasFlt = input.bool(false, "Require 4H + 1H Full Bias Alignment", group=grp_re)
 
// =====================================================
// 2. ORIGINAL BBMA VISUALS
// =====================================================
MidBB = ta.sma(close, 20)
TopBB = MidBB + 2.0 * ta.stdev(close, 20)
LowBB = MidBB - 2.0 * ta.stdev(close, 20)
 
plot(MidBB, color=color.rgb(80, 104, 171), linewidth=2, title="Mid BB")
plot(TopBB, color=color.black, linewidth=1, title="Top BB")
plot(LowBB, color=color.black, linewidth=1, title="Low BB")
 
wma5h  = ta.wma(high, 5)
wma10h = ta.wma(high, 10)
wma5l  = ta.wma(low, 5)
wma10l = ta.wma(low, 10)
 
p5h  = plot(wma5h,  color=color.new(color.red, 90),     title="WMA 5H")
p10h = plot(wma10h, color=color.new(color.orange, 90),   title="WMA 10H")
p5l  = plot(wma5l,  color=color.new(color.fuchsia, 90), title="WMA 5L")
p10l = plot(wma10l, color=color.new(color.gray, 90),     title="WMA 10L")
 
ema50Value = ta.ema(close, 50)
plot(ema50Value, color=color.new(color.purple, 10), linewidth=2, title="EMA 50")
 
fill(p5h, p10h, color=color.new(wma5h > wma10h ? color.gray : color.red, 70),   title="Area High")
fill(p5l, p10l, color=color.new(wma5l > wma10l ? color.green : color.gray, 70), title="Area Low")
 
// =====================================================
// 3. PRO BBMA ENGINE (STRICT CLEAN SETUP RULES)
// =====================================================
f_bbmaEngine(bool reqCsm, bool reqEma50) =>
    _mid   = ta.sma(close, 20)
    _top   = _mid + 2.0 * ta.stdev(close, 20)
    _low   = _mid - 2.0 * ta.stdev(close, 20)
 
    _w5h   = ta.wma(high, 5)
    _w10h  = ta.wma(high, 10)
    _w5l   = ta.wma(low, 5)
    _w10l  = ta.wma(low, 10)
    _ema50 = ta.ema(close, 50)
 
    // 1. BIAS (1 = BULLISH, -1 = BEARISH, 0 = NEUTRAL)
    bullBias = _w5h > _w10h and _w5l > _w10l and close > _ema50
    bearBias = _w5h < _w10h and _w5l < _w10l and close < _ema50
    int bias = bullBias ? 1 : bearBias ? -1 : 0
 
    // 2. EXTREME (MA 5/10 exits BB + Reverse Candle closes back inside BB)
    extBuy  = (_w5l[1] < _low[1] or _w5l < _low) and close > open and close > _low
    extSell = (_w5h[1] > _top[1] or _w5h > _top) and close < open and close < _top
 
    // 3. MHV (Market Hilang Volume - Failed BB Breakout)
    lowTouchAge = ta.barssince(low <= _low)
    topTouchAge = ta.barssince(high >= _top)
 
    hadLowTouch = not na(lowTouchAge) and lowTouchAge >= 1 and lowTouchAge <= 5
    hadTopTouch = not na(topTouchAge) and topTouchAge >= 1 and topTouchAge <= 5
 
    mhvBuy  = hadLowTouch and low > _low and low >= low[1] and close > open and close > _w5l and not extBuy
    mhvSell = hadTopTouch and high < _top and high <= high[1] and close < open and close < _w5h and not extSell
 
    // 4. CSM / CSAK (Candlestick Momentum / Arah Kukuh)
    csmBull = close > _top or (close > _mid and open < _mid and close > _w5h and close > _w10h)
    csmBear = close < _low or (close < _mid and open > _mid and close < _w5l and close < _w10l)
 
    hadBullCsm = not reqCsm or (not na(ta.barssince(csmBull)) and ta.barssince(csmBull) >= 1 and ta.barssince(csmBull) <= 10)
    hadBearCsm = not reqCsm or (not na(ta.barssince(csmBear)) and ta.barssince(csmBear) >= 1 and ta.barssince(csmBear) <= 10)
 
    // 5. SNIPER RE-ENTRY (Clean Rejection off WMA 5/10 + MidBB)
    emaBuyOk  = not reqEma50 or close > _ema50
    emaSellOk = not reqEma50 or close < _ema50
 
    candleRange = high - low
    lowerWick   = math.min(open, close) - low
    upperWick   = high - math.max(open, close)
 
    bullTrend = _w5h > _w10h and _w5l > _w10l and _mid > _mid[1] and emaBuyOk and hadBullCsm
    reBuy     = bullTrend and
                 (low <= _w5l or low <= _w10l) and
                 close > open and
                 close > _w5l and
                 close > _mid and
                 open >= _low and
                 (candleRange > 0 and lowerWick >= candleRange * 0.20)
 
    bearTrend = _w5h < _w10h and _w5l < _w10l and _mid < _mid[1] and emaSellOk and hadBearCsm
    reSell    = bearTrend and
                 (high >= _w5h or high >= _w10h) and
                 close < open and
                 close < _w5h and
                 close < _mid and
                 open <= _top and
                 (candleRange > 0 and upperWick >= candleRange * 0.20)
 
    aboveMid = close > _mid
    belowMid = close < _mid
 
    [extBuy, extSell, mhvBuy, mhvSell, reBuy, reSell, bias, aboveMid, belowMid]
 
// =====================================================
// 4. NON-REPAINTING CONFIRMED HTF ENGINE
// =====================================================
f_confirmedEngine(bool reqCsm, bool reqEma50) =>
    [eb, es, mb, ms, rb, rs, bias, aboveMid, belowMid] = f_bbmaEngine(reqCsm, reqEma50)
 
    ebAge = ta.barssince(eb)
    esAge = ta.barssince(es)
    mbAge = ta.barssince(mb)
    msAge = ta.barssince(ms)
    rbAge = ta.barssince(rb)
    rsAge = ta.barssince(rs)
 
    [rbAge[1], ebAge[1], mbAge[1],
     rsAge[1], esAge[1], msAge[1],
     bias[1], aboveMid[1], belowMid[1],
     rb[1], rs[1]]
 
// =====================================================
// 5. CURRENT CHART ENGINE (FIXED UNPACKING FOR EXTREMES)
// =====================================================
[chartExtBuy, chartExtSell, _, _, chartReBuyRaw, chartReSellRaw, _, chartAboveMid, chartBelowMid] = f_bbmaEngine(useCsmFlt, useEma50)
 
chartExtBuyTrig  = barstate.isconfirmed and chartExtBuy and not chartExtBuy[1]
chartExtSellTrig = barstate.isconfirmed and chartExtSell and not chartExtSell[1]
 
// =====================================================
// 6. MULTI-TIMEFRAME DATA (100% NON-REPAINTING)
// =====================================================
[re15BAge, ext15BAge, mhv15BAge,
 re15SAge, ext15SAge, mhv15SAge,
 bias15M, _, _, re15ExactB, re15ExactS] = request.security(
     syminfo.tickerid, "15", f_confirmedEngine(useCsmFlt, useEma50),
     gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
 
[re1hBAge, ext1hBAge, mhv1hBAge,
 re1hSAge, ext1hSAge, mhv1hSAge,
 bias1H, aboveMid1H, belowMid1H, re1hExactB, re1hExactS] = request.security(
     syminfo.tickerid, "60", f_confirmedEngine(useCsmFlt, useEma50),
     gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
 
[re4hBAge, ext4hBAge, mhv4hBAge,
 re4hSAge, ext4hSAge, mhv4hSAge,
 bias4H, aboveMid4H, belowMid4H, re4hExactB, re4hExactS] = request.security(
     syminfo.tickerid, "240", f_confirmedEngine(useCsmFlt, useEma50),
     gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
 
[_, _, _, _, _, _, dailyBias, _, _, _, _] = request.security(
     syminfo.tickerid, "D", f_confirmedEngine(useCsmFlt, useEma50),
     gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
 
// =====================================================
// 7. SYNCHRONIZATION VALIDATION & STRICT 4H+1H MID-BB GATE
// =====================================================
f_valid(float age, int window) =>
    not na(age) and age < window
 
bothAboveMidBB = aboveMid4H and aboveMid1H
bothBelowMidBB = belowMid4H and belowMid1H
 
// 4H Re-Entry
re4hBuyValid  = f_valid(re4hBAge, sync4H) and bothAboveMidBB
re4hSellValid = f_valid(re4hSAge, sync4H) and bothBelowMidBB
 
// 1H Extreme and Re-Entry
ext1hBuyValid  = f_valid(ext1hBAge, sync1H)
ext1hSellValid = f_valid(ext1hSAge, sync1H)
 
re1hBuyValid  = f_valid(re1hBAge, sync1H) and bothAboveMidBB
re1hSellValid = f_valid(re1hSAge, sync1H) and bothBelowMidBB
 
// 15M Extreme and MHV
ext15BuyValid  = f_valid(ext15BAge, sync15M)
ext15SellValid = f_valid(ext15SAge, sync15M)
 
mhv15BuyValid  = f_valid(mhv15BAge, sync15M)
mhv15SellValid = f_valid(mhv15SAge, sync15M)
 
// =====================================================
// 8. SEASON CONFIRMATION (S1 / S2 / S3)
// =====================================================
s1Buy  = re4hBuyValid and ext1hBuyValid and mhv15BuyValid
s1Sell = re4hSellValid and ext1hSellValid and mhv15SellValid
 
s2Buy  = re4hBuyValid and ext1hBuyValid and ext15BuyValid
s2Sell = re4hSellValid and ext1hSellValid and ext15SellValid
 
s3Buy  = re4hBuyValid and re1hBuyValid and ext15BuyValid
s3Sell = re4hSellValid and re1hSellValid and ext15SellValid
 
s1BuyTrig  = barstate.isconfirmed and s1Buy and not s1Buy[1]
s1SellTrig = barstate.isconfirmed and s1Sell and not s1Sell[1]
 
s2BuyTrig  = barstate.isconfirmed and s2Buy and not s2Buy[1]
s2SellTrig = barstate.isconfirmed and s2Sell and not s2Sell[1]
 
s3BuyTrig  = barstate.isconfirmed and s3Buy and not s3Buy[1]
s3SellTrig = barstate.isconfirmed and s3Sell and not s3Sell[1]
 
anySeasonTrig = s1BuyTrig or s1SellTrig or s2BuyTrig or s2SellTrig or s3BuyTrig or s3SellTrig
 
// =====================================================
// 9. SNIPER RE-ENTRY CONFIRMATION (ZERO SPAM)
// =====================================================
rawReBuySel  = reTFMode == "4H" ? re4hExactB : reTFMode == "1H" ? re1hExactB : reTFMode == "15M" ? re15ExactB : chartReBuyRaw
rawReSellSel = reTFMode == "4H" ? re4hExactS : reTFMode == "1H" ? re1hExactS : reTFMode == "15M" ? re15ExactS : chartReSellRaw
 
chartCandleBuyOk  = close > open and chartAboveMid and close > wma5l
chartCandleSellOk = close < open and chartBelowMid and close < wma5h
 
biasOkBuy  = not useBiasFlt or (bias4H == 1 and bias1H == 1)
biasOkSell = not useBiasFlt or (bias4H == -1 and bias1H == -1)
 
reBuyCandidate  = barstate.isconfirmed and rawReBuySel  and chartCandleBuyOk  and bothAboveMidBB and biasOkBuy
reSellCandidate = barstate.isconfirmed and rawReSellSel and chartCandleSellOk and bothBelowMidBB and biasOkSell
 
var int lastReBuyBar  = na
var int lastReSellBar = na
 
reEntryBuyTrig  = reBuyCandidate  and (na(lastReBuyBar)  or (bar_index - lastReBuyBar)  >= reCooldown)
reEntrySellTrig = reSellCandidate and (na(lastReSellBar) or (bar_index - lastReSellBar) >= reCooldown)
 
if reEntryBuyTrig
    lastReBuyBar := bar_index
 
if reEntrySellTrig
    lastReSellBar := bar_index
 
reEntryBuyConf  = not na(lastReBuyBar)  and (bar_index - lastReBuyBar)  < 2 and bothAboveMidBB
reEntrySellConf = not na(lastReSellBar) and (bar_index - lastReSellBar) < 2 and bothBelowMidBB
 
// =====================================================
// 10. CHART LABELS (EXTREME & RE-ENTRY)
// =====================================================
superBlue = #0066FF
superRed  = #FF0000
 
plotshape(showExtPlot and chartExtBuyTrig, title="EXTREME BUY", style=shape.triangleup, location=location.belowbar,
     color=color.green, text="EXT BUY", textcolor=color.white, size=size.small)
plotshape(showExtPlot and chartExtSellTrig, title="EXTREME SELL", style=shape.triangledown, location=location.abovebar,
     color=color.red, text="EXT SELL", textcolor=color.white, size=size.small)
 
plotshape(showPlot and s1BuyTrig, title="S1 BUY", style=shape.labelup, location=location.belowbar,
     color=color.teal, text="S1 BUY\n(RE-EX-MHV)", textcolor=color.white, size=size.small)
plotshape(showPlot and s1SellTrig, title="S1 SELL", style=shape.labeldown, location=location.abovebar,
     color=color.maroon, text="S1 SELL\n(RE-EX-MHV)", textcolor=color.white, size=size.small)
 
plotshape(showPlot and s2BuyTrig, title="S2 BUY", style=shape.labelup, location=location.belowbar,
     color=color.green, text="S2 BUY\n(RE-EX-EX)", textcolor=color.white, size=size.small)
plotshape(showPlot and s2SellTrig, title="S2 SELL", style=shape.labeldown, location=location.abovebar,
     color=color.red, text="S2 SELL\n(RE-EX-EX)", textcolor=color.white, size=size.small)
 
plotshape(showPlot and s3BuyTrig, title="S3 BUY", style=shape.labelup, location=location.belowbar,
     color=color.lime, text="S3 BUY\n(RE-RE-EX)", textcolor=color.black, size=size.small)
plotshape(showPlot and s3SellTrig, title="S3 SELL", style=shape.labeldown, location=location.abovebar,
     color=color.orange, text="S3 SELL\n(RE-RE-EX)", textcolor=color.black, size=size.small)
 
plotshape(showRePlot and reEntryBuyTrig, title="CLEAN RE BUY", style=shape.triangleup, location=location.belowbar,
     color=superBlue, text="RE BUY", textcolor=superBlue, size=size.small)
plotshape(showRePlot and reEntrySellTrig, title="CLEAN RE SELL", style=shape.triangledown, location=location.abovebar,
     color=superRed, text="RE SELL", textcolor=superRed, size=size.small)
 
// =====================================================
// 11. ALERTS
// =====================================================
alertcondition(chartExtBuyTrig,  "EXTREME BUY",  "PrivatePlork V4: Extreme BUY confirmed on {{ticker}} @ {{close}}")
alertcondition(chartExtSellTrig, "EXTREME SELL", "PrivatePlork V4: Extreme SELL confirmed on {{ticker}} @ {{close}}")
 
alertcondition(s1BuyTrig,  "S1 BUY",  "PrivatePlork V4: S1 BUY (RE-EX-MHV) confirmed on {{ticker}} @ {{close}}")
alertcondition(s1SellTrig, "S1 SELL", "PrivatePlork V4: S1 SELL (RE-EX-MHV) confirmed on {{ticker}} @ {{close}}")
 
alertcondition(s2BuyTrig,  "S2 BUY",  "PrivatePlork V4: S2 BUY (RE-EX-EX) confirmed on {{ticker}} @ {{close}}")
alertcondition(s2SellTrig, "S2 SELL", "PrivatePlork V4: S2 SELL (RE-EX-EX) confirmed on {{ticker}} @ {{close}}")
 
alertcondition(s3BuyTrig,  "S3 BUY",  "PrivatePlork V4: S3 BUY (RE-RE-EX) confirmed on {{ticker}} @ {{close}}")
alertcondition(s3SellTrig, "S3 SELL", "PrivatePlork V4: S3 SELL (RE-RE-EX) confirmed on {{ticker}} @ {{close}}")
 
alertcondition(anySeasonTrig, "ANY SEASON", "PrivatePlork V4: MTF Season confirmed on {{ticker}} @ {{close}}")
 
alertcondition(reEntryBuyTrig,  "RE-ENTRY BUY",  "PrivatePlork V4: Clean Re-Entry BUY confirmed on {{ticker}} @ {{close}}")
alertcondition(reEntrySellTrig, "RE-ENTRY SELL", "PrivatePlork V4: Clean Re-Entry SELL confirmed on {{ticker}} @ {{close}}")
 
// =====================================================
// 12. DASHBOARD
// =====================================================
f_seasonText(bool buy, bool sell) =>
    buy ? "BUY CONFIRM" : sell ? "SELL CONFIRM" : "WAITING"
 
f_seasonBg(bool buy, bool sell) =>
    buy ? color.rgb(20, 120, 60) : sell ? color.rgb(160, 35, 35) : color.rgb(155, 150, 140)
 
f_reBg(bool buy, bool sell) =>
    buy ? superBlue : sell ? superRed : color.rgb(155, 150, 140)
 
f_biasText(int b) =>
    b == 1 ? "BULLISH" : b == -1 ? "BEARISH" : "NEUTRAL"
 
f_biasColor(int b) =>
    b == 1 ? color.lime : b == -1 ? color.red : color.yellow
 
var table seasonTable = table.new(position.top_right, 2, 11, border_width=1, border_color=color.rgb(40, 42, 48))
 
if barstate.isfirst
    table.merge_cells(seasonTable, 0, 0, 1, 0)
 
if barstate.islast and showDash
    table.cell(seasonTable, 0, 0, "PrivatePlork BBMA BIAS V4", text_color=color.aqua, bgcolor=color.rgb(28, 33, 44))
 
    table.cell(seasonTable, 0, 1, "BBMA MTF",    text_color=color.white, bgcolor=color.rgb(43, 50, 62))
    table.cell(seasonTable, 1, 1, "CODE STATUS", text_color=color.aqua,  bgcolor=color.rgb(43, 50, 62))
 
    table.cell(seasonTable, 0, 2, "S1 (RE-EX-MHV)", text_color=color.white, bgcolor=color.rgb(75, 76, 80))
    table.cell(seasonTable, 1, 2, f_seasonText(s1Buy, s1Sell), text_color=color.white, bgcolor=f_seasonBg(s1Buy, s1Sell))
 
    table.cell(seasonTable, 0, 3, "S2 (RE-EX-EX)", text_color=color.white, bgcolor=color.rgb(75, 76, 80))
    table.cell(seasonTable, 1, 3, f_seasonText(s2Buy, s2Sell), text_color=color.white, bgcolor=f_seasonBg(s2Buy, s2Sell))
 
    table.cell(seasonTable, 0, 4, "S3 (RE-RE-EX)", text_color=color.white, bgcolor=color.rgb(75, 76, 80))
    table.cell(seasonTable, 1, 4, f_seasonText(s3Buy, s3Sell), text_color=color.white, bgcolor=f_seasonBg(s3Buy, s3Sell))
 
    table.cell(seasonTable, 0, 5, "RE-ENTRY (" + reTFMode + ")", text_color=color.white, bgcolor=color.rgb(60, 65, 75))
    table.cell(seasonTable, 1, 5, f_seasonText(reEntryBuyConf, reEntrySellConf), text_color=color.white, bgcolor=f_reBg(reEntryBuyConf, reEntrySellConf))
 
    string midStatus = bothAboveMidBB ? "ABOVE MID-BB (BUY)" : bothBelowMidBB ? "BELOW MID-BB (SELL)" : "SPLIT / MIXED"
    color  midColor  = bothAboveMidBB ? color.lime : bothBelowMidBB ? color.red : color.yellow
    table.cell(seasonTable, 0, 6, "4H+1H MID-BB", text_color=color.white, bgcolor=color.rgb(50, 55, 65))
    table.cell(seasonTable, 1, 6, midStatus, text_color=midColor, bgcolor=color.rgb(20, 20, 25))
 
    table.cell(seasonTable, 0, 7, "DAILY BIAS", text_color=color.white, bgcolor=color.rgb(45, 45, 50))
    table.cell(seasonTable, 1, 7, f_biasText(dailyBias), text_color=f_biasColor(dailyBias), bgcolor=color.rgb(20, 20, 25))
 
    table.cell(seasonTable, 0, 8, "4H BIAS", text_color=color.white, bgcolor=color.rgb(45, 45, 50))
    table.cell(seasonTable, 1, 8, f_biasText(bias4H), text_color=f_biasColor(bias4H), bgcolor=color.rgb(20, 20, 25))
 
    table.cell(seasonTable, 0, 9, "1H BIAS", text_color=color.white, bgcolor=color.rgb(45, 45, 50))
    table.cell(seasonTable, 1, 9, f_biasText(bias1H), text_color=f_biasColor(bias1H), bgcolor=color.rgb(20, 20, 25))
 
    table.cell(seasonTable, 0, 10, "15M BIAS", text_color=color.white, bgcolor=color.rgb(45, 45, 50))
    table.cell(seasonTable, 1, 10, f_biasText(bias15M), text_color=f_biasColor(bias15M), bgcolor=color.rgb(20, 20, 25))
 
if barstate.islast and not showDash
    table.clear(seasonTable, 0, 0, 1, 10)

← 返回列表