RedK Momentum Bars (RedK Mo_Bars)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RedKTrader

//@version=5
indicator('[dev]RedK Momentum Bars', shorttitle='RedK MoBars v3.0', explicit_plot_zorder = true, timeframe='', timeframe_gaps=false)

// A trading system composed of 2 short Lazy Lines (preferably one open and one close - 2-3 bars apart) and a WMA long filter 
// loosely inspired by Edler Impulse
// v2.0 cleaned up code and added MA options to be able to mix and match, and experiment with various setups 
// default values (my personal preference) remain the same as in v1.0 
// for example, some traders will consider "bear territory" only below SMA50, others will use EMA30 .. and so on.
// ---------------------------------------------------------------------------------------------------------------
// MoBars v3.0: 
// updated defaults to match the most common 3x MA cross-over set-up of SMA (10, 20, 50)
// updated visuals to push the 0 line to the background of the plot (using the explcit_plot_zorder param)
// and added alerts for crossing up, down and swing around the 0 line (the Bullish/Bearish Filter MA)

//==============================================================================
f_LazyLine(_data, _length) =>
    w1 = 0,     w2 = 0,     w3 = 0
    L1 = 0.0,   L2 = 0.0,   L3 = 0.0
    w = _length / 3

    if _length > 2
        w2 := math.round(w)
        w1 := math.round((_length - w2) / 2)
        w3 := int((_length - w2) / 2)

        L1 := ta.wma(_data, w1)
        L2 := ta.wma(L1, w2)
        L3 := ta.wma(L2, w3)
        
    else
        L3 := _data
        
    L3
//==============================================================================

// =============================================================================    
f_getMA(source, length, type) =>
    type == "SMA" ? ta.sma(source, length) : 
      type == "EMA" ? ta.ema(source, length) :
      type == "WMA" ? ta.wma(source, length) :
      type == "HMA" ? ta.hma(source, length) :
      f_LazyLine(source, length)
// =============================================================================    

// ------------------------------------------------------------------------------------------------
// Inputs
// Note, in v3.0, changed default lengths to 10, 20 and 50  -- and all MA types to SMA. 
// ------------------------------------------------------------------------------------------------

Fast_Src    = input.source(close,   title='Fast MA Source',          inline = 'Fast')
Fast_Length = input.int(10,          title = 'Length',   minval = 1, inline = 'Fast')
Fast_Type   = input.string('SMA', title = 'Type',                    inline = 'Fast',
  options = ['RSS_WMA', 'WMA', 'EMA', 'SMA', 'HMA'])

Slow_Src    = input.source(close,    title='Slow MA Source',         inline = 'Slow')
Slow_Length = input.int(20,          title='Length',     minval = 1, inline = 'Slow')
Slow_Type   = input.string('SMA', title = 'Type',                    inline = 'Slow',
  options = ['RSS_WMA', 'WMA', 'EMA', 'SMA', 'HMA'])

Slow_Delay  = input.int(3,          title='Delay (1 = None)',       minval = 1)

Fil_Length  = input.int(50,       title='Filter MA Length', minval = 1, inline = 'Filter')
Fil_Type    = input.string('SMA', title = 'Type',                   inline = 'Filter',
  options = ['RSS_WMA', 'WMA', 'EMA', 'SMA', 'HMA'])


// ------------------------------------------------------------------------------------------------
// Calculation
// ------------------------------------------------------------------------------------------------

Fast    = f_getMA(Fast_Src, Fast_Length, Fast_Type)
Slow    = f_getMA(Slow_Src, Slow_Length, Slow_Type)

Filter  = f_getMA(close, Fil_Length, Fil_Type)

Fast_M  = Fast - Filter
Slow_M  = Slow - Filter

Rel_M   = ta.wma(Slow_M, Slow_Delay)

// prep the Momentum bars
o = Rel_M
c = Fast_M
h = math.max(o, c)
l = math.min(o, c)

rising      = ta.change(c) > 0


// ------------------------------------------------------------------------------------------------
// Colors & Plots
// ------------------------------------------------------------------------------------------------

hline(0, title = 'Zero Line', color = color.blue, linestyle = hline.style_solid)

c_barup     = #11ff20ff
c_bardn     = #ff1111ff
c_bardj     = #ffffffff

c_barupb    = #1b5e20ff
c_bardnb    = #981919ff
c_bardjb    = #9598a1ff

barcolor    = c > o and rising ? c_barup : c < o and not rising ? c_bardn : c_bardj
borcolor    = c > o and rising ? c_barupb : c < o and not rising ? c_bardnb : c_bardjb
plotcandle(o, h, l, c, 'MoBars', barcolor, barcolor, bordercolor = borcolor)


// ===========================================================================================================
//      v3.0 adding alerts 
// these alerts will trigger as soon as the Momentum Bar touches above the filter line 
// this approach can lead to "false signals" but also has an advantage (of alerting to a possible mood/mode change)
// another option - maybe in an updated version - could be to trigger alerts *only* when the full Momentum Bar completely clears the filter line (above or below)
// and it's easy to make that a user choice in the study inputs
// ===========================================================================================================

Alert_up    = ta.crossover(h,0)
Alert_dn    = ta.crossunder(l,0)
Alert_swing = Alert_up or Alert_dn

// "." in alert title for the alerts to show in the right order up/down/swing 
alertcondition(Alert_up,    ".   MoBars Crossing 0 Up",         "MoBars Up - Bullish Mode Detected!")
alertcondition(Alert_dn,    "..  MoBars Crossing 0 Down",       "MoBars Down - Bearish Mode Detected!")
alertcondition(Alert_swing, "... MoBars Crossing 0",            "Mobars Swing - Possible Reversal Detected!")

