本页源码来自 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/
// © ChartTrader-X
//@version=6
indicator("Reclaim TrendSnap Signals", overlay=true, max_labels_count=500)
// ─── Inputs ───────────────────────────────────────────────
trendSnapLength = input.int(65, "Trend Length")
minBarsBetweenSignals = input.int(20, "Min Bars Between Signals")
enableSnapBuy = input.bool(true, "Enable BUY (Long)")
enableSnapSell = input.bool(true, "Enable SELL (Short)")
// New options
strictBuyCandle = input.bool(false, "BUY: only GREEN candle legal in uptrend", group="Candle Color Filter")
strictSellCandle = input.bool(false, "SELL: only RED candle legal in downtrend", group="Candle Color Filter")
oneSnapPerCycle = input.bool(true, "Show only 1 signal per cycle", group="Signal Frequency")
// ─── EMA Plot Look Inputs (Histogram style) ────────────────
pulseSmoothLen = input.int(20, "EMA Smoothing Length (for histogram look)", group="EMA Plot Look")
// ─── Trend EMA (drives the signal logic, unchanged) ────────
trendEma = ta.ema(close, trendSnapLength)
// ---- Second-stage smoothing of the SAME ema, just for the visual ----
trendPulse = ta.ema(trendEma, pulseSmoothLen)
pulseColor = trendEma > trendPulse ? color.lime : color.red
// ---- Histogram-style body (fill between ema and its smoothed self) ----
pTrendEma = plot(trendEma, color=color.new(pulseColor, 100), title="Trend EMA")
pTrendPulse = plot(trendPulse, color=color.new(pulseColor, 100), title="Trend Pulse (Smoothed)")
fill(pTrendEma, pTrendPulse, color=pulseColor, title="TrendSnap Histogram")
isGreenCandle = close > open
isRedCandle = close < open
// ─── Trend State (Reclaim Cycle Tracking) ──────────────────
var trendState = ""
var cycleStartBar = 0
// one-signal-per-cycle guards
var bool buySnapped = false
var bool sellSnapped = false
if trendState == "" or trendState == "up"
if close < trendEma and close[1] < trendEma[1]
trendState := "down"
cycleStartBar := bar_index[1]
if trendState == "" or trendState == "down"
if close > trendEma and close[1] > trendEma[1]
trendState := "up"
cycleStartBar := bar_index[1]
// reset per-cycle flags when a new cycle starts
if trendState == "up" and trendState[1] != "up"
buySnapped := false
if trendState == "down" and trendState[1] != "down"
sellSnapped := false
// ─── BUY Signal (Reclaim Long) ──────────────────────────────
greenCandleOK = strictBuyCandle ? isGreenCandle : true
buySnapSignal = low < trendEma and close > trendEma and greenCandleOK and trendState == "up" and (bar_index - cycleStartBar > minBarsBetweenSignals) and open > trendEma and close > trendEma
buySnapAllowed = not oneSnapPerCycle or not buySnapped
if buySnapSignal and buySnapAllowed and enableSnapBuy
buySnapped := true
label.new(bar_index, low, "BUY", style=label.style_label_up, color=color.green, textcolor=color.white)
// ─── SELL Signal (Reclaim Short) ────────────────────────────
redCandleOK = strictSellCandle ? isRedCandle : true
sellSnapSignal = high > trendEma and close < trendEma and redCandleOK and trendState == "down" and (bar_index - cycleStartBar > minBarsBetweenSignals) and open < trendEma and close < trendEma
sellSnapAllowed = not oneSnapPerCycle or not sellSnapped
if sellSnapSignal and sellSnapAllowed and enableSnapSell
sellSnapped := true
label.new(bar_index, high, "SELL", style=label.style_label_down, color=color.red, textcolor=color.white)
// ─── Alerts ───────────────────────────────────────────────
alertcondition(buySnapSignal and buySnapAllowed and enableSnapBuy, title="Buy Signal", message="BUY signal")
alertcondition(sellSnapSignal and sellSnapAllowed and enableSnapSell, title="Sell Signal", message="SELL signal")