本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Voonology
// PineScript16 — Developed by Voonology
// Version: 1.0.0
//@version=6
indicator("MACD + RSI Fusion", shorttitle="MACD+ RSI Fusion", overlay=false, precision=2)
// ------------------------------------------------------------
// MACD + RSI FUSION V1.0
// Two independent oscillator zones inside ONE TradingView pane.
// Original MACD/RSI formulas and crossover comparisons are kept.
// Only the DRAWING coordinates are rescaled.
// ------------------------------------------------------------
// === INPUTS ===================================================
string groupMACD = "01 | MACD"
sourceInput = input.source(close, "Source", group=groupMACD)
fastLength = input.int(12, "Fast EMA", minval=1, group=groupMACD)
slowLength = input.int(26, "Slow EMA", minval=2, group=groupMACD)
signalLength = input.int(9, "Signal EMA", minval=1, group=groupMACD)
string groupRSI = "02 | RSI"
rsiLength = input.int(14, "RSI Length", minval=2, group=groupRSI)
upperLimit = input.int(70, "Overbought", minval=51, maxval=99, group=groupRSI)
lowerLimit = input.int(30, "Oversold", minval=1, maxval=49, group=groupRSI)
string groupView = "03 | Visual Settings"
normLength = input.int(150, "MACD Visual Scaling Lookback", minval=20, maxval=2000, group=groupView, tooltip="Only affects how large MACD looks on screen. Raw MACD calculations and crossover signals remain unchanged.")
showHistogram = input.bool(true, "Show MACD Histogram", group=groupView)
showSignals = input.bool(true, "Show Confirmed Signals", group=groupView)
showDashboard = input.bool(true, "Show Mini Dashboard", group=groupView)
string groupSignals = "04 | Signal Confirmation"
longRsiFloor = input.int(50, "Long: RSI must be at least", minval=1, maxval=99, group=groupSignals)
shortRsiCeiling = input.int(50, "Short: RSI must be at most", minval=1, maxval=99, group=groupSignals)
requireZero = input.bool(false, "Require MACD Above/Below Zero", group=groupSignals, tooltip="On: bullish cross must occur with MACD > 0; bearish cross must occur with MACD < 0. Fewer, more trend-focused signals.")
if fastLength >= slowLength
runtime.error("MACD Fast EMA must be shorter than Slow EMA.")
// === THEME ====================================================
color greenStrong = #10B981
color greenSoft = #65D6AC
color redStrong = #F87171
color redSoft = #FCA5A5
color blueLine = #38BDF8
color amberLine = #FBBF24
color textPrimary = #F1F5F9
color textMuted = #94A3B8
color cardBase = #101827
// === UNMODIFIED INDICATOR VALUES ===============================
[macdRaw, signalRaw, histogramRaw] = ta.macd(sourceInput, fastLength, slowLength, signalLength)
rsiRaw = ta.rsi(sourceInput, rsiLength)
// === VISUAL COORDINATES (NOT MARKET VALUES) ===================
// MACD occupies y=8..44, around zero at y=26.
// RSI occupies y=56..96 (RSI 0 => 56; RSI 100 => 96).
// The middle 48..54 is intentionally empty for readability.
float macdZeroY = 26.0
float macdHalfHeight = 18.0
float rsiBottomY = 56.0
float rsiHeight = 40.0
float magnitude = math.max(math.abs(macdRaw), math.max(math.abs(signalRaw), math.abs(histogramRaw)))
float recentPeak = ta.highest(magnitude, normLength)
float divisor = math.max(nz(recentPeak, 0.0), 1e-10)
float drawFactor = macdHalfHeight / divisor
float macdY = macdZeroY + macdRaw * drawFactor
float signalY = macdZeroY + signalRaw * drawFactor
float histogramY = macdZeroY + histogramRaw * drawFactor
float rsiY = rsiBottomY + rsiHeight * rsiRaw / 100.0
float rsiUpperY = rsiBottomY + rsiHeight * upperLimit / 100.0
float rsiLowerY = rsiBottomY + rsiHeight * lowerLimit / 100.0
float rsiMiddleY = rsiBottomY + rsiHeight * 0.5
// === FRAME / ZONES ============================================
// Invisible scale anchors prevent visual zones from moving vertically.
hline(100, "Visual Top Anchor", color=color.new(color.gray, 100))
hline(0, "Visual Bottom Anchor", color=color.new(color.gray, 100))
hline(51, "Zone Separator", color=color.new(textMuted, 72), linestyle=hline.style_dotted)
hline(26, "MACD Zero (Visual)", color=color.new(textMuted, 58), linestyle=hline.style_dotted)
pRSITop = plot(96.0, "RSI Zone Ceiling (Visual)", color=color.new(redStrong, 100), display=display.pane)
pUpper = plot(rsiUpperY, "RSI Overbought Guide (Visual)", color=color.new(redStrong, 50), linewidth=1, display=display.pane)
pLower = plot(rsiLowerY, "RSI Oversold Guide (Visual)", color=color.new(greenStrong, 50), linewidth=1, display=display.pane)
pRSIBottom = plot(56.0, "RSI Zone Floor (Visual)", color=color.new(greenStrong, 100), display=display.pane)
plot(rsiMiddleY, "RSI 50 Guide (Visual)", color=color.new(textMuted, 70), linewidth=1, display=display.pane)
fill(pRSITop, pUpper, color=color.new(redStrong, 92), title="Overbought Shading")
fill(pLower, pRSIBottom, color=color.new(greenStrong, 92), title="Oversold Shading")
// === MACD DISPLAY =============================================
color histogramColor = histogramRaw >= 0 ? (histogramRaw >= nz(histogramRaw[1], histogramRaw) ? greenStrong : color.new(greenSoft, 32)) : (histogramRaw <= nz(histogramRaw[1], histogramRaw) ? redStrong : color.new(redSoft, 34))
plot(showHistogram ? histogramY : na, "MACD Histogram (Visual)", color=histogramColor, style=plot.style_histogram, histbase=26.0, linewidth=3, display=display.pane)
plot(macdY, "MACD Line (Visual)", color=blueLine, linewidth=2, display=display.pane)
plot(signalY, "MACD Signal (Visual)", color=amberLine, linewidth=2, display=display.pane)
// === RSI DISPLAY ==============================================
color rsiColor = rsiRaw >= upperLimit ? redStrong : rsiRaw <= lowerLimit ? greenStrong : rsiRaw >= 50 ? blueLine : amberLine
plot(rsiY, "RSI Line (Visual)", color=rsiColor, linewidth=3, display=display.pane)
// === CLOSED-BAR SIGNALS =======================================
// Signals use RAW oscillator values, never the rescaled drawing values.
bool macdCrossUp = ta.crossover(macdRaw, signalRaw)
bool macdCrossDown = ta.crossunder(macdRaw, signalRaw)
bool longConfirmed = barstate.isconfirmed and macdCrossUp and rsiRaw >= longRsiFloor and (not requireZero or macdRaw > 0)
bool shortConfirmed = barstate.isconfirmed and macdCrossDown and rsiRaw <= shortRsiCeiling and (not requireZero or macdRaw < 0)
plotshape(showSignals and longConfirmed ? 4.0 : na, title="Bullish Confirmed", location=location.absolute, style=shape.triangleup, color=greenStrong, size=size.tiny, text="L", textcolor=textPrimary)
plotshape(showSignals and shortConfirmed ? 48.0 : na, title="Bearish Confirmed", location=location.absolute, style=shape.triangledown, color=redStrong, size=size.tiny, text="S", textcolor=textPrimary)
// === RAW VALUES IN TRADINGVIEW DATA WINDOW ====================
// These values are not plotted on the visual scale.
plot(rsiRaw, "RSI | Actual 0-100", display=display.data_window)
plot(macdRaw, "MACD | Actual", display=display.data_window)
plot(signalRaw, "MACD Signal | Actual", display=display.data_window)
plot(histogramRaw, "MACD Histogram | Actual", display=display.data_window)
// === COMPACT DASHBOARD ========================================
bool bullishBias = rsiRaw >= 50 and macdRaw > signalRaw
bool bearishBias = rsiRaw < 50 and macdRaw < signalRaw
string biasLabel = bullishBias ? "BULLISH" : bearishBias ? "BEARISH" : "MIXED"
color biasColor = bullishBias ? greenStrong : bearishBias ? redStrong : amberLine
string rsiStatus = rsiRaw >= upperLimit ? "OVERBOUGHT" : rsiRaw <= lowerLimit ? "OVERSOLD" : rsiRaw >= 50 ? "ABOVE 50" : "BELOW 50"
string macdStatus = macdRaw > signalRaw ? "ABOVE SIGNAL" : macdRaw < signalRaw ? "BELOW SIGNAL" : "AT SIGNAL"
string histStatus = histogramRaw >= 0 ? "POSITIVE" : "NEGATIVE"
var table dashboard = table.new(position.top_right, 3, 4, bgcolor=color.new(cardBase, 4), frame_color=color.new(textMuted, 65), frame_width=1, border_width=0)
if barstate.islast and showDashboard
table.cell(dashboard, 0, 0, "RSI", text_color=textMuted, text_size=size.tiny, bgcolor=color.new(#1E293B, 0))
table.cell(dashboard, 1, 0, "MACD", text_color=textMuted, text_size=size.tiny, bgcolor=color.new(#1E293B, 0))
table.cell(dashboard, 2, 0, "FUSION", text_color=textMuted, text_size=size.tiny, bgcolor=color.new(#1E293B, 0))
table.cell(dashboard, 0, 1, str.tostring(rsiRaw, "#.0"), text_color=rsiColor, text_size=size.small)
table.cell(dashboard, 1, 1, str.tostring(macdRaw, "#.####"), text_color=blueLine, text_size=size.small)
table.cell(dashboard, 2, 1, biasLabel, text_color=biasColor, text_size=size.small)
table.cell(dashboard, 0, 2, rsiStatus, text_color=textPrimary, text_size=size.tiny)
table.cell(dashboard, 1, 2, macdStatus, text_color=textPrimary, text_size=size.tiny)
table.cell(dashboard, 2, 2, histStatus, text_color=textPrimary, text_size=size.tiny)
table.cell(dashboard, 0, 3, "RSI " + str.tostring(lowerLimit) + "/" + str.tostring(upperLimit), text_color=textMuted, text_size=size.tiny)
table.cell(dashboard, 1, 3, "SIG " + str.tostring(signalRaw, "#.####"), text_color=amberLine, text_size=size.tiny)
table.cell(dashboard, 2, 3, "H " + str.tostring(histogramRaw, "#.####"), text_color=histogramColor, text_size=size.tiny)
// === ALERTS ===================================================
alertcondition(longConfirmed, title="MACD RSI Fusion | Bullish", message="MACD bullish crossover with RSI confirmation on {{ticker}} {{interval}} (bar closed).")
alertcondition(shortConfirmed, title="MACD RSI Fusion | Bearish", message="MACD bearish crossover with RSI confirmation on {{ticker}} {{interval}} (bar closed).")
//
//