RSI + MACD

Glitter_ · study · 54 行 · 点赞 528 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("RSI + MACD", shorttitle="RSI+MACD", overlay=false)
 
// ---------- Inputs ----------
rsiLen  = input.int(14, "RSI Length", minval=1, group="RSI")
rsiSrc  = input.source(close, "RSI Source", group="RSI")
 
fastLen = input.int(12, "MACD Fast", minval=1, group="MACD")
slowLen = input.int(26, "MACD Slow", minval=1, group="MACD")
sigLen  = input.int(9, "MACD Signal", minval=1, group="MACD")
normLen = input.int(200, "MACD Scale Lookback", minval=10, group="MACD")
 
// ---------- Layout ----------
// RSI sits at 0-100. MACD sits in a band centered on 160.
mid   = 160.0
halfH = 45.0
 
// ---------- Calculations ----------
rsi = ta.rsi(rsiSrc, rsiLen)
[macdLine, signalLine, hist] = ta.macd(close, fastLen, slowLen, sigLen)
 
peak = ta.highest(math.max(math.abs(macdLine), math.abs(signalLine), math.abs(hist)), normLen)
k    = peak == 0 ? 0.0 : halfH / peak
 
macdP = mid + macdLine * k
sigP  = mid + signalLine * k
histP = mid + hist * k
 
// ---------- RSI (standard look) ----------
plot(rsi, "RSI", color=#7E57C2, linewidth=1, display=display.pane)
h70 = hline(70, "Upper Band", color=#787B86, linestyle=hline.style_dashed)
hline(50, "Middle Band", color=color.new(#787B86, 50), linestyle=hline.style_dashed)
h30 = hline(30, "Lower Band", color=#787B86, linestyle=hline.style_dashed)
fill(h70, h30, color=color.rgb(126, 87, 194, 90), title="RSI Background")
 
// ---------- MACD (standard look) ----------
histColor = hist >= 0 ? (hist > nz(hist[1]) ? #26A69A : #B2DFDB) : (hist < nz(hist[1]) ? #FF5252 : #FFCDD2)
 
plot(histP, "Histogram", style=plot.style_columns, histbase=mid, color=histColor, display=display.pane)
plot(macdP, "MACD", color=#2962FF, linewidth=1, display=display.pane)
plot(sigP, "Signal", color=#FF6D00, linewidth=1, display=display.pane)
hline(mid, "MACD Zero", color=#787B86, linestyle=hline.style_dashed)
 
// ---------- Real values in the status line ----------
plot(rsi, "RSI (real)", display=display.status_line)
plot(macdLine, "MACD (real)", display=display.status_line)
plot(signalLine, "Signal (real)", display=display.status_line)
plot(hist, "Hist (real)", display=display.status_line)
 
// ---------- Alerts ----------
alertcondition(ta.crossover(macdLine, signalLine), "MACD Bull Cross", "MACD crossed above signal")
alertcondition(ta.crossunder(macdLine, signalLine), "MACD Bear Cross", "MACD crossed below signal")
alertcondition(ta.crossover(rsi, 30), "RSI leaves oversold", "RSI crossed back above 30")
alertcondition(ta.crossunder(rsi, 70), "RSI leaves overbought", "RSI crossed back below 70")

← 返回列表