MACD Strategy Alert
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Nick_M

//@version=5
indicator("MACD Strategy Alert","MACD Entry Alert",overlay =true)
//EMA indicator
len = input.int(200, minval=1, title="Length",group="EMA SETTING")
src = input(close, title="Source")
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
out = ta.ema(src, len)
EmaColor = input(#2962FF,"EMA      ",group="Color Settings",inline="Above")

plot(out, title="EMA", color=EmaColor, offset=offset)


ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MACD",tooltip = "add MACD Indicator to your chart")
smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100, group="MACD")

smoothingLine = ma(out, smoothingLength, typeMA)
//plot(smoothingLine, title="Smoothing Line", color=#f37f20, offset=offset)

xUp = ta.crossover ( open, out)
xDn = ta.crossunder(open, out)
//plotchar(xUp, "Go Long",  "▲", location.bottom, color.lime, size = size.tiny)
//plotchar(xDn, "Go Short", "▼", location.top,    color.red,  size = size.tiny)

//  MACD Indicator
// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
srcM = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])

// Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
Col_Buy_signal = input(#26A69A, "Buy signal     ", group="Color Settings", inline="Below")
Col_Sell_signal = input(#FF5252, "  Sell signal", group="Color Settings", inline="Above")

// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal
//plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))

//plot(macd, title="MACD", color=col_macd)
//plot(signal, title="Signal", color=col_signal)

MACDB = ta.crossover  (macd, signal) 
MACDS = ta.crossunder (macd, signal) 

//Open long position
Buy = if open > out  and close > out and signal < 0 and macd < 0 and MACDB
    1

plotchar(Buy , "Go Long",  "▲", location.belowbar, color= Col_Buy_signal, size = size.tiny, text = "Buy", textcolor =Col_Buy_signal)
//strategy.entry("long", strategy.long, 0.01, when = Buy == 1)
//strategy.exit("exit", "long", profit = 1500, loss = 1000)
//plotchar(x , "Go Long",  "▲", location.abovebar, color.purple, size = size.tiny)
//alertcondition(Buy or Sell, title='Alert buy', message='Potencial buy')


//Open Short position
Sell = if open < out  and close < out and signal > 0 and macd > 0 and MACDS
    1
    

plotchar(Sell , "Go Short", "▼", location.abovebar, color= Col_Sell_signal,  size = size.tiny, text = "Sell", textcolor =Col_Sell_signal)
alertcondition(Sell or Buy, title='Alert Entry', message='Potential Entry')
//strategy.entry("Short", strategy.short, 0.001, when = Sell == 1)
//strategy.exit("exit", "Short", profit = 1500, loss = 1000)

//plotchar(y , "Go Short", "▼", location.top,    color.yellow,  size = size.tiny)
//plotchar(MACDD , "Go Long",  "▲", location.bottom, color.blue, size = size.tiny)
//plotchar(MACDSD , "Go Short", "▼", location.top,    color.yellow,  size = size.tiny)



