Macd and ema my

Macd and ema my

//@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
= 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

Read More

Share:

Latest News