OPEN-SOURCE SCRIPT

Nitin Strategy: Patterns + EMA/RSI Retest

//version=5
strategy("Crypto Strategy: Patterns + EMA/RSI Retest", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=90)

// Inputs
emaLength = input.int(50, "EMA Length")
rsiLength = input.int(14, "RSI Length")
overbought = input(70, "RSI Overbought")
oversold = input(30, "RSI Oversold")
patternLookback = input.int(20, "Pattern Lookback")

// Indicators
ema = ta.ema(close, emaLength)
rsi = ta.rsi(close, rsiLength)

// 1. BULLISH PATTERN (Double Bottom + Retest)
// -------------------------------------------
var float necklineBullish = na
var bool breakoutBullish = false

// Detect Double Bottom (two lows with a peak in between)
swingLow = ta.lowest(low, 5)
swingLowPrev = ta.lowest(low, 5)[5]
swingHighBetween = ta.highest(high, 5)
isDoubleBottom = swingLow >= swingLowPrev * 0.99 and swingLow <= swingLowPrev * 1.01 and swingHighBetween > swingLow and swingHighBetween > swingLowPrev

// Update neckline on pattern detection
if isDoubleBottom
necklineBullish := swingHighBetween
breakoutBullish := false

// Check breakout above neckline
if not breakoutBullish and close > necklineBullish
breakoutBullish := true

// Retest after breakout (price returns to neckline and bounces)
retestBullish = breakoutBullish and low <= necklineBullish and close > necklineBullish and rsi < oversold


// 2. BEARISH PATTERN (Double Top + Retest)
// -------------------------------------------
var float necklineBearish = na
var bool breakoutBearish = false

// Detect Double Top (two highs with a trough in between)
swingHigh = ta.highest(high, 5)
swingHighPrev = ta.highest(high, 5)[5]
swingLowBetween = ta.lowest(low, 5)
isDoubleTop = swingHigh <= swingHighPrev * 1.01 and swingHigh >= swingHighPrev * 0.99 and swingLowBetween < swingHigh and swingLowBetween < swingHighPrev

// Update neckline on pattern detection
if isDoubleTop
necklineBearish := swingLowBetween
breakoutBearish := false

// Check breakout below neckline
if not breakoutBearish and close < necklineBearish
breakoutBearish := true

// Retest after breakout (price returns to neckline and rejects)
retestBearish = breakoutBearish and high >= necklineBearish and close < necklineBearish and rsi > overbought


// 3. EMA + RSI Conditions
// -----------------------
emaBuy = ta.crossover(close, ema) and rsi < oversold
emaSell = ta.crossunder(close, ema) and rsi > overbought


// Final Buy/Sell Signals (Combine Retest & EMA/RSI)
buySignal = retestBullish or emaBuy
sellSignal = retestBearish or emaSell


// Execute Orders
strategy.entry("Buy", strategy.long, when=buySignal)
strategy.close("Buy", when=sellSignal)


// Plotting
plot(ema, "EMA", color=color.blue)
plot(necklineBullish, "Bullish Neckline", color=color.green, linewidth=2)
plot(necklineBearish, "Bearish Neckline", color=color.red, linewidth=2)
plotshape(buySignal, title="Buy", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(sellSignal, title="Sell", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")

Aviso legal