MA + MACD alert Trends
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ducanhdavinci

//@version=5
indicator(shorttitle="MA+MACD alert", title="MA + MACD alert", overlay=true)
import TradingView/Strategy/3


// Input MA
// Input MA
len1 = input.int(100, minval=1, title="Length MA1", group="MA 1")
src1 = input(close, title="Source MA1", group="MA 1")
typeMA1 = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA 1")

len2 = input.int(200, minval=1, title="Length MA2", group="MA 2")
src2 = input(close, title="Source MA2", group="MA 2")
typeMA2 = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA 2")

len3 = input.int(25, minval=1, title="Length MA3", group="MA 3")
src3 = input(close, title="Source MA3", group="MA 3")
typeMA3 = input.string(title = "Method", defval = "EMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA 3")

len4 = input.int(50, minval=1, title="Length MA4", group="MA 4")
src4 = input(close, title="Source MA4", group="MA 4")
typeMA4 = input.string(title = "Method", defval = "EMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA 4")


len5 = input.int(1, minval=1, title="Length MA5", group="MA 5")
src5 = input(close, title="Source MA5", group="MA 5")
typeMA5 = input.string(title = "Method", defval = "EMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA 5")

len6 = input.int(2, minval=1, title="Length MA6", group="MA 6")
src6 = input(close, title="Source MA6", group="MA 6")
typeMA6 = input.string(title = "Method", defval = "EMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA 6")

// input MACD
// Thông số MACD của khung thời gian lớn hơn 1 khung so với khung vào lệnh
fast_length = input(title="Fast Length", defval=12, group = "MACD")
slow_length = input(title="Slow Length", defval=46, group = "MACD")
signal_length = input.int(title="Signal Smoothing",  minval = 1, defval = 9, group = "MACD")
src_macd = input(title="Source", defval=close, group = "MACD")

// Calculating MA
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)

MA1 = ma(src1, len1, typeMA1)
MA2 = ma(src2, len2, typeMA2)
MA3 = ma(src3, len3, typeMA3)
MA4 = ma(src4, len4, typeMA4)
MA5 = ma(src5, len5, typeMA5)
MA6 = ma(src6, len6, typeMA6)

// Calculating MACD
fast_ma = ta.ema(src_macd, fast_length)
slow_ma = ta.ema(src_macd, slow_length)
macd = fast_ma - slow_ma
signal = ta.ema(macd, signal_length)
//hist = macd - signal

// CHECK CONDITION ENTRY
bool check_macd_buy = ( macd > signal and macd > 0 and ta.lowest(macd,25) < 0 )
bool check_macd_sell = ( macd < signal and macd < 0 and ta.highest(macd,25) > 0 )

bool check_ma_buy = ta.crossunder(MA5,MA6) and MA3 > MA4 and ( close > MA1 or close > MA2 ) and ( MA3>MA1 or MA3>MA2 ) and ta.lowest(MA3-MA4,20)<0
bool check_ma_sell = ta.crossover(MA5,MA6) and MA3 < MA4 and ( close < MA1 or close < MA2 ) and ( MA3<MA1 or MA3<MA2 ) and ta.highest(MA3-MA4,20)>0

bool check_ma_buy2 = ( close > MA1 or close > MA2 ) and ta.crossover(MA3,MA4) and ta.lowest(MA3-MA4,20)<0
bool check_ma_sell2 = ( close < MA1 or close < MA2 ) and ta.crossunder(MA3,MA4) and ta.highest(MA3-MA4,20)>0

// Check entry buy
bool buy= check_macd_buy and ( check_ma_buy or check_ma_buy2 )
if buy
    alert( (str.format("Folow BUY {0} {1}m \n ", syminfo.ticker, timeframe.period )), alert.freq_once_per_bar_close)
//str.format("Folow {0} Buy {1}m \n ", syminfo.ticker, timeframe.period

// Check entry sell
bool sell = check_macd_sell and ( check_ma_sell or check_ma_sell2 )
if sell
    alert( (str.format("Folow SELL {0} {1}m \n ", syminfo.ticker, timeframe.period )), alert.freq_once_per_bar_close)

///////

p_ma1=plot(MA1, title="MA1", color=color.rgb(0, 20, 204, 100))
p_ma2=plot(MA2, title="MA2", color=color.rgb(19, 108, 1, 100))
p_ma3=plot(MA3, title="MA3", color=color.rgb(243, 33, 33, 100))
p_ma4=plot(MA4, title="MA4", color=color.rgb(0, 125, 10, 100))
fill(p_ma1, p_ma2, color = MA1 > MA2 ? color.rgb(255, 233, 35, 55) : color.rgb(255, 233, 35, 55))
fill(p_ma3, p_ma4, color = MA3 > MA4 ? color.rgb(255, 255, 255, 35) : color.rgb(80, 80, 80, 35))

plotchar( buy, "Go Long",  "⚑", location.belowbar, color.lime, size = size.small)
plotchar( sell, "Go Short", "⚑", location.abovebar,    color.red,  size = size.small)

