The_Bitcoin_Beast

MTF Moving Average with color change and independent

Aug 2nd, 2026 (edited)
102
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | Money | 0 0
  1. //@version=6
  2. indicator("MTF Multi-MA Slope with Full HA Candle Coloring", overlay=true)
  3.  
  4. // --- CRITICAL INPUTS & TOGGLES (no group, so they sit at the very top) ---
  5. showMessage = input.bool(true, "Turn On For A Very Important Message")
  6. plotActualHA = input.bool(true, "Turn ON for True Heikin-Ashi Bars / OFF for Regular Bars")
  7.  
  8. // --- Inputs: Timeframe & Line Type ---
  9. useMtf = input.bool(false, "Use Multi-Timeframe (MTF)", group="Timeframe Settings")
  10. mtfPeriod = input.timeframe("M", "MTF Timeframe", group="Timeframe Settings")
  11. noRepaint = input.bool(true, "MTF: Use Confirmed (Non-Repainting) Values", group="Timeframe Settings",
  12. tooltip="Uses the last closed higher-timeframe bar. Turn off to see the live, repainting value.")
  13. maType = input.string("EMA", "Moving Average Type", options=["EMA", "SMA", "VWMA", "Hull MA", "WMA", "RMA", "Heikin Ashi Close"], group="Indicator Settings")
  14. len = input.int(20, "Length", minval=2, group="Indicator Settings")
  15.  
  16. // --- Inputs: Slope Thresholds ---
  17. slopeMode = input.string("Points", "Slope Units", options=["Points", "Percent", "ATR"], group="Threshold Settings",
  18. tooltip="Points = raw price change per bar. Percent = % change per bar. ATR = change divided by ATR(14). Percent/ATR make thresholds portable across symbols.")
  19. posThresh = input.float(1.0, "Positive Threshold (Rising)", step=0.1, group="Threshold Settings")
  20. negThresh = input.float(-1.0, "Negative Threshold (Falling)", step=0.1, group="Threshold Settings")
  21.  
  22. // --- Inputs: Line Colors ---
  23. colUp = input.color(color.green, "Line Rising Color", group="Color Settings")
  24. colDown = input.color(color.red, "Line Falling Color", group="Color Settings")
  25. colFlat = input.color(color.yellow, "Line Flat / Third Color", group="Color Settings")
  26.  
  27. // --- Inputs: Signal Display ---
  28. showArrows = input.bool(true, "Show Breakout Arrows", group="Signal Settings")
  29. showLabels = input.bool(true, "Show BUY/SELL Text on Arrows", group="Signal Settings")
  30.  
  31. // --- Inputs: Heikin Ashi Candle Colors ---
  32. haUpColor = input.color(color.green, "HA Candle Bullish Color", group="Heikin Ashi Visuals")
  33. haDownColor = input.color(color.red, "HA Candle Bearish Color", group="Heikin Ashi Visuals")
  34. reverseHA = input.bool(false, "DCA BeastMode: Reverse HA Colors", group="Heikin Ashi Visuals",
  35. tooltip="Swaps HA candle colors: bearish HA candles use the bullish color and vice versa. Useful for highlighting dips as accumulation zones.")
  36.  
  37. // --- Inputs: Heikin Ashi EMA Smoother ---
  38. smoothHA = input.bool(false, "Turn ON Heikin Ashi EMA Smoother", group="Heikin Ashi Smoother",
  39. tooltip="Smooths the open, high, low and close with an EMA before building the Heikin Ashi candles. Turn OFF for standard Heikin Ashi.")
  40. smoothLen = input.int(10, "Smoother EMA Length", minval=1, group="Heikin Ashi Smoother")
  41.  
  42. // --- Heikin Ashi (calculated locally, no extra request.security call) ---
  43. haClose = ohlc4
  44. float haOpen = na
  45. haOpen := na(haOpen[1]) ? (open + close) / 2 : (haOpen[1] + haClose[1]) / 2
  46. haHigh = math.max(high, haOpen, haClose)
  47. haLow = math.min(low, haOpen, haClose)
  48.  
  49. // --- Smoothed Heikin Ashi (EMA of OHLC, then Heikin Ashi math) ---
  50. emaO = ta.ema(open, smoothLen)
  51. emaH = ta.ema(high, smoothLen)
  52. emaL = ta.ema(low, smoothLen)
  53. emaC = ta.ema(close, smoothLen)
  54. smClose = (emaO + emaH + emaL + emaC) / 4
  55. float smOpen = na
  56. smOpen := na(smOpen[1]) ? (emaO + emaC) / 2 : (smOpen[1] + smClose[1]) / 2
  57. smHigh = math.max(emaH, smOpen, smClose)
  58. smLow = math.min(emaL, smOpen, smClose)
  59.  
  60. // --- Active Heikin Ashi values (standard or smoothed) ---
  61. float actO = smoothHA ? smOpen : haOpen
  62. float actH = smoothHA ? smHigh : haHigh
  63. float actL = smoothHA ? smLow : haLow
  64. float actC = smoothHA ? smClose : haClose
  65.  
  66. // --- Moving average selector ---
  67. f_ma(float src, int length, string type) =>
  68. switch type
  69. "EMA" => ta.ema(src, length)
  70. "SMA" => ta.sma(src, length)
  71. "VWMA" => ta.vwma(src, length)
  72. "Hull MA" => ta.hma(src, length)
  73. "WMA" => ta.wma(src, length)
  74. "RMA" => ta.rma(src, length)
  75. "Heikin Ashi Close" => ta.sma(haClose, length)
  76. => na
  77.  
  78. // --- Slope calculation in selected units ---
  79. f_slope(float line) =>
  80. float chg = ta.change(line)
  81. switch slopeMode
  82. "Percent" => chg / line[1] * 100
  83. "ATR" => chg / ta.atr(14)
  84. => chg
  85.  
  86. rawLine = f_ma(close, len, maType)
  87. rawSlope = f_slope(rawLine)
  88.  
  89. // --- Multi-timeframe handling ---
  90. [rtLine, rtSlope] = request.security(syminfo.tickerid, mtfPeriod, [rawLine, rawSlope], gaps=barmerge.gaps_off)
  91. [npLine, npSlope] = request.security(syminfo.tickerid, mtfPeriod, [rawLine[1], rawSlope[1]], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
  92.  
  93. myLine = not useMtf ? rawLine : (noRepaint ? npLine : rtLine)
  94. lineSlope = not useMtf ? rawSlope : (noRepaint ? npSlope : rtSlope)
  95.  
  96. // --- State ---
  97. isFlat = not na(lineSlope) and lineSlope <= posThresh and lineSlope >= negThresh
  98. isUp = not na(lineSlope) and lineSlope > posThresh
  99. lineColor = isFlat or na(lineSlope) ? colFlat : (isUp ? colUp : colDown)
  100.  
  101. plot(myLine, "Slope Line", color=lineColor, linewidth=3)
  102.  
  103. // --- Signal logic ---
  104. var int lastState = 0
  105. currentState = isFlat ? 0 : (isUp ? 1 : -1)
  106. buySignal = lastState <= 0 and currentState == 1
  107. sellSignal = lastState >= 0 and currentState == -1
  108. lastState := currentState
  109.  
  110. // --- Arrows ---
  111. plotshape(showArrows and buySignal and showLabels, "Bullish Arrow with Label", shape.triangleup, location.belowbar, colUp, size=size.small, text="BUY", textcolor=colUp)
  112. plotshape(showArrows and buySignal and not showLabels, "Bullish Arrow without Label", shape.triangleup, location.belowbar, colUp, size=size.small)
  113. plotshape(showArrows and sellSignal and showLabels, "Bearish Arrow with Label", shape.triangledown, location.abovebar, colDown, size=size.small, text="SELL", textcolor=colDown)
  114. plotshape(showArrows and sellSignal and not showLabels, "Bearish Arrow without Label", shape.triangledown, location.abovebar, colDown, size=size.small)
  115.  
  116. // --- Alerts ---
  117. alertcondition(buySignal, "Slope Turned Bullish", "MA slope turned bullish on {{ticker}} ({{interval}})")
  118. alertcondition(sellSignal, "Slope Turned Bearish", "MA slope turned bearish on {{ticker}} ({{interval}})")
  119.  
  120. // --- Candle colors (always based on the Heikin Ashi direction) ---
  121. isHaBullish = actC >= actO
  122. showBullColor = reverseHA ? not isHaBullish : isHaBullish
  123. finalBodyColor = showBullColor ? haUpColor : haDownColor
  124.  
  125. // --- Conditional switch rendering: True HA bars or Regular OHLC bars ---
  126. float plotOpen = plotActualHA ? actO : open
  127. float plotHigh = plotActualHA ? actH : high
  128. float plotLow = plotActualHA ? actL : low
  129. float plotClose = plotActualHA ? actC : close
  130.  
  131. plotcandle(plotOpen, plotHigh, plotLow, plotClose, title="Advanced Heikin-Ashi Layer", color=finalBodyColor, wickcolor=finalBodyColor, bordercolor=finalBodyColor)
  132.  
  133. // --- Instructions table when the toggle is on ---
  134. var table infoTable = table.new(position.bottom_center, 1, 1)
  135. if showMessage and barstate.islast
  136. string msg = "⚠️ CRITICAL SETUP STEP:\n" +
  137. "To allow this script full graphical control to switch styles cleanly:\n" +
  138. "1. Right-click anywhere on your blank chart background.\n" +
  139. "2. Click on 'Settings' (or the gear icon in the top right).\n" +
  140. "3. Select the 'Symbol' tab on the left menu.\n" +
  141. "4. UNCHECK the boxes for: 'Bodies', 'Borders', and 'Wicks'.\n" +
  142. "This turns off the platform's default bars so the toggle works flawlessly!"
  143. 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)
  144.  
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment