//@version=5
// ─────────────────────────────────────────────────────────────────────────────
//  ORIGIN — Three Indicators Have To Agree
//  Free, from Origin Explains Charts · https://t.me/originexplainscharts
//
//  This is the SAME algorithm as the MT5 robot in this zip
//  (OriginThreeAgree.mq5) and as the chart in the video. One set of rules:
//
//    TOOL 1   QQE Mod. The histogram bar is BLUE, i.e. above its own zero
//             line. Red bars do not count.
//    TOOL 2   Trend Indicator A-V2. The flat step line is stepping UP, and
//             that same line is where the get-out goes.
//    TOOL 3   SuperTrend. The line FLIPPED under the candles on this bar —
//             not "it is green", the flip itself. The video's own frame
//             names this candle TRIGGER.
//
//    All three land on the SAME finished candle. Buy at that candle's close,
//    get out under the A-V2 line, take the money at two times the risk.
//    Sells are the mirror: red bar, line stepping down, SuperTrend flipping
//    over the candles, stop above the line.
//
//  HOW TO ADD IT: put this on the ONE-HOUR chart, open the Pine Editor at the
//  bottom of TradingView, paste this in and press "Add to chart".
//
//  It DRAWS what the rules see. It places no orders and predicts nothing.
//
//  ONE NOTE ON THE RUN-IN. The MT5 robot plays the exact arithmetic the
//  video's chart uses, seeded the way that file seeds it. This version uses
//  TradingView's own built-ins (ta.rsi, ta.rma, ta.supertrend), which are the
//  same indicators seeded on their own first bars. On the first few dozen
//  candles of a chart the two can disagree by a hair, because a Wilder
//  average has not converged yet; past that they are the same line.
// ─────────────────────────────────────────────────────────────────────────────
indicator("Origin — Three Indicators Have To Agree", overlay = true, max_lines_count = 500, max_labels_count = 500)

// ─── the three tools, at the video's frame values ────────────────────────────
grp1 = "1 · QQE Mod"
rsiLen     = input.int(6,     "RSI Length",            minval = 2, group = grp1)
rsiSmooth  = input.int(5,     "RSI Smoothing",         minval = 1, group = grp1)
qqeFactor  = input.float(1.61,"Fast QQE Factor",       minval = 0.1, step = 0.01, group = grp1)
qqeThresh  = input.float(3.0, "Threshold (panel style only)", minval = 0.0, step = 0.5, group = grp1)

grp2 = "2 · Trend Indicator A-V2"
emaSlow    = input.int(52, "EMA Length",             minval = 2, group = grp2)
emaFast    = input.int(10, "EMA Length (Smoothing)", minval = 2, group = grp2)

grp3 = "3 · SuperTrend"
atrPeriod  = input.int(9,    "ATR Period",   minval = 1, group = grp3)
multiplier = input.float(3.9,"Multiplier",   minval = 0.1, step = 0.1, group = grp3)

grp4 = "The trade"
reward     = input.float(2.0, "Target, x the risk", minval = 0.1, step = 0.1, group = grp4)
wantLong   = input.bool(true,  "Buys",  group = grp4)
wantShort  = input.bool(true,  "Sells", group = grp4)

grp5 = "Drawing"
fwd        = input.int(14, "Draw the plan this many candles forward", minval = 4, group = grp5)
upCol      = input.color(#089981, "Buy",  group = grp5)
dnCol      = input.color(#F23645, "Sell", group = grp5)

// ─── 1 · QQE Mod ─────────────────────────────────────────────────────────────
//  histo > 0 is the blue bar and histo < 0 is the red one. The QQE line is a
//  TRAILING band on the smoothed RSI: while the trend is up the band sits a
//  fixed distance under it and ratchets upward, never coming back down; when
//  the smoothed RSI closes under the band it jumps above and starts ratcheting
//  down. So the histogram reads +distance on a climb and -distance on a fall,
//  which is the alternating run of blue and red blocks the video shows.
wl     = math.max(1, math.round(2.0 * qqeFactor - 1.0))
rsiRaw = ta.rsi(close, rsiLen)
rsiMa  = ta.rma(rsiRaw, rsiSmooth)
darRaw = ta.rma(math.abs(rsiMa - nz(rsiMa[1], rsiMa)), wl)
dar    = ta.rma(darRaw, wl) * qqeFactor

var float band = na
if not na(rsiMa) and not na(dar)
    if na(band)
        band := rsiMa - dar                                    // first band, trend starts up
    if rsiMa < band
        band := rsiMa + dar                                    // closed under it — flip down
    else if band < rsiMa - dar
        band := rsiMa - dar                                    // ratchet up, never back down

histo  = na(rsiMa) or na(band) ? na : rsiMa - band
qqeUp  = histo > 0
qqeDn  = histo < 0

// ─── 2 · Trend Indicator A-V2 ────────────────────────────────────────────────
//  Two EMAs and a FLAT step line that holds its level until price makes a new
//  extreme, then steps. It ratchets TOWARDS the trend: on an up-run it is the
//  highest low since the ribbon turned up. Written the other way round the line
//  would never step up at all. This changes no signal — only the get-out.
avFastLine = ta.ema(close, emaFast)
avSlowLine = ta.ema(close, emaSlow)

var int   avDir   = 0
var float avLevel = na
if bar_index > emaSlow
    d = avFastLine > avSlowLine ? 1 : -1
    if d != avDir
        avDir   := d                                           // flip: start a new step
        avLevel := d > 0 ? low : high
    else
        avLevel := d > 0 ? math.max(avLevel, low) : math.min(avLevel, high)

avUp = bar_index > emaSlow and avDir > 0 and not na(avLevel)

// ─── 3 · SuperTrend ──────────────────────────────────────────────────────────
//  TradingView's own SuperTrend: stDir is -1 while the line sits UNDER price
//  and +1 while it sits over it. The FLIP is what the third card names, so
//  these are true only on the bar the line turned over — not on every bar after.
[stLine, stDir] = ta.supertrend(multiplier, atrPeriod)
stUp = stDir < 0
stDn = stDir > 0
flipUp = stUp and stUp[1] == false
flipDn = stDn and stDn[1] == false

// ─── the three cards, on one finished candle ─────────────────────────────────
longOk  = qqeUp and avUp and flipUp  and wantLong
shortOk = qqeDn and not avUp and flipDn and wantShort

// ─── the plan, drawn when they agree ─────────────────────────────────────────
if longOk or shortOk
    d     = longOk ? 1 : -1
    entry = close
    stop  = avLevel
    risk  = math.abs(entry - stop)
    tgt   = entry + d * risk * reward
    col   = d > 0 ? upCol : dnCol

    line.new(bar_index, entry, bar_index + fwd, entry, color = col, width = 2)
    label.new(bar_index + fwd, entry, "BUY", style = label.style_label_left,
              color = col, textcolor = color.white, size = size.small)

    line.new(bar_index, stop, bar_index + fwd, stop, color = color.new(color.orange, 0),
             style = line.style_dashed, width = 2)
    label.new(bar_index + fwd, stop, "GET OUT", style = label.style_label_left,
              color = color.new(color.orange, 0), textcolor = color.white, size = size.small)

    line.new(bar_index, tgt, bar_index + fwd, tgt, color = color.new(col, 30),
             style = line.style_dotted, width = 1)
    label.new(bar_index + fwd, tgt, str.tostring(reward, "#.#") + "x", style = label.style_label_left,
              color = color.new(col, 30), textcolor = color.white, size = size.small)

// ─── what each tool is showing, so all three are visible at once ─────────────
plot(stLine, "SuperTrend", color = stUp ? color.new(upCol, 0) : color.new(dnCol, 0), linewidth = 2)
plot(avLevel, "Trend A-V2 step line", color = color.new(color.blue, 10), style = plot.style_stepline,
     linewidth = 2, offset = 0)
plotchar(histo, "QQE Mod — blue is a buy bar", "", location.bottom,
         color = qqeUp ? color.new(#2962FF, 0) : qqeDn ? color.new(dnCol, 0) : na, size = size.tiny)

plotshape(longOk,  "All three agree — buy",  shape.triangleup,   location.belowbar, upCol, size = size.small)
plotshape(shortOk, "All three agree — sell", shape.triangledown, location.abovebar, dnCol, size = size.small)

// ─── alerts ──────────────────────────────────────────────────────────────────
alertcondition(longOk,  "Origin: all three agree — BUY",  "Origin — QQE blue, A-V2 up and SuperTrend flipped on the one-hour")
alertcondition(shortOk, "Origin: all three agree — SELL", "Origin — QQE red, A-V2 down and SuperTrend flipped on the one-hour")
