OPEN-SOURCE SCRIPT

Megvie Scalping C - Pullback EMA20/50 (3-5m) by Lynda

25
//version=5
indicator("Megvie Scalping C - Pullback EMA20/50 (3-5m)", overlay=true, max_labels_count=500, max_lines_count=500)

// === INPUTS ===
ema_fast_len = input.int(20, "EMA fast (pullback)")
ema_slow_len = input.int(50, "EMA slow (trend)")
rsi_len = input.int(14, "RSI length")
rsi_min = input.int(40, "RSI min for entry")
atr_len = input.int(14, "ATR length (for SL/TP)")
use_atr_for_sl = input.bool(true, "Use ATR for SL size")
atr_sl_mult = input.float(1.0, "SL = ATR * multiplier", step=0.1)
rr = input.float(1.8, "Risk:Reward (TP = SL * RR)", step=0.1)
max_signals_repeat = input.int(3, "Min bars between signals", minval=1)

// === INDICATORS ===
ema_fast = ta.ema(close, ema_fast_len)
ema_slow = ta.ema(close, ema_slow_len)
rsi = ta.rsi(close, rsi_len)
atr = ta.atr(atr_len)

plot(ema_fast, color=color.new(color.green, 0), title="EMA 20")
plot(ema_slow, color=color.new(color.red, 0), title="EMA 50")

// === TREND FILTER ===
trend_bull = ema_fast > ema_slow
trend_bear = ema_fast < ema_slow

// === PULLBACK CONDITION ===
// Consider a pullback when price traded at/under EMA20 within the last 3 bars and now shows a bullish/bearish confirmation
pullback_bull = ta.lowest(low, 3) <= ema_fast and close > ema_fast
pullback_bear = ta.highest(high, 3) >= ema_fast and close < ema_fast

// === CONFIRMATION CANDLE ===
// Bullish confirmation: current close > open AND close > high[1] (strong close)
// Bearish confirmation: current close < open AND close < low[1]
bullish_candle = close > open and close > high[1]
bearish_candle = close < open and close < low[1]

// === ENTRY SIGNALS (Version C logic) ===
buySignal = trend_bull and pullback_bull and rsi >= rsi_min and bullish_candle
sellSignal = trend_bear and pullback_bear and rsi <= (100 - rsi_min) and bearish_candle

// Prevent firing signals too often
var int lastSignalBar = na
ok_to_fire = na(lastSignalBar) ? true : (bar_index - lastSignalBar) > max_signals_repeat

buyFire = buySignal and ok_to_fire
sellFire = sellSignal and ok_to_fire

if buyFire
lastSignalBar := bar_index
if sellFire
lastSignalBar := bar_index

// === SL / TP CALCULATION ===
var float sl_price = na
var float tp_price = na
var line sl_line = na
var line tp_line = na
var label sig_label = na

if buyFire
if use_atr_for_sl
sl_price := close - atr * atr_sl_mult
else
sl_price := ta.lowest(low, 3) - syminfo.mintick * 5
tp_price := close + (close - sl_price) * rr

// draw lines and label
line.delete(sl_line[1])
line.delete(tp_line[1])
label.delete(sig_label[1])
sl_line := line.new(bar_index, sl_price, bar_index + 50, sl_price, color=color.new(color.red, 0), width=1, extend=extend.right)
tp_line := line.new(bar_index, tp_price, bar_index + 50, tp_price, color=color.new(color.green, 0), width=1, extend=extend.right)
sig_label := label.new(bar_index, low, "BUY\nSL:" + str.tostring(sl_price, format.mintick) + "\nTP:" + str.tostring(tp_price, format.mintick), style=label.style_label_up, color=color.new(color.green,0), textcolor=color.white, size=size.small)

if sellFire
if use_atr_for_sl
sl_price := close + atr * atr_sl_mult
else
sl_price := ta.highest(high, 3) + syminfo.mintick * 5
tp_price := close - (sl_price - close) * rr

// draw lines and label
line.delete(sl_line[1])
line.delete(tp_line[1])
label.delete(sig_label[1])
sl_line := line.new(bar_index, sl_price, bar_index + 50, sl_price, color=color.new(color.red, 0), width=1, extend=extend.right)
tp_line := line.new(bar_index, tp_price, bar_index + 50, tp_price, color=color.new(color.green, 0), width=1, extend=extend.right)
sig_label := label.new(bar_index, high, "SELL\nSL:" + str.tostring(sl_price, format.mintick) + "\nTP:" + str.tostring(tp_price, format.mintick), style=label.style_label_down, color=color.new(color.red,0), textcolor=color.white, size=size.small)

// === PLOT SIGNAL ARROWS ===
plotshape(buyFire, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small, text="BUY")
plotshape(sellFire, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small, text="SELL")

// === ALERTS ===
alertcondition(buyFire, title="BUY Signal", message="Megvie C: BUY signal. SL: {{plot_0}} TP: {{plot_1}}")
alertcondition(sellFire, title="SELL Signal", message="Megvie C: SELL signal. SL: {{plot_0}} TP: {{plot_1}}")
alertcondition(ta.cross(close, tp_price), title="TP Hit", message="Megvie C: TP reached")
alertcondition(ta.cross(close, sl_price), title="SL Hit", message="Megvie C: SL reached")

// === NOTES ===
// - Optimized for 3-5 minute charts.
// - Test in paper trading before using real capital.
// - Adjust ATR multiplier and RR to match your risk management.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.