// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// CrocVibes by Telgos
// Danke für die Grundlagen und Basisversion: amper04, ray_duke, husky, Audidriver13│

//@version=6
indicator("Croc Vibes V3.7 + Blues by Telgos", overlay=true, max_labels_count=500, max_lines_count=500)

// === Inputs ===
// --- Gruppe: Chart-Signale ---
wolke_Line_rot      = input.source(title="Wolke Linie Pink", defval=close, group="Chart-Signale")
blauer_punkt_long   = input.source(title="Blauer Punkt Long", defval=close, group="Chart-Signale")
blauer_punkt_short  = input.source(title="Blauer Punkt Short", defval=close, group="Chart-Signale")
hellgruener_punkt_long = input.source(title="Hellgruener Punkt Long", defval=close, group="Chart-Signale")
cloud_punkt_long    = input.source(title="Cloud Punkt Long", defval=close, group="Chart-Signale")
red_devil_long      = input.source(title="Red Devil Long", defval=close, group="Chart-Signale")
bb_kombi_long       = input.source(title="BB Kombi long", defval=close, group="Chart-Signale")
lollipop_long       = input.source(title="Lollipop long", defval=close, group="Chart-Signale")
aw_rocket_long      = input.source(title="AW-Rocket long", defval=close, group="Chart-Signale")
bgrl_signal         = input.source(title="Black Green Rocket Long", defval=close, group="Chart-Signale")
rotes_plus          = input.source(title="Rotes Plus", defval=close, group="Chart-Signale")

// --- Gruppe: Lochstreifen ---
status_g = input.source(title="Status (1) Grün", defval=close, group="Lochstreifen")
status_r = input.source(title="Status (2) Rot", defval=close, group="Lochstreifen")
wolke_g  = input.source(title="Wolke (1) Grün", defval=close, group="Lochstreifen")
wolke_r  = input.source(title="Wolke (3) Rot", defval=close, group="Lochstreifen")
trend_s  = input.source(title="Trend (1) Schwarz", defval=close, group="Lochstreifen")
trend_r  = input.source(title="Trend (2) Rot", defval=close, group="Lochstreifen")
trend_g  = input.source(title="Trend (3) Grün", defval=close, group="Lochstreifen")
setter_r = input.source(title="Setter (Trend 5) Rot", defval=close, group="Lochstreifen")
setter_g = input.source(title="Setter (Trend 6) grün", defval=close, group="Lochstreifen")
welle_g  = input.source(title="Welle (1) grün", defval=close, group="Lochstreifen")
welle_gg = input.source(title="Welle (2) dunkelgrün", defval=close, group="Lochstreifen")
welle_r  = input.source(title="Welle (4) Rot", defval=close, group="Lochstreifen")
welle_rr = input.source(title="Welle (6) dunkelRot", defval=close, group="Lochstreifen")
kerze_g  = input.source(title="Kerzenfarbe (1) Gruen", defval=close, group="Lochstreifen")
kerze_s  = input.source(title="Kerzenfarbe (2) Schwarz", defval=close, group="Lochstreifen")
kerze_r  = input.source(title="Kerzenfarbe (3) Rot", defval=close, group="Lochstreifen")

// --- Gruppe: MSI ---
src1 = input.source(close, "Day OverBought 1", group="MSI")

// --- Gruppe: Darstellung ---
label_offset_mult = input.float(3.0, title="Label-Abstand (ATR x)", minval=0.1, step=0.1, group="Darstellung")

// === Helpers ===
asBool(src) => nz(src) > 0
atr = ta.atr(16)

// Bool-Kapselungen
b_long     = asBool(blauer_punkt_long)
b_short    = asBool(blauer_punkt_short)
hgr_long   = asBool(hellgruener_punkt_long)
cp_long    = asBool(cloud_punkt_long)
rd_long    = asBool(red_devil_long)
bb_long    = asBool(bb_kombi_long)
lolli_long = asBool(lollipop_long)
awr_long   = asBool(aw_rocket_long)
bgrl       = asBool(bgrl_signal)
r_plus     = asBool(rotes_plus)

st_g = asBool(status_g)
st_r = asBool(status_r)
wk_g = asBool(wolke_g)
wk_r = asBool(wolke_r)
tr_s = asBool(trend_s)
tr_r = asBool(trend_r)
tr_g = asBool(trend_g)
set_r = asBool(setter_r)
set_g = asBool(setter_g)
wl_g  = asBool(welle_g)
wl_gg = asBool(welle_gg)
wl_r  = asBool(welle_r)
wl_rr = asBool(welle_rr)
cz_g  = asBool(kerze_g)
cz_s  = asBool(kerze_s)
cz_r  = asBool(kerze_r)

// === Markt-Zustände ===
baerische_welle = wl_rr or wl_r
bullische_welle = wl_gg or wl_g

// === Pearl Diver ===
allRed       = st_r and cz_r and wk_r and tr_r and set_r and baerische_welle
kerzeSG_l    = st_r and (cz_s or cz_g) and wk_r and tr_r and set_r and baerische_welle
isPearlLong  = kerzeSG_l and allRed[1] and allRed[2]

allGreen     = st_g and cz_g and wk_g and tr_g and set_g and bullische_welle
kerzeSG_s    = st_g and (cz_s or cz_r) and wk_g and tr_g and set_g and bullische_welle
isPearlShort = kerzeSG_s and allGreen[1] and allGreen[2]

// === Stephan-Setup (V2-Logik) ===
stephan_zusatzbedingung_erfuellt = cp_long or rd_long or lolli_long or bb_long or awr_long or bgrl
stephan_setup = not na(src1) and wk_g and tr_s and set_r and stephan_zusatzbedingung_erfuellt

// === Ulti Rally (V2-kompatibel) ===
Kerze_in_wolke_gr = close - wolke_Line_rot
ulti_rally = hgr_long and (Kerze_in_wolke_gr > 0) and st_r and cz_r and set_r and not baerische_welle

// === BGRL / Rotes Plus ===
is_bgrl       = bgrl
is_rotes_plus = r_plus and wk_g

// =====================================================================
// === BLUES-ADD-ON (immer sichtbar, ersetzt alte blauen Labels) ===
// =====================================================================

// Wolken-Status
g_active = wk_g
r_active = wk_r

// steigende Flanke (Signal wird jetzt neu > 0)
f_rise(sig) =>
    cond_now  = (not na(sig)) and (sig > 0)
    cond_prev = na(sig[1]) or (sig[1] <= 0)
    cond_now and cond_prev

// --- Zonen-Tracking GRÜN (für „Ein Hoch kommt noch”) ---
var int   g_zoneStart = na
var float g_zoneHigh  = na
g_switch = barstate.isfirst ? true : (g_active != g_active[1])
if bar_index == 0 or g_switch
    g_zoneStart := bar_index
    g_zoneHigh  := na
g_inZone        = g_active and bar_index >= g_zoneStart
g_zoneHigh_prev = g_zoneHigh
if g_inZone
    if na(g_zoneHigh) or high > g_zoneHigh
        g_zoneHigh := high

// --- Zonen-Tracking ROT (für „Ein Tief kommt noch”) ---
var int   r_zoneStart = na
var float r_zoneLow   = na
r_switch = barstate.isfirst ? true : (r_active != r_active[1])
if bar_index == 0 or r_switch
    r_zoneStart := bar_index
    r_zoneLow   := na
r_inZone       = r_active and bar_index >= r_zoneStart
r_zoneLow_prev = r_zoneLow
if r_inZone
    if na(r_zoneLow) or low < r_zoneLow
        r_zoneLow := low

// --- Trigger ---
long_blue_now  = g_inZone and f_rise(blauer_punkt_short)   // „Ein Hoch kommt noch“
short_blue_now = r_inZone and f_rise(blauer_punkt_long)    // „Ein Tief kommt noch“
long_start_ok  = long_blue_now  and not na(g_zoneHigh_prev)
short_start_ok = short_blue_now and not na(r_zoneLow_prev)

// --- States/Objekte ---
var bool  long_pending_active  = false
var float long_refHigh         = na
var label startLblLong         = na
var line  longPendingLine      = na

var bool  short_pending_active = false
var float short_refLow         = na
var label startLblShort        = na
var line  shortPendingLine     = na

// --- Start (Label + Linie) ---
if long_start_ok and not long_pending_active
    long_pending_active := true
    long_refHigh        := g_zoneHigh_prev
    if high < long_refHigh
        if not na(startLblLong)
            label.delete(startLblLong)
        startLblLong := label.new(bar_index, high + atr * label_offset_mult, "Ein Hoch kommt noch",
                                  xloc=xloc.bar_index, yloc=yloc.price,
                                  style=label.style_label_down, color=color.blue, textcolor=color.white, size=size.normal)
    if not na(longPendingLine)
        line.delete(longPendingLine)
    longPendingLine := line.new(bar_index, long_refHigh, bar_index + 1, long_refHigh, xloc=xloc.bar_index, extend=extend.right, color=color.blue, width=2)

if short_start_ok and not short_pending_active
    short_pending_active := true
    short_refLow         := r_zoneLow_prev
    if low > short_refLow
        if not na(startLblShort)
            label.delete(startLblShort)
        startLblShort := label.new(bar_index, low - atr * label_offset_mult, "Ein Tief kommt noch",
                                   xloc=xloc.bar_index, yloc=yloc.price,
                                   style=label.style_label_up, color=color.blue, textcolor=color.white, size=size.normal)
    if not na(shortPendingLine)
        line.delete(shortPendingLine)
    shortPendingLine := line.new(bar_index, short_refLow, bar_index + 1, short_refLow,
                                 xloc=xloc.bar_index, extend=extend.right, color=color.blue, width=2)

// --- Erfüllung & Wolkenbruch ---
long_fulfilled_now  = long_pending_active  and high >= long_refHigh
short_fulfilled_now = short_pending_active and low  <= short_refLow
long_cloud_break    = long_pending_active  and not g_active
short_cloud_break   = short_pending_active and not r_active

// --- Ende & Aufräumen ---
end_long  = long_pending_active  and (long_fulfilled_now  or long_cloud_break)
end_short = short_pending_active and (short_fulfilled_now or short_cloud_break)

if end_long
    if not na(startLblLong)
        label.delete(startLblLong)
        startLblLong := na
    if not na(longPendingLine)
        line.delete(longPendingLine)
        longPendingLine := na
    long_pending_active := false
    long_refHigh := na

if end_short
    if not na(startLblShort)
        label.delete(startLblShort)
        startLblShort := na
    if not na(shortPendingLine)
        line.delete(shortPendingLine)
        shortPendingLine := na
    short_pending_active := false
    short_refLow := na

// =====================================================================
// === Alerts (unverändert)
// =====================================================================
alertcondition(isPearlLong,  title="Pearl Long",                   message="Pearl Diver erkannt")
alertcondition(isPearlShort, title="Pearl Short",                  message="Oyster Farmer erkannt")
alertcondition(ulti_rally,   title="Ulti Rally",                   message="Ulti Rally erkannt")
alertcondition(stephan_setup,title="Stephan Setup",                message="Stephan Setup erkannt")
alertcondition(is_bgrl,      title="BGRL",                         message="Black Green Rocket Long erkannt")
alertcondition(is_rotes_plus,title="Rotes Kreuz bei grüner Wolke", message="Rotes Kreuz erkannt")

// =====================================================================
// === Labels (ohne blaue Labels; die macht jetzt das Blues-Add-on)
// =====================================================================
if isPearlLong
    label.new(bar_index, low - atr * label_offset_mult, text="Pearl Diver", style=label.style_label_up, color=color.purple, textcolor=color.white, size=size.small)
if isPearlShort
    label.new(bar_index, high + atr * label_offset_mult, text="Oyster Farmer", style=label.style_label_down, color=color.purple, textcolor=color.white, size=size.small)
if ulti_rally
    label.new(bar_index, low - atr * label_offset_mult, text="Ulti", style=label.style_label_up, color=color.aqua, textcolor=color.white, size=size.small)
if stephan_setup
    label.new(bar_index, low - atr * label_offset_mult, text="Stephan", style=label.style_label_up, color=color.teal, textcolor=color.white, size=size.small)
if is_bgrl
    label.new(bar_index, low - atr * label_offset_mult, text="BGRL", style=label.style_label_up, color=color.gray, textcolor=color.white, size=size.small)
if is_rotes_plus
    label.new(bar_index, high + atr * label_offset_mult, text="Rotes Kreuz", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.small)

// =====================================================================
// === Shapes für Screener/Backtest (unverändert)
// =====================================================================
plotshape(isPearlLong,   title="Pearl Long",    location=location.abovebar, color=color.new(color.purple, 100), style=shape.labelup,     size=size.tiny)
plotshape(isPearlShort,  title="Pearl Short",   location=location.belowbar, color=color.new(color.purple, 100), style=shape.labeldown,   size=size.tiny)
plotshape(ulti_rally,    title="Ulti Rally",    location=location.abovebar, color=color.new(color.aqua,   100), style=shape.diamond,     size=size.tiny)
plotshape(stephan_setup, title="Stephan Setup", location=location.abovebar, color=color.new(color.teal,   100), style=shape.labelup,     size=size.tiny)
plotshape(is_bgrl,       title="BGRL",          location=location.abovebar, color=color.new(color.gray,   100), style=shape.triangleup,  size=size.tiny)
plotshape(is_rotes_plus, title="Rotes Kreuz",   location=location.belowbar, color=color.new(color.red,    100), style=shape.labeldown,   size=size.tiny)
