
//version=5
indicator(“EMA 200 + MACD Strategy”, overlay=true)
// EMA 200
ema200 = ta.ema(close, 200)
plot(ema200, color=color.blue, linewidth=2, title=”EMA 200″)
// MACD settings
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
macdHist = macdLine – signalLine
// Buy and Sell signals
buySignal = ta.crossover(macdLine, signalLine) and macdLine > 0
sellSignal = ta.crossunder(macdLine, signalLine) and macdLine < 0
// Plot Buy and Sell signals
plotshape(buySignal, color=color.green, style=shape.labelup, title=”Buy Signal”, location=location.belowbar, text=”BUY”)
plotshape(sellSignal, color=color.red, style=shape.labeldown, title=”Sell Signal”, location=location.abovebar, text=”SELL”)
// Plot MACD Histogram with green for positive and red for negative
barcolor(macdHist >= 0 ? color.green : color.red, offset=-1)
// Stop Loss Logic (20%)
var float entryPrice = na
var float stopLossPrice = na
// Reset stop loss when a new buy signal occurs
if buySignal
entryPrice := close
stopLossPrice := entryPrice * 0.8 // 20% stop loss
// Sell when stop loss is hit or sell signal occurs
stopLossHit = not na(stopLossPrice) and close <= stopLossPrice
exitSignal = sellSignal or stopLossHit
// Plot stop loss line
plot(stopLossPrice, color=color.red, linewidth=2, title=”Stop Loss Line”, style=plot.style_linebr)
// Exit the position on sell signal or stop loss hit
strategy.entry(“Buy”, strategy.long, when=buySignal)
strategy.close(“Buy”, when=exitSignal)

