Engulfing and ema
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © maulana5lik

//@version=4
study("Engulfing and ema", overlay=true)

validThreshold = input(50, title="Valid Threshold (%)", minval=1, maxval=100, step=1)
boringThreshold = input(50, title="Boring Candle Threshold (%)", minval=1, maxval=100, step=1)
engulfingThreshold = input(50, title="Engulfing Threshold (%)", minval=1, maxval=100, step=1)
stopLevel = input(20, title="Stop Level (Pips)", minval=1)
boringColor = input(color.orange, title="Boring Candle Color")
ema1_length = input(9, title="EMA 1 Length")
ema2_length = input(21, title="EMA 2 Length")

 // Calculate EMAs
ema1 = ema(close, ema1_length)
ema2 = ema(close, ema2_length)

// Boring Candle (Inside Bar)
boringCandle = abs(open - close) * 100 / abs(high - low) <= boringThreshold

// Engulfing Candlestick
bullEngulfing = close[1] < open[1] and close > open[1] and abs(close[1] - open[1]) >= (high[1] - low[1]) * (validThreshold / 100) and close - open >= engulfingThreshold / 100 and not boringCandle
bearEngulfing = close[1] > open[1] and close < open[1] and abs(close[1] - open[1]) >= (high[1] - low[1]) * (validThreshold / 100) and open - close >= engulfingThreshold / 100 and not boringCandle


bullStop = close + (stopLevel * syminfo.mintick)
bearStop = close - (stopLevel * syminfo.mintick)
bullSL = low
bearSL = high
bullTP = bullStop +  (bullStop - low)
bearTP = bearStop -  (high - bearStop)

// Plot EMAs on the chart
plot(ema1, title="EMA 1", color=color.blue)
plot(ema2, title="EMA 2", color=color.red)

// Tandai Boring color
barcolor(boringCandle ? boringColor : na)

plotshape(bullEngulfing, style=shape.arrowup, location=location.abovebar, color=color.new(color.green, 0))
plotshape(bearEngulfing, style=shape.arrowdown, location=location.abovebar, color=color.new(color.red, 0))

// menentukan OP, SL dan TP
plot(bullEngulfing ? bullStop : na, title="Bullish Engulfing stop", color=color.new(color.red, 0), linewidth=3, style=plot.style_linebr)
plot(bearEngulfing ? bearStop : na, title="Bearish Engulfing stop", color=color.new(color.red, 0), linewidth=3, style=plot.style_linebr)
plot(bullEngulfing ? bullSL : na, title="Bullish Engulfing SL", color=color.new(color.red, 0), linewidth=3, style=plot.style_linebr)
plot(bearEngulfing ? bearSL : na, title="Bearish Engulfing SL", color=color.new(color.red, 0), linewidth=3, style=plot.style_linebr)
plot(bullEngulfing ? bullTP : na, title="Bullish Engulfing TP", color=color.new(color.green, 0), linewidth=3, style=plot.style_linebr)
plot(bearEngulfing ? bearTP : na, title="Bearish Engulfing TP", color=color.new(color.green, 0), linewidth=3, style=plot.style_linebr)

// Label Stop Loss and Take Profit
label.new(bullEngulfing ? bar_index : na, bullSL, text="SL: " + tostring(bullSL), color=color.red, textcolor=color.white, style=label.style_labelup, size=size.tiny)
label.new(bearEngulfing ? bar_index : na, bearSL, text="SL: " + tostring(bearSL), color=color.red, textcolor=color.white, style=label.style_labeldown, size=size.tiny)
label.new(bullEngulfing ? bar_index : na, bullTP, text="TP: " + tostring(bullTP), color=color.green, textcolor=color.white, style=label.style_labeldown, size=size.tiny)
label.new(bearEngulfing ? bar_index : na, bearTP, text="TP: " + tostring(bearTP), color=color.green, textcolor=color.white, style=label.style_labelup, size=size.tiny)
