OPEN-SOURCE SCRIPT

EMA 21/50 Buy Strategy

//version=5
strategy("EMA 21/50 Buy Strategy", overlay=true)

// Input for EMA lengths
ema21Length = input.int(21, title="EMA 21 Length")
ema50Length = input.int(50, title="EMA 50 Length")

// Stop loss and take profit in pips
stopLossPips = input.int(50, title="Stop Loss (pips)")
takeProfitPips = input.int(100, title="Take Profit (pips)")

// Convert pips to price (adjust for pip value based on market type)
pipValue = syminfo.mintick
stopLossPrice = stopLossPips * pipValue
takeProfitPrice = takeProfitPips * pipValue

// Calculate EMAs
ema21 = ta.ema(close, ema21Length)
ema50 = ta.ema(close, ema50Length)

// Bullish crossover condition
bullishCrossover = ta.crossover(ema21, ema50)

// Strategy entry logic
if (bullishCrossover)
strategy.entry("Buy", strategy.long)
strategy.exit("Take Profit", from_entry="Buy", limit=close + takeProfitPrice, stop=close - stopLossPrice)

// Plot the EMAs
plot(ema21, color=color.blue, title="EMA 21")
plot(ema50, color=color.red, title="EMA 50")

Disclaimer