//@version=6
// ======================================================================================
//  ____                 _   _ _____ _         _____               _            
// |  _ \               | |   (_)__   _| |        |_   _|             | |          
// | |_) | ___   ___ ___| |__  _   | | | |__   ___  | | _ __ __ _  __| | ___ _ __  
// |  _ < / _ \ / __/ __| '_ \| |  | | | '_ \ / _ \ | || '__/ _` |/ _` |/ _ \ '__|
// | |_) | (_) | (_| (__| | | | |  | | | | | |  __/ | || | | (_| | (_| |  __/ |  
// |____/ \___/ \___\___|_| |_|_|  |_| |_| |_|\___| \_/_|  \__,_|\__,_|\___|_|  
//                                                                              
//  Brand     : BocchiTheTrader
//  Indicator : Custom Quantum Dashboard UI (CQDUI)
//  IG        : @emirhancbn18
// ======================================================================================
indicator("BocchiTheTrader | Quantum Trend Matrix", overlay=true)

// ================= Inputs & Modularity =================
// HUD Positioning
posInput = input.string("Top Right", title="Table Position", options=["Top Right", "Bottom Right", "Top Left", "Bottom Left"], group="UI Settings")
table_pos = posInput == "Top Right" ? position.top_right : posInput == "Bottom Right" ? position.bottom_right : posInput == "Top Left" ? position.top_left : position.bottom_left

// Color Palettes
bullish_color = input.color(#17dfad, title="Bullish Color", group="UI Settings")
bearish_color = input.color(#dd326b, title="Bearish Color", group="UI Settings")
neutral_color = input.color(#ff9800, title="Neutral Color", group="UI Settings")
text_color    = input.color(#ffffff, title="Text Color", group="UI Settings")

// Indicator Parameters
smaLen       = input.int(50, title="Trend SMA Length", group="Indicator Settings")
ema_fast_len = input.int(20, title="Fast EMA Length", group="Indicator Settings")
ema_slow_len = input.int(50, title="Slow EMA Length", group="Indicator Settings")

// ================= Structural Mathematics =================
// Trend (SMA)
currTrend   = close > ta.sma(close, smaLen) ? "Bullish" : "Bearish"
dailyTrend  = request.security(syminfo.tickerid, "D", close > ta.sma(close, smaLen) ? "Bullish" : "Bearish")
weeklyTrend = request.security(syminfo.tickerid, "W", close > ta.sma(close, smaLen) ? "Bullish" : "Bearish")

// Volume
volStr = str.tostring(volume, format.volume)

// MACD
macd    = ta.ema(close, 12) - ta.ema(close, 26)
macdSig = ta.ema(macd, 9)
macdTxt = macd > macdSig ? "Buy" : "Sell"

// RSI
rsi    = ta.rsi(close, 14)
rsiTxt = rsi >= 50 ? "Buy" : "Sell"

// Stochastic
k        = ta.stoch(high, low, close, 14)
d        = ta.sma(k, 3)
stochTxt = k > d ? "Buy" : "Sell"

// Fisher Transform (RSI based)
x         = ta.rsi(close, 9) / 100
fish      = 0.5 * math.log((1 + x) / (1 - x))
fisher    = ta.ema(fish, 3)
fisherTxt = fisher > 0 ? "Buy" : "Sell"

// MFI
mfi    = ta.mfi(hlc3, 14)
mfiTxt = mfi > 50 ? "Buy" : "Sell"

// Bollinger
[bbBasis, bbU, bbL] = ta.bb(close, 20, 2.0)
bbTxt = close < bbL ? "Buy" : close > bbU ? "Sell" : "Neutral"

// DMI + ADX
[pDI, mDI, adxVal] = ta.dmi(14, 14)
dmiTxt = pDI > mDI ? "Buy" : "Sell"
adxTxt = adxVal > 25 ? "Trend" : "Weak"

// Dynamic EMA
emaFast = ta.ema(close, ema_fast_len)
emaSlow = ta.ema(close, ema_slow_len)
emaTxt  = emaFast > emaSlow ? "Buy" : "Sell"
ema_row_string = "EMA " + str.tostring(ema_fast_len) + "/" + str.tostring(ema_slow_len)

// ================= Helpers =================
fColor(sig) =>
    sig == "Buy" or sig == "Bullish" or sig == "Trend" ? bullish_color : 
     sig == "Sell" or sig == "Bearish" or sig == "Weak" ? bearish_color : 
     neutral_color

// ================= Table Render Component =================
var table t = table.new(table_pos, 2, 14, border_width=1, frame_color=color.black, bgcolor=color.rgb(20,20,24))

if barstate.islast
    table.cell(t, 0, 0, "Metric", bgcolor=color.gray, text_color=text_color)
    table.cell(t, 1, 0, "Value",  bgcolor=color.gray, text_color=text_color)

    table.cell(t, 0, 1, "Weekly Trend", text_color=text_color)
    table.cell(t, 1, 1, weeklyTrend, bgcolor=fColor(weeklyTrend), text_color=text_color)

    table.cell(t, 0, 2, "Daily Trend", text_color=text_color)
    table.cell(t, 1, 2, dailyTrend, bgcolor=fColor(dailyTrend), text_color=text_color)

    table.cell(t, 0, 3, "Current Trend", text_color=text_color)
    table.cell(t, 1, 3, currTrend, bgcolor=fColor(currTrend), text_color=text_color)

    table.cell(t, 0, 4, "Volume", text_color=text_color)
    table.cell(t, 1, 4, volStr, text_color=neutral_color)

    table.cell(t, 0, 5, "MACD", text_color=text_color)
    table.cell(t, 1, 5, macdTxt, bgcolor=fColor(macdTxt), text_color=text_color)

    table.cell(t, 0, 6, "RSI", text_color=text_color)
    table.cell(t, 1, 6, rsiTxt, bgcolor=fColor(rsiTxt), text_color=text_color)

    table.cell(t, 0, 7, "Stoch", text_color=text_color)
    table.cell(t, 1, 7, stochTxt, bgcolor=fColor(stochTxt), text_color=text_color)

    table.cell(t, 0, 8, "Fisher", text_color=text_color)
    table.cell(t, 1, 8, fisherTxt, bgcolor=fColor(fisherTxt), text_color=text_color)

    table.cell(t, 0, 9, "MFI", text_color=text_color)
    table.cell(t, 1, 9, mfiTxt, bgcolor=fColor(mfiTxt), text_color=text_color)

    table.cell(t, 0, 10, "Bollinger", text_color=text_color)
    table.cell(t, 1, 10, bbTxt, bgcolor=fColor(bbTxt), text_color=text_color)

    table.cell(t, 0, 11, "DMI", text_color=text_color)
    table.cell(t, 1, 11, dmiTxt, bgcolor=fColor(dmiTxt), text_color=text_color)

    table.cell(t, 0, 12, "ADX", text_color=text_color)
    table.cell(t, 1, 12, str.tostring(adxVal, "#.0") + " (" + adxTxt + ")", bgcolor=fColor(adxTxt), text_color=text_color)

    table.cell(t, 0, 13, ema_row_string, text_color=text_color)
    table.cell(t, 1, 13, emaTxt, bgcolor=fColor(emaTxt), text_color=text_color)
    
    
    
    
    
    
    第二个指标
    
    
    // This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
indicator("Fish vs Shark Vote Dashboard (6 Signals)", overlay=true, max_labels_count=500)

//======================
// Inputs
//======================
groupCore = "Core Settings"
useCloseConfirm = input.bool(true, "Use candle CLOSE confirmation (recommended)", group=groupCore)
showBg = input.bool(true, "Background color by vote", group=groupCore)
showLabels = input.bool(true, "Show label when regime changes", group=groupCore)

groupTrend = "Trend Vote"
emaFastLen = input.int(21, "EMA Fast", minval=1, group=groupTrend)
emaSlowLen = input.int(55, "EMA Slow", minval=1, group=groupTrend)

groupTide = "Deep Tide Vote"
rsiLen = input.int(14, "RSI Length", minval=1, group=groupTide)
rsiMaLen = input.int(21, "RSI MA Length", minval=1, group=groupTide)
macdFast = input.int(12, "MACD Fast", minval=1, group=groupTide)
macdSlow = input.int(26, "MACD Slow", minval=1, group=groupTide)
macdSig  = input.int(9, "MACD Signal", minval=1, group=groupTide)

groupBreak = "Breakout / Breakdown Vote"
swingLen = input.int(30, "Swing Lookback (1H)", minval=5, group=groupBreak)
atrLen = input.int(14, "ATR Length", minval=1, group=groupBreak)
atrBeyond = input.float(0.30, "Min ATR beyond level", step=0.05, group=groupBreak)

groupVol = "Volume Vote"
useVolVote = input.bool(true, "Use Volume Vote", group=groupVol)
volLen = input.int(20, "Volume SMA Length", minval=1, group=groupVol)
volMult = input.float(1.10, "Volume > SMA *", step=0.05, group=groupVol)

groupMTF = "Higher Timeframe Vote (optional but powerful)"
useHTFVote = input.bool(true, "Use HTF alignment vote", group=groupMTF)
htfTf = input.timeframe("240", "HTF timeframe (for 1H use 4H)", group=groupMTF)
htfEmaLen = input.int(55, "HTF EMA Length", minval=1, group=groupMTF)
htfRsiLen = input.int(14, "HTF RSI Length", minval=1, group=groupMTF)

//======================
// Helpers
//======================
confirmed = useCloseConfirm ? barstate.isconfirmed : true

fish = "🐟"
shark = "🦈"

//======================
// Votes (6 total)
//======================

// Vote 1: Local trend (EMA fast vs slow)
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
vTrendBull = emaFast > emaSlow
vTrendBear = emaFast < emaSlow

// Vote 2: RSI MA vs 50 (deep bias)
rsi = ta.rsi(close, rsiLen)
rsiMA = ta.rma(rsi, rsiMaLen)
vRsiBull = rsiMA > 50
vRsiBear = rsiMA < 50

// Vote 3: MACD line vs zero (macro momentum)
[macdLine, signalLine, hist] = ta.macd(close, macdFast, macdSlow, macdSig)
vMacdBull = macdLine > 0
vMacdBear = macdLine < 0

// Vote 4: Pressure (RSI > 50 AND MACD hist > 0) = underlying push
vPressBull = (rsi > 50) and (hist > 0)
vPressBear = (rsi < 50) and (hist < 0)

// Vote 5: Breakout / Breakdown (anti-fakeout with ATR distance)
atr = ta.atr(atrLen)
swingHigh = ta.highest(high, swingLen)
swingLow  = ta.lowest(low, swingLen)

breakUpRaw = close > swingHigh and (close - swingHigh) >= atr * atrBeyond
breakDnRaw = close < swingLow  and (swingLow - close)  >= atr * atrBeyond

vBreakBull = confirmed and breakUpRaw
vBreakBear = confirmed and breakDnRaw

// Vote 6: Volume (or HTF if enabled; HTF overrides volume vote when enabled)
volOk = volume > ta.sma(volume, volLen) * volMult
vVolBull = confirmed and (not useVolVote or volOk) and close >= open
vVolBear = confirmed and (not useVolVote or volOk) and close < open

// HTF vote (replaces volume vote if enabled)
htfClose = request.security(syminfo.tickerid, htfTf, close)
htfEma   = request.security(syminfo.tickerid, htfTf, ta.ema(close, htfEmaLen))
htfRsi   = request.security(syminfo.tickerid, htfTf, ta.rsi(close, htfRsiLen))
vHtfBull = (htfClose > htfEma) and (htfRsi > 50)
vHtfBear = (htfClose < htfEma) and (htfRsi < 50)

// Choose vote 6 source
v6Bull = useHTFVote ? vHtfBull : vVolBull
v6Bear = useHTFVote ? vHtfBear : vVolBear

//======================
// Count Fish/Sharks
//======================
fishCount =
     (vTrendBull ? 1 : 0) +
     (vRsiBull   ? 1 : 0) +
     (vMacdBull  ? 1 : 0) +
     (vPressBull ? 1 : 0) +
     (vBreakBull ? 1 : 0) +
     (v6Bull     ? 1 : 0)

sharkCount =
     (vTrendBear ? 1 : 0) +
     (vRsiBear   ? 1 : 0) +
     (vMacdBear  ? 1 : 0) +
     (vPressBear ? 1 : 0) +
     (vBreakBear ? 1 : 0) +
     (v6Bear     ? 1 : 0)

// Any “neutral” votes (when neither bull nor bear) are simply not counted.
// Total bias is still readable from fish vs sharks.
totalVotesCounted = fishCount + sharkCount

//======================
// Market “Vote” State
//======================
allFish  = fishCount == 6
allShark = sharkCount == 6

bullPressure = fishCount >= 5 and sharkCount <= 1
bearControl  = sharkCount >= 4 and fishCount <= 2
chop         = (fishCount == 3 and sharkCount == 3) or (totalVotesCounted <= 3)

state =
     allFish  ? "ALL FISH → Continuation Trend" :
     allShark ? "ALL SHARKS → Breakdown Momentum" :
     bullPressure ? "5/1 → Bullish Pressure" :
     bearControl  ? "4/2 → Bearish Control" :
     (fishCount == 3 and sharkCount == 3) ? "3 vs 3 → Indecision / Chop" :
     "Mixed → Wait for clarity"

//======================
// Dashboard Table
//======================
voteIcon(bull, bear) =>
    bull ? fish : bear ? shark : "·"

var table t = table.new(position.top_right, 2, 9, border_width=1)

if barstate.islast
    table.cell(t, 0, 0, "Market Vote", text_color=color.white, bgcolor=color.new(color.black, 0))
    table.cell(t, 1, 0, state, text_color=color.white, bgcolor=allFish ? color.new(color.green, 0) : allShark ? color.new(color.red, 0) : chop ? color.new(color.gray, 10) : bullPressure ? color.new(color.green, 35) : bearControl ? color.new(color.red, 35) : color.new(color.blue, 60))

    table.cell(t, 0, 1, "Fish / Sharks", text_color=color.white, bgcolor=color.new(color.black, 0))
    table.cell(t, 1, 1, fish + " " + str.tostring(fishCount) + "  |  " + shark + " " + str.tostring(sharkCount), text_color=color.white, bgcolor=color.new(color.black, 0))

    table.cell(t, 0, 2, "1) EMA Trend", text_color=color.white, bgcolor=color.new(color.black, 0))
    table.cell(t, 1, 2, voteIcon(vTrendBull, vTrendBear), text_color=color.white, bgcolor=color.new(color.black, 0))

    table.cell(t, 0, 3, "2) RSI MA vs 50", text_color=color.white, bgcolor=color.new(color.black, 0))
    table.cell(t, 1, 3, voteIcon(vRsiBull, vRsiBear), text_color=color.white, bgcolor=color.new(color.black, 0))

    table.cell(t, 0, 4, "3) MACD vs 0", text_color=color.white, bgcolor=color.new(color.black, 0))
    table.cell(t, 1, 4, voteIcon(vMacdBull, vMacdBear), text_color=color.white, bgcolor=color.new(color.black, 0))

    table.cell(t, 0, 5, "4) Pressure (RSI+Hist)", text_color=color.white, bgcolor=color.new(color.black, 0))
    table.cell(t, 1, 5, voteIcon(vPressBull, vPressBear), text_color=color.white, bgcolor=color.new(color.black, 0))

    table.cell(t, 0, 6, "5) Breakout/Down", text_color=color.white, bgcolor=color.new(color.black, 0))
    table.cell(t, 1, 6, voteIcon(vBreakBull, vBreakBear), text_color=color.white, bgcolor=color.new(color.black, 0))

    table.cell(t, 0, 7, useHTFVote ? "6) HTF Alignment" : "6) Volume Candle", text_color=color.white, bgcolor=color.new(color.black, 0))
    table.cell(t, 1, 7, voteIcon(v6Bull, v6Bear), text_color=color.white, bgcolor=color.new(color.black, 0))

    table.cell(t, 0, 8, "Rule", text_color=color.white, bgcolor=color.new(color.black, 0))
    table.cell(t, 1, 8, "Trade w/ majority. Avoid 3v3.", text_color=color.white, bgcolor=color.new(color.black, 0))

//======================
// Background + Labels
//======================
bgcolor(showBg and allFish ? color.new(color.green, 88) : na)
bgcolor(showBg and allShark ? color.new(color.red, 88) : na)
bgcolor(showBg and bullPressure and not allFish ? color.new(color.green, 92) : na)
bgcolor(showBg and bearControl and not allShark ? color.new(color.red, 92) : na)
bgcolor(showBg and (fishCount == 3 and sharkCount == 3) ? color.new(color.gray, 92) : na)

var string prevState = ""
if showLabels and barstate.isconfirmed
    curr = state
    if curr != prevState
        label.new(bar_index, high, curr + " (" + str.tostring(fishCount) + "F/" + str.tostring(sharkCount) + "S)", style=label.style_label_down, textcolor=color.white, color=color.new(color.black, 0))
        prevState := curr

//======================
// Alerts
//======================
alertcondition(allFish, "ALL FISH (Continuation)", "All Fish: continuation trend conditions.")
alertcondition(allShark, "ALL SHARKS (Breakdown)", "All Sharks: breakdown momentum conditions.")
alertcondition(bullPressure, "Bullish Pressure (5/1)", "5 Fish / 1 Shark: bullish pressure.")
alertcondition(bearControl, "Bearish Control (4/2)", "4 Sharks / 2 Fish: bearish control.")
alertcondition(fishCount == 3 and sharkCount == 3, "Chop (3 vs 3)", "3 vs 3: indecision / chop.")