Moving Averages With Cross Alerts
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bstashio

//@version=5
indicator('Moving Averages With Cross Alerts', overlay = true)



////////////////////////////////////////////////////////////////////////////////
//
// Generic Settings
//
////////////////////////////////////////////////////////////////////////////////

line_width         = 1
color_transparency = 20
color_white        = color.new(color.white , color_transparency)
color_red          = color.new(color.red   , color_transparency)
color_green        = color.new(color.green , color_transparency)
color_blue         = color.new(color.blue  , color_transparency)
color_aqua         = color.new(color.aqua  , color_transparency)
color_yellow       = color.new(color.yellow, color_transparency)

//------------------------------------------------------------------------------

ma_type = input.string(
  title   = 'Moving Averages Type',
  options = ['SMA', 'EMA', 'HMA', 'RMA', 'WMA', 'VWMA', 'VWAP', 'ALMA'],
  defval  = 'SMA')

//------------------------------------------------------------------------------
  
get_ma( source, length ) =>
    switch ma_type
        'SMA'  => ta.sma(source, length)
        'EMA'  => ta.ema(source, length)
        'HMA'  => ta.hma(source, length)
        'RMA'  => ta.rma(source, length)
        'WMA'  => ta.wma(source, length)
        'VWMA' => ta.vwma(source, length)
        'VWAP' => ta.vwma(source, length)
        'ALMA' => ta.alma(source, length, 0.85, 5)

////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////

// @Notes: 
// the followng doesn't work because "Cannot use 'plot' in local scope"
// so now I have to abandon using a loop and have each ma done indivdually with repetitive code
//
// @TradingView_Staff consider supporting this
//
//var lengths = array.from(5, 13, 50, 200, 800)
//var colors  = array.from(color_yellow, color_red, color_aqua, color_white, color_blue)
//
//for [index, value] in lengths
//    name    = 'MA ' + str.tostring(index)
//    group   = name
//    enable  = input.bool(title = name + ' Enable', defval = true)
//    source  = input.source(close, title = name + ' Source')
//    length  = input.int(title = name + ' Length', minval = 1, defval = value)
//    ma      = ma_type == 'EMA' ? ta.ema(source, length) : ta.sma(source, length)
//    plot(enable and ma ? ma : na, title = name + ' Line', linewidth = line_width, color = colors[index])

////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
//
// Moving Average 1
//
////////////////////////////////////////////////////////////////////////////////

ma1_enable  = input.bool(group = 'MA1', title = 'MA1 Enable', defval = true)
ma1_source  = input.source(group = 'MA1', title = 'MA1 Source', defval = close)
ma1_length  = input.int(group = 'MA1', title = 'MA1 Length', minval = 1, defval = 5)
ma1_color   = input.color(group = 'MA1', title = 'MA1 Color', defval = color_yellow)
ma1         = get_ma( ma1_source, ma1_length )

plot(
  series    = ma1_enable and ma1 ? ma1 : na,
  title     = 'MA1 Line',
  linewidth = line_width,
  color     = ma1_color)

////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
//
// Moving Average 2
//
////////////////////////////////////////////////////////////////////////////////

ma2_enable  = input.bool(group = 'MA2', title = 'MA2 Enable', defval = true)
ma2_source  = input.source(group = 'MA2', title = 'MA2 Source', defval = close)
ma2_length  = input.int(group = 'MA2', title = 'MA2 Length', minval = 1, defval = 13)
ma2_color   = input.color(group = 'MA2', title = 'MA2 Color', defval = color_red)
ma2         = get_ma( ma2_source, ma2_length )

plot(
  series    = ma2_enable and ma2 ? ma2 : na,
  title     = 'MA2 Line',
  linewidth = line_width,
  color     = ma2_color)

////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
//
// Moving Average 3
//
////////////////////////////////////////////////////////////////////////////////

ma3_enable  = input.bool(group = 'MA3', title = 'MA3 Enable', defval = true)
ma3_source  = input.source(group = 'MA3', title = 'MA3 Source', defval = close)
ma3_length  = input.int(group = 'MA3', title = 'MA3 Length', minval = 1, defval = 50)
ma3_color   = input.color(group = 'MA3', title = 'MA3 Color', defval = color_aqua)
ma3         = get_ma( ma3_source, ma3_length )

plot(
  series    = ma3_enable and ma3 ? ma3 : na,
  title     = 'MA3 Line',
  linewidth = line_width,
  color     = ma3_color)

////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
//
// Moving Average 4
//
////////////////////////////////////////////////////////////////////////////////

ma4_enable  = input.bool(group = 'MA4', title = 'MA4 Enable', defval = true)
ma4_source  = input.source(group = 'MA4', title = 'MA4 Source', defval = close)
ma4_length  = input.int(group = 'MA4', title = 'MA4 Length', minval = 1, defval = 200)
ma4_color   = input.color(group = 'MA4', title = 'MA4 Color', defval = color_white)
ma4         = get_ma( ma4_source, ma4_length )

plot(
  series    = ma4_enable and ma4 ? ma4 : na,
  title     = 'MA4 Line',
  linewidth = line_width,
  color     = ma4_color)

////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
//
// Moving Average 5
//
////////////////////////////////////////////////////////////////////////////////

ma5_enable  = input.bool(group = 'MA5', title = 'MA5 Enable', defval = true)
ma5_source  = input.source(group = 'MA5', title = 'MA5 Source', defval = close)
ma5_length  = input.int(group = 'MA5', title = 'MA5 Length', minval = 1, defval = 800)
ma5_color   = input.color(group = 'MA5', title = 'MA5 Color', defval = color_blue)
ma5         = get_ma( ma5_source, ma5_length )

plot(
  series    = ma5_enable and ma5 ? ma5 : na,
  title     = 'MA5 Line',
  linewidth = line_width,
  color     = ma5_color)

////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
//
// Alerts
//
////////////////////////////////////////////////////////////////////////////////

uptrend_message   = ' - Indicating Possible Uptrend'
downtrend_message = ' - Indicating Possible Downtrend'

//------------------------------------------------------------------------------

alertcondition(
  ta.crossover(ma1, ma2),
  title   = 'MA1 Cross Over MA2',
  message = 'MA1 Cross Over MA2 Alert' + uptrend_message)

alertcondition(
  ta.crossunder(ma1, ma2),
  title   = 'MA1 Cross Under MA2',
  message = 'MA1 Cross Under MA2 Alert' + downtrend_message)

alertcondition(
  ta.crossover(ma1, ma3),
  title   = 'MA1 Cross Over MA3',
  message = 'MA1 Cross Over MA3 Alert' + uptrend_message)

alertcondition(
  ta.crossunder(ma1, ma3),
  title   = 'MA1 Cross Under MA3',
  message = 'MA1 Cross Under MA3 Alert' + downtrend_message)

//------------------------------------------------------------------------------

alertcondition(
  ta.crossover(ma2, ma3),
  title   = 'MA2 Cross Over MA3',
  message = 'MA2 Cross Over MA3 Alert' + uptrend_message)

alertcondition(
  ta.crossunder(ma2, ma3),
  title   = 'MA2 Cross Under MA3',
  message = 'MA2 Cross Under MA3 Alert' + downtrend_message)

alertcondition(
  ta.crossover(ma2, ma4),
  title   = 'MA2 Cross Over MA4',
  message = 'MA2 Cross Over MA4 Alert' + uptrend_message)

alertcondition(
  ta.crossunder(ma2, ma4),
  title   = 'MA2 Cross Under MA4',
  message = 'MA2 Cross Under MA4 Alert' + downtrend_message)

//------------------------------------------------------------------------------

alertcondition(
  ta.crossover(ma3, ma4),
  title = 'MA3 Cross Over MA4',
  message='MA3 Cross Over MA4 Alert' + uptrend_message)

alertcondition(
  ta.crossunder(ma3, ma4),
  title   = 'MA3 Cross Under MA4',
  message = 'MA3 Cross Under MA4 Alert' + downtrend_message)

alertcondition(
  ta.crossover(ma3, ma5),
  title   = 'MA3 Cross Over MA5',
  message = 'MA3 Cross Over MA5 Alert' + uptrend_message)

alertcondition(
  ta.crossunder(ma3, ma5),
  title   = 'MA3 Cross Under MA5',
  message = 'MA3 Cross Under MA5 Alert' + downtrend_message)

//------------------------------------------------------------------------------

alertcondition(
  ta.crossover(ma4, ma5),
  title   = 'MA4 Cross Over MA5',
  message = 'MA4 Cross Over MA5 Alert' + uptrend_message)

alertcondition(
  ta.crossunder(ma4, ma5),
  title   = 'MA4 Cross Under MA5',
  message = 'MA4 Cross Under MA5 Alert' + downtrend_message)
