Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=6
- indicator("MTF Multi-MA Slope with Full HA Candle Coloring", overlay=true)
- // --- CRITICAL INPUTS & TOGGLES (no group, so they sit at the very top) ---
- showMessage = input.bool(true, "Turn On For A Very Important Message")
- plotActualHA = input.bool(true, "Turn ON for True Heikin-Ashi Bars / OFF for Regular Bars")
- // --- Inputs: Timeframe & Line Type ---
- useMtf = input.bool(false, "Use Multi-Timeframe (MTF)", group="Timeframe Settings")
- mtfPeriod = input.timeframe("M", "MTF Timeframe", group="Timeframe Settings")
- noRepaint = input.bool(true, "MTF: Use Confirmed (Non-Repainting) Values", group="Timeframe Settings",
- tooltip="Uses the last closed higher-timeframe bar. Turn off to see the live, repainting value.")
- maType = input.string("EMA", "Moving Average Type", options=["EMA", "SMA", "VWMA", "Hull MA", "WMA", "RMA", "Heikin Ashi Close"], group="Indicator Settings")
- len = input.int(20, "Length", minval=2, group="Indicator Settings")
- // --- Inputs: Slope Thresholds ---
- slopeMode = input.string("Points", "Slope Units", options=["Points", "Percent", "ATR"], group="Threshold Settings",
- tooltip="Points = raw price change per bar. Percent = % change per bar. ATR = change divided by ATR(14). Percent/ATR make thresholds portable across symbols.")
- posThresh = input.float(1.0, "Positive Threshold (Rising)", step=0.1, group="Threshold Settings")
- negThresh = input.float(-1.0, "Negative Threshold (Falling)", step=0.1, group="Threshold Settings")
- // --- Inputs: Line Colors ---
- colUp = input.color(color.green, "Line Rising Color", group="Color Settings")
- colDown = input.color(color.red, "Line Falling Color", group="Color Settings")
- colFlat = input.color(color.yellow, "Line Flat / Third Color", group="Color Settings")
- // --- Inputs: Signal Display ---
- showArrows = input.bool(true, "Show Breakout Arrows", group="Signal Settings")
- showLabels = input.bool(true, "Show BUY/SELL Text on Arrows", group="Signal Settings")
- // --- Inputs: Heikin Ashi Candle Colors ---
- haUpColor = input.color(color.green, "HA Candle Bullish Color", group="Heikin Ashi Visuals")
- haDownColor = input.color(color.red, "HA Candle Bearish Color", group="Heikin Ashi Visuals")
- reverseHA = input.bool(false, "DCA BeastMode: Reverse HA Colors", group="Heikin Ashi Visuals",
- tooltip="Swaps HA candle colors: bearish HA candles use the bullish color and vice versa. Useful for highlighting dips as accumulation zones.")
- // --- Inputs: Heikin Ashi EMA Smoother ---
- smoothHA = input.bool(false, "Turn ON Heikin Ashi EMA Smoother", group="Heikin Ashi Smoother",
- tooltip="Smooths the open, high, low and close with an EMA before building the Heikin Ashi candles. Turn OFF for standard Heikin Ashi.")
- smoothLen = input.int(10, "Smoother EMA Length", minval=1, group="Heikin Ashi Smoother")
- // --- Heikin Ashi (calculated locally, no extra request.security call) ---
- haClose = ohlc4
- float haOpen = na
- haOpen := na(haOpen[1]) ? (open + close) / 2 : (haOpen[1] + haClose[1]) / 2
- haHigh = math.max(high, haOpen, haClose)
- haLow = math.min(low, haOpen, haClose)
- // --- Smoothed Heikin Ashi (EMA of OHLC, then Heikin Ashi math) ---
- emaO = ta.ema(open, smoothLen)
- emaH = ta.ema(high, smoothLen)
- emaL = ta.ema(low, smoothLen)
- emaC = ta.ema(close, smoothLen)
- smClose = (emaO + emaH + emaL + emaC) / 4
- float smOpen = na
- smOpen := na(smOpen[1]) ? (emaO + emaC) / 2 : (smOpen[1] + smClose[1]) / 2
- smHigh = math.max(emaH, smOpen, smClose)
- smLow = math.min(emaL, smOpen, smClose)
- // --- Active Heikin Ashi values (standard or smoothed) ---
- float actO = smoothHA ? smOpen : haOpen
- float actH = smoothHA ? smHigh : haHigh
- float actL = smoothHA ? smLow : haLow
- float actC = smoothHA ? smClose : haClose
- // --- Moving average selector ---
- f_ma(float src, int length, string type) =>
- switch type
- "EMA" => ta.ema(src, length)
- "SMA" => ta.sma(src, length)
- "VWMA" => ta.vwma(src, length)
- "Hull MA" => ta.hma(src, length)
- "WMA" => ta.wma(src, length)
- "RMA" => ta.rma(src, length)
- "Heikin Ashi Close" => ta.sma(haClose, length)
- => na
- // --- Slope calculation in selected units ---
- f_slope(float line) =>
- float chg = ta.change(line)
- switch slopeMode
- "Percent" => chg / line[1] * 100
- "ATR" => chg / ta.atr(14)
- => chg
- rawLine = f_ma(close, len, maType)
- rawSlope = f_slope(rawLine)
- // --- Multi-timeframe handling ---
- [rtLine, rtSlope] = request.security(syminfo.tickerid, mtfPeriod, [rawLine, rawSlope], gaps=barmerge.gaps_off)
- [npLine, npSlope] = request.security(syminfo.tickerid, mtfPeriod, [rawLine[1], rawSlope[1]], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
- myLine = not useMtf ? rawLine : (noRepaint ? npLine : rtLine)
- lineSlope = not useMtf ? rawSlope : (noRepaint ? npSlope : rtSlope)
- // --- State ---
- isFlat = not na(lineSlope) and lineSlope <= posThresh and lineSlope >= negThresh
- isUp = not na(lineSlope) and lineSlope > posThresh
- lineColor = isFlat or na(lineSlope) ? colFlat : (isUp ? colUp : colDown)
- plot(myLine, "Slope Line", color=lineColor, linewidth=3)
- // --- Signal logic ---
- var int lastState = 0
- currentState = isFlat ? 0 : (isUp ? 1 : -1)
- buySignal = lastState <= 0 and currentState == 1
- sellSignal = lastState >= 0 and currentState == -1
- lastState := currentState
- // --- Arrows ---
- plotshape(showArrows and buySignal and showLabels, "Bullish Arrow with Label", shape.triangleup, location.belowbar, colUp, size=size.small, text="BUY", textcolor=colUp)
- plotshape(showArrows and buySignal and not showLabels, "Bullish Arrow without Label", shape.triangleup, location.belowbar, colUp, size=size.small)
- plotshape(showArrows and sellSignal and showLabels, "Bearish Arrow with Label", shape.triangledown, location.abovebar, colDown, size=size.small, text="SELL", textcolor=colDown)
- plotshape(showArrows and sellSignal and not showLabels, "Bearish Arrow without Label", shape.triangledown, location.abovebar, colDown, size=size.small)
- // --- Alerts ---
- alertcondition(buySignal, "Slope Turned Bullish", "MA slope turned bullish on {{ticker}} ({{interval}})")
- alertcondition(sellSignal, "Slope Turned Bearish", "MA slope turned bearish on {{ticker}} ({{interval}})")
- // --- Candle colors (always based on the Heikin Ashi direction) ---
- isHaBullish = actC >= actO
- showBullColor = reverseHA ? not isHaBullish : isHaBullish
- finalBodyColor = showBullColor ? haUpColor : haDownColor
- // --- Conditional switch rendering: True HA bars or Regular OHLC bars ---
- float plotOpen = plotActualHA ? actO : open
- float plotHigh = plotActualHA ? actH : high
- float plotLow = plotActualHA ? actL : low
- float plotClose = plotActualHA ? actC : close
- plotcandle(plotOpen, plotHigh, plotLow, plotClose, title="Advanced Heikin-Ashi Layer", color=finalBodyColor, wickcolor=finalBodyColor, bordercolor=finalBodyColor)
- // --- Instructions table when the toggle is on ---
- var table infoTable = table.new(position.bottom_center, 1, 1)
- if showMessage and barstate.islast
- string msg = "⚠️ CRITICAL SETUP STEP:\n" +
- "To allow this script full graphical control to switch styles cleanly:\n" +
- "1. Right-click anywhere on your blank chart background.\n" +
- "2. Click on 'Settings' (or the gear icon in the top right).\n" +
- "3. Select the 'Symbol' tab on the left menu.\n" +
- "4. UNCHECK the boxes for: 'Bodies', 'Borders', and 'Wicks'.\n" +
- "This turns off the platform's default bars so the toggle works flawlessly!"
- table.cell(infoTable, 0, 0, msg, bgcolor=color.new(color.black, 15), text_color=color.white, text_size=size.normal, text_halign=text.align_left)
Advertisement