OPEN-SOURCE SCRIPT

9 EMA Strategy with Retest Logic - Buy Only

//version=5
indicator("9 EMA Strategy with Retest Logic - Buy Only", overlay=true)

// Input parameters
emaLength = input.int(9, title="EMA Length")
bodySizeMultiplier = input.float(0.5, title="Minimum Body Size as Multiplier of ATR")
atrLength = input.int(14, title="ATR Length")

// Calculations
ema = ta.ema(close, emaLength)
atr = ta.atr(atrLength)

// Candle body calculations
bodySize = math.abs(close - open)
minBodySize = bodySizeMultiplier * atr

// Variables to track retest logic
var bool tradeActive = false

// Conditions for retests
priceAboveEMA = close > ema
priceBelowEMA = close < ema

// Retest Buy Logic
bigBarCondition = bodySize >= minBodySize
buyRetestCondition = priceBelowEMA[1] and priceAboveEMA and bigBarCondition

// Buy Signal Condition
buyCondition = buyRetestCondition and not tradeActive

// Trigger Buy Signal
if (buyCondition)
tradeActive := true
label.new(bar_index, high, "BUY", style=label.style_label_up, color=color.green, textcolor=color.white)
alert("Buy Signal Triggered at " + str.tostring(close))

// Reset Trade Active Status
if (priceBelowEMA)
tradeActive := false

// Plot EMA
plot(ema, color=color.purple, linewidth=2, title="9 EMA")
Bands and ChannelsChart patternsCycles

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer