Ichimoku MA Up & Down
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Feel_Long_KIM
//
// Ichimoku and MA use the default.
//
// It is repainted because it uses a moving average line.
// A marker is only true if it was created after the candle closed.
// The principle is too simple.
// Please enjoy using it.
//
// [Trend]
// - Up : Conversion Line > MA #1 and Base Line > MA #2
//   It is an uptrend. The short-term moving average should be above the conversion line. And the long-term should be above the Base Line.
// - Down : Conversion Line < MA #1 and Base Line < MA #2
//   It's a downtrend. The short-term moving average should be below the conversion line. And the long-term should be below the Base Line.
//
// You can get better results if you use a momentum indicator like RSI.
// Thank you.

//@version=5
indicator("Ichimoku MA Up & Down", shorttitle="Ichimoku MA Up & Down", overlay=true, timeframe="", timeframe_gaps=true)

// Ichimoku
conversionPeriods = input.int(9, minval=1, title="Conversion Line Length", group="Ichimoku Cloud")
basePeriods = input.int(26, minval=1, title="Base Line Length", group="Ichimoku Cloud")
laggingSpan2Periods = input.int(52, minval=1, title="Leading Span B Length", group="Ichimoku Cloud")
displacement = input.int(26, minval=1, title="Lagging Span", group="Ichimoku Cloud")
donchian(len) => math.avg(ta.lowest(len), ta.highest(len))
conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)
leadLine1 = math.avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)
plot(conversionLine, color=#2962FF, title="Conversion Line")
plot(baseLine, color=#B71C1C, title="Base Line")
plot(close, offset = -displacement + 1, color=#43A047, title="Lagging Span")
p1 = plot(leadLine1, offset = displacement - 1, color=#A5D6A7, title="Leading Span A")
p2 = plot(leadLine2, offset = displacement - 1, color=#EF9A9A, title="Leading Span B")
fill(p1, p2, color = leadLine1 > leadLine2 ? color.rgb(67, 160, 71, 90) : color.rgb(244, 67, 54, 90))

// MA 1
len = input.int(10, minval=1, title="Length", group="MA #1")
src = input(close, title="Source", group="MA #1")
out = ta.sma(src, len)
plot(out, color=color.red, title="MA #1")

// MA 2
len2 = input.int(20, minval=1, title="Length", group="MA #2")
src2 = input(close, title="Source", group="MA #2")
out2 = ta.sma(src2, len2)
plot(out2, color=color.blue, title="MA #2")

// Function
conversionUpMa1(period) =>
    conversionLine[period] > out[period]
conversionUpMa2(period) =>
    baseLine[period] > out2[period]
conversionDownMa1(period) =>
    conversionLine[period] < out[period]
conversionDownMa2(period) =>
    baseLine[period] < out2[period]
conversionUpMk(period) =>
    conversionLine[period] > out[0] and baseLine[period] > out2[0]
conversionDownMk(period) =>
    conversionLine[period] < out[0] and baseLine[period] < out2[0]

// Background
conversionBgUp = false
conversionBgDn = false
if conversionUpMa1(0) and conversionUpMa2(0)
    conversionBgUp := true
if conversionDownMa1(0) and conversionDownMa2(0)
    conversionBgDn := true
bgcolor(conversionBgUp ? color.new(#33CCFF, 95) : color.new(#33CCFF, 100))
bgcolor(conversionBgDn ? color.new(#EE0D59, 95) : color.new(#EE0D59, 100))

// Marker
upMk = false
dnMk = false
if conversionDownMk(1) and conversionUpMk(0)
    upMk := true
if conversionUpMk(1) and conversionDownMk(0)
    dnMk := true
bgTrend = false
if upMk == true
    bgTrend := true
if dnMk == true
    bgTrend := false
plotshape(upMk ? upMk : na, title='Up Marker', location=location.belowbar, style=shape.square, color=color.new(#2962FF, 0), size=size.auto, text='UP', textcolor=color.new(#2962FF, 0))
plotshape(dnMk ? dnMk : na, title='Down Marker', location=location.abovebar, style=shape.square, color=color.new(#B71C1C, 0), size=size.auto, text='DOWN', textcolor=color.new(#B71C1C, 0))

// Alert
alertcondition(upMk, title="Up Alert",message="Up")
alertcondition(dnMk, title="Down Alert",message="Down")