本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// This source code is subject to the terms of the Mozilla Public License 2.0 https://mozilla.org/MPL/2.0/
// © Algonney
indicator("Direction Compass: RSI Trend Regime Filter", shorttitle = "DirCompass", overlay = false, precision = 2)
// ───────── Inputs ─────────
grpCore = "Core"
src = input.source(ohlc4, "Source", group = grpCore)
smoothLen = input.int(3, "Smoothing (EMA)", minval = 1, group = grpCore)
rsiLen = input.int(89, "Momentum length (RSI)", minval = 2, group = grpCore)
band = input.float(5.0, "Neutral band (±)", minval = 0, step = 0.5, group = grpCore)
grpVis = "Visuals"
bullCol = input.color(#00E5A0, "Bull", group = grpVis, inline = "c")
bearCol = input.color(#FF4D6D, "Bear", group = grpVis, inline = "c")
neuCol = input.color(#8C94A6, "Neutral", group = grpVis, inline = "c")
showBg = input.bool(true, "Background on price chart", group = grpVis)
showBars = input.bool(true, "Color price candles", group = grpVis)
showLabels = input.bool(true, "BUY / EXIT labels on price chart", group = grpVis)
showTable = input.bool(true, "Info panel", group = grpVis)
// ───────── Core ─────────
smooth = ta.ema(src, smoothLen)
score = ta.rsi(smooth, rsiLen) - 50
isBull = score > band
isBear = score < -band
state = isBull ? 1 : isBear ? -1 : 0
stateCol = state == 1 ? bullCol : state == -1 ? bearCol : neuCol
// Signals only on closed candles (no repaint)
buySig = barstate.isconfirmed and score > band and nz(score[1]) <= band
exitSig = barstate.isconfirmed and score <= band and nz(score[1]) > band
var int barsIn = 0
barsIn := state != nz(state[1]) ? 1 : barsIn + 1
// ───────── Pane ─────────
lineCol = color.from_gradient(score, -20, 20, bearCol, bullCol)
plot(score, "Histogram", color = color.new(stateCol, 75), style = plot.style_columns)
plot(score, "Glow", color = color.new(lineCol, 80), linewidth = 6)
plot(score, "Score", color = lineCol, linewidth = 2)
hUp = hline(band, "Bull line", color = color.new(bullCol, 50), linestyle = hline.style_dashed)
hDn = hline(-band, "Bear line", color = color.new(bearCol, 50), linestyle = hline.style_dashed)
hline(0, "Zero", color = color.new(neuCol, 60), linestyle = hline.style_dotted)
fill(hUp, hDn, color = color.new(neuCol, 90), title = "Neutral zone")
plotshape(buySig ? score : na, "Buy dot", shape.circle, location.absolute, bullCol, size = size.tiny)
plotshape(exitSig ? score : na, "Exit dot", shape.circle, location.absolute, bearCol, size = size.tiny)
// ───────── Price chart ─────────
bgcolor(showBg and state != 0 ? color.new(stateCol, 88) : na, title = "Price background", force_overlay = true)
barcolor(showBars ? stateCol : na, title = "Candle color")
plotshape(showLabels and buySig, "BUY label", shape.labelup, location.belowbar, bullCol, text = "BUY", textcolor = color.black, size = size.small, force_overlay = true)
plotshape(showLabels and exitSig, "EXIT label", shape.labeldown, location.abovebar, bearCol, text = "EXIT", textcolor = color.white, size = size.small, force_overlay = true)
// ───────── Info panel ─────────
var table panel = table.new(position.top_right, 2, 5, bgcolor = color.new(#0B0F19, 10), border_color = color.new(neuCol, 75), border_width = 1, frame_color = color.new(neuCol, 50), frame_width = 1)
if showTable and barstate.islast
stateTxt = state == 1 ? "BULL ▲" : state == -1 ? "BEAR ▼" : "NEUTRAL ●"
actTxt = state == 1 ? "Buy / Hold" : "Stay out"
strength = math.min(math.abs(score) / 20 * 100, 100)
table.cell(panel, 0, 0, "Direction", text_color = color.silver, text_size = size.small, text_halign = text.align_left)
table.cell(panel, 1, 0, stateTxt, text_color = stateCol, text_size = size.small)
table.cell(panel, 0, 1, "Action", text_color = color.silver, text_size = size.small, text_halign = text.align_left)
table.cell(panel, 1, 1, actTxt, text_color = stateCol, text_size = size.small)
table.cell(panel, 0, 2, "Score", text_color = color.silver, text_size = size.small, text_halign = text.align_left)
table.cell(panel, 1, 2, str.tostring(score, "#.##"), text_color = color.white, text_size = size.small)
table.cell(panel, 0, 3, "Strength", text_color = color.silver, text_size = size.small, text_halign = text.align_left)
table.cell(panel, 1, 3, str.tostring(strength, "#") + "%", text_color = color.white, text_size = size.small)
table.cell(panel, 0, 4, "Candles in state", text_color = color.silver, text_size = size.small, text_halign = text.align_left)
table.cell(panel, 1, 4, str.tostring(barsIn), text_color = color.white, text_size = size.small)
// ───────── Alerts ─────────
alertcondition(buySig, "BUY", "Direction Compass: BULL started on {{ticker}} ({{interval}}), price {{close}}")
alertcondition(exitSig, "EXIT", "Direction Compass: BULL ended on {{ticker}} ({{interval}}), price {{close}}")
alertcondition(state == -1 and nz(state[1]) != -1, "BEAR", "Direction Compass: BEAR zone on {{ticker}} ({{interval}})")