WP_MM
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Wpkenpachi

//@version=5
indicator("WP_MM", overlay = true)

// INPUTS
src = input.source(close, title="Source")
tf = input.timeframe(title="Timeframe", defval = "2")
ema50_out = ta.ema(close, 50)
ema20_out = ta.ema(close, 20)
ema9_out = ta.ema(close, 9)
ema50 = request.security(syminfo.tickerid, tf, ema50_out, gaps=barmerge.gaps_on)
ema20 = request.security(syminfo.tickerid, tf, ema20_out, gaps=barmerge.gaps_on)
ema9 = request.security(syminfo.tickerid, tf, ema9_out, gaps=barmerge.gaps_on)
show_strengh_bar = input.bool(defval = true, title = "Show Strengh Bar")


// Verify candle type BULLISH | BEARISH
is_bulish_candle() =>
    open < close ? true : false

// Strengh Candle verify if
// A candle with body <= 400 ticks has <= 25 points in shadow size ( up shadow if bullish, down shadow if bearish ), or if > 400 if it has <= 50 points in shadow size
// Or a candle with 100% of body size <= 400 having max of 5% in shadow size (the same rule for shadows above), or if > 400 if it having max of 10% in shadow
is_strenght_candle() =>
    _body = math.abs(close - open)
    _is_bullish = is_bulish_candle()
    _up_shadow = high // just a series default value
    _down_shadow = low // just  serties default value
    if _is_bullish
        _up_shadow := high - close
        _down_shadow := open - low
    else
        _up_shadow := high - open
        _down_shadow := close - low

    _literal_min_shadow_size = (_body <= 400) ? 25 : 50
    _literal_min_shadow_percent = (_body <= 400) ? 5 : 10
    _up_shadow_percent = math.abs((_up_shadow * 100) / _body)
    _down_shadow_percent = math.abs((_down_shadow * 100) / _body)

    var is_strenght = false
    if _is_bullish
        is_strenght := _up_shadow_percent <= _literal_min_shadow_percent or _up_shadow <= _literal_min_shadow_size ? true : false
    else
        is_strenght := _down_shadow_percent <= _literal_min_shadow_percent or _down_shadow <= _literal_min_shadow_size ? true : false
    is_strenght

// Check the up micro trend 
// it means: if EMA9 is above EMA20 and the space between them is up to 30 ticks
is_up_micro_trend() =>
    (ema9 > ema20) and (ema9 - ema20) >= 30

// Check the down micro trend
// it means: if EMA9 is below EMA20 and the space between them is up to 30 ticks
is_down_micro_trend() =>
    (ema9 < ema20) and (ema20 - ema9) >= 30

// Check if the EMAS are directional it means the diffrence betwen current price ema and last price ema is up to 15 ticks (diff_to_be_directional)
ema_is_directional() =>
    lower_ema = ema9
    higher_ema = ema20
    diff_to_be_directional = 15
    if is_up_micro_trend()
        (lower_ema > lower_ema[1]) and math.abs(lower_ema - lower_ema[1]) >= diff_to_be_directional and (higher_ema > higher_ema[1]) and math.abs(higher_ema - higher_ema[1]) >= diff_to_be_directional
    else if is_down_micro_trend()
        (lower_ema[1] > lower_ema) and math.abs(lower_ema[1] - lower_ema) >= diff_to_be_directional and (higher_ema[1] > higher_ema) and math.abs(higher_ema[1] - higher_ema) >= diff_to_be_directional

// Check if it is a trigger candle
// it means:
// in a up trend the candle need to touch EMA20 with his low and close above EMA9
// in a down trend the candle need to touch EMA20 with his high and close below EMA9
// is_trigger_candle() =>
//     if is_up_micro_trend()
//         true ? (low <= ema20) and (close > ema9) : false
//     else if is_down_micro_trend()
//         true ? (high >= ema20) and (close < ema9) : false

// Color the candle if its a trigger candle in the apropriated context
barcolor(show_strengh_bar and is_strenght_candle() and is_up_micro_trend() and ema_is_directional() and is_bulish_candle() ? color.lime : na)
barcolor(show_strengh_bar and is_strenght_candle() and is_down_micro_trend() and ema_is_directional() and not is_bulish_candle() ? color.maroon : na)


// Painting the trend range
plot_50 = plot(ema50, title = "EMA50", color = color.red)
plot_20 = plot(ema20, title = "EMA20", color = color.yellow)
plot_9 = plot(ema9, title = "EMA9", color = color.green)
fill_color = is_up_micro_trend() and ema_is_directional() ? color.new(color.green, 10) : (is_down_micro_trend() ? color.new(color.maroon, 10) : na)
fill(plot_20, plot_9, color = fill_color, title = "MicroTrend", fillgaps = true)
