本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Grace Signals", overlay=true)
// SETTINGS
emaLength = input.int(13, "EMA Length")
toleranceTicks = input.int(4, "Retest Tolerance (Ticks)", minval=1)
maxBars = input.int(8, "Maximum Bars to Retest", minval=1)
showLabels = input.bool(true, "Show BUY / SELL Labels")
showSignalLines = input.bool(true, "Show Signal Lines")
// 13 EMA
ema13 = ta.ema(close, emaLength)
tolerance = toleranceTicks * syminfo.mintick
// EMA PLOT
plot(ema13, title="13 EMA", color=color.yellow, linewidth=2)
// BREAKOUT CONFIRMATION
bullBreakout = ta.crossover(close, ema13)
bearBreakout = ta.crossunder(close, ema13)
// SETUP TRACKING
var int buySetupBar = na
var int sellSetupBar = na
if barstate.isconfirmed
if bullBreakout
buySetupBar := bar_index
sellSetupBar := na
if bearBreakout
sellSetupBar := bar_index
buySetupBar := na
// RETEST WINDOWS
buyWindow = not na(buySetupBar) and bar_index > buySetupBar and bar_index - buySetupBar <= maxBars
sellWindow = not na(sellSetupBar) and bar_index > sellSetupBar and bar_index - sellSetupBar <= maxBars
// PRICE MUST RETEST EMA
buyRetest = buyWindow and low <= ema13 + tolerance and close > ema13
sellRetest = sellWindow and high >= ema13 - tolerance and close < ema13
// CANDLE CONFIRMATION
bullishConfirmation = close > open
bearishConfirmation = close < open
// FINAL SIGNALS
buySignal = barstate.isconfirmed and buyRetest and bullishConfirmation
sellSignal = barstate.isconfirmed and sellRetest and bearishConfirmation
// RESET AFTER SIGNAL OR EXPIRATION
if barstate.isconfirmed
if buySignal or (buyWindow and bar_index - buySetupBar >= maxBars)
buySetupBar := na
if sellSignal or (sellWindow and bar_index - sellSetupBar >= maxBars)
sellSetupBar := na
// BUY / SELL LABELS
plotshape(showLabels and buySignal,
title="BUY Signal",
text="BUY",
style=shape.labelup,
location=location.belowbar,
color=color.lime,
textcolor=color.black,
size=size.small)
plotshape(showLabels and sellSignal,
title="SELL Signal",
text="SELL",
style=shape.labeldown,
location=location.abovebar,
color=color.red,
textcolor=color.white,
size=size.small)
// SIGNAL PRICE LINES
plot(showSignalLines and buySignal ? close : na,
title="BUY Signal Line",
color=color.lime,
linewidth=3,
style=plot.style_circles)
plot(showSignalLines and sellSignal ? close : na,
title="SELL Signal Line",
color=color.red,
linewidth=3,
style=plot.style_circles)
// ALERTS
alertcondition(buySignal, title="13 EMA Retest BUY",
message="BUY: Price closed above 13 EMA, retested and confirmed bullish.")
alertcondition(sellSignal, title="13 EMA Retest SELL",
message="SELL: Price closed below 13 EMA, retested and confirmed bearish.")