本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator("RSI Divergence System", overlay=true, shorttitle="RSIDiv")
// === INPUTS ===
rsiShortLen = input.int(6, "短期RSI期間")
levelLow = input.int(30, "押し目閾値")
levelHigh = input.int(70, "戻り閾値")
// 長期足設定
longTF = input.timeframe("240", "長期足")
longRSILen = input.int(14, "長期RSI期間")
pivotLeft = input.int(3, "長期スイング左幅")
pivotRight = input.int(3, "長期スイング右幅")
// ATR トレール設定
atrLen = input.int(14, "ATR期間")
atrMult = input.float(1.5, "ATR倍率")
tpMult = input.float(0.5, "半分利確倍率")
// === 短期RSI ===
rsiShort = ta.rsi(close, rsiShortLen)
bullEntry = ta.crossover(rsiShort, levelLow)
bearEntry = ta.crossunder(rsiShort, levelHigh)
// === 長期足RSIとピボット ===
longHigh = request.security(syminfo.tickerid, longTF, high)
longLow = request.security(syminfo.tickerid, longTF, low)
longRSI = request.security(syminfo.tickerid, longTF, ta.rsi(close, longRSILen))
pl = ta.pivotlow(longLow, pivotLeft, pivotRight)
ph = ta.pivothigh(longHigh, pivotLeft, pivotRight)
pl0 = pl ? longLow[pivotRight] : na
pl1 = pl[1] ? longLow[pivotRight+1] : na
ph0 = ph ? longHigh[pivotRight] : na
ph1 = ph[1] ? longHigh[pivotRight+1] : na
rsi_pl0 = not na(pl0) ? longRSI[pivotRight] : na
rsi_pl1 = not na(pl1) ? longRSI[pivotRight+1] : na
rsi_ph0 = not na(ph0) ? longRSI[pivotRight] : na
rsi_ph1 = not na(ph1) ? longRSI[pivotRight+1] : na
// === ダイバージェンス判定 ===
bullDiv = not na(rsi_pl0) and not na(rsi_pl1) and (pl0 < pl1) and (rsi_pl0 > rsi_pl1)
bearDiv = not na(rsi_ph0) and not na(rsi_ph1) and (ph0 > ph1) and (rsi_ph0 < rsi_ph1)
// === ATRトレールストップ & 半分利確 ===
atr = ta.atr(atrLen)
lowestLow = ta.lowest(low, pivotRight*2)
highestHigh = ta.highest(high, pivotRight*2)
var float longTrail = na
var float shortTrail = na
var float longTP = na
var float shortTP = na
if bullEntry
longTrail := close - atr*atrMult
longTP := close + (close - lowestLow) * tpMult
if bearEntry
shortTrail := close + atr*atrMult
shortTP := close - (highestHigh - close) * tpMult
longTrail := not na(longTrail) ? math.max(longTrail, close - atr*atrMult) : longTrail
shortTrail := not na(shortTrail) ? math.min(shortTrail, close + atr*atrMult) : shortTrail
// === RSIをチャートに表示 ===
plot(rsiShort, title="RSI", color=color.blue)
hline(70, "Overbought", color=color.red)
hline(30, "Oversold", color=color.green)
hline(50, "Middle", color=color.gray)
// === ダイバージェンスをRSI上に表示 ===
plotshape((bullEntry and bullDiv) ? rsiShort : na,
title="BullDiv", location=location.absolute,
color=color.green, style=shape.triangleup, size=size.small, text="DivUP")
plotshape((bearEntry and bearDiv) ? rsiShort : na,
title="BearDiv", location=location.absolute,
color=color.red, style=shape.triangledown, size=size.small, text="DivDN")
// === 売買ラベルを価格チャートに表示 ===
plotshape(bullEntry, title="Buy Signal", location=location.belowbar,
style=shape.labelup, color=color.green, text="BUY", textcolor=color.white, size=size.small)
plotshape(bearEntry, title="Sell Signal", location=location.abovebar,
style=shape.labeldown, color=color.red, text="SELL", textcolor=color.white, size=size.small)
// === 利確/トレールラインを価格チャートに表示 ===
plot(longTP, title="Long TP", color=color.new(color.green, 0), style=plot.style_linebr, linewidth=2)
plot(shortTP, title="Short TP", color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2)
plot(longTrail, title="Long Trail", color=color.new(color.green, 50), style=plot.style_line, linewidth=1)
plot(shortTrail, title="Short Trail", color=color.new(color.red, 50), style=plot.style_line, linewidth=1)
// === アラート ===
alertcondition(bullEntry and bullDiv, title="Bull Div", message="Bullish Divergence")
alertcondition(bearEntry and bearDiv, title="Bear Div", message="Bearish Divergence")