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

//@version=5
indicator("Dynamic Moving Averages", overlay = true)

//Moving Average Inputs
len0 = input(title="MA Length", defval = 50, group ="Long Term Trend MA", inline = "line 1", 
  tooltip = "This Trend MA defines the trend. 
  The source of a Dynamic MA will be \"low\" when trading above trend MA and \"high\" when below the Trend MA.")
len1 = input(title="MA Length", defval = 5, group = "Moving Average 1", inline = "line 1")
len2 = input(title="MA Length", defval = 10, group ="Moving Average 2", inline = "line 1")
len3 = input(title="MA Length", defval = 20, group ="Moving Average 3", inline = "line 1")
len4 = input(title="MA Length", defval = 100, group ="Moving Average 4", inline = "line 1")
len5 = input(title="MA Length", defval = 200, group ="Moving Average 5", inline = "line 1")
len6 = input(title="MA Length", defval = 350, group ="Moving Average 6", inline = "line 1")

src0 = input(close, title="MA 1 Source", group = "Long Term Trend MA", inline = "line 1")
src1 = input(close, title="MA 1 Source", group = "Moving Average 1", inline = "line 1")
src2 = input(close, title="MA 1 Source", group = "Moving Average 2", inline = "line 1")
src3 = input(close, title="MA 1 Source", group = "Moving Average 3", inline = "line 1")
src4 = input(close, title="MA 1 Source", group = "Moving Average 4", inline = "line 1")
src5 = input(close, title="MA 1 Source", group = "Moving Average 5", inline = "line 1")
src6 = input(close, title="MA 1 Source", group = "Moving Average 6", inline = "line 1")

isDynamic1 = input(title = "Dynamic M.A.?", defval = true, group = "Moving Average 1", inline = "line 2", 
  tooltip = "Changes the source to high or low based on the trend")
isDynamic2 = input(title = "Dynamic M.A.?", defval = true, group = "Moving Average 2", inline = "line 2", 
  tooltip = "Changes the source to high or low based on the trend")
isDynamic3 = input(title = "Dynamic M.A.?", defval = true, group = "Moving Average 3", inline = "line 2", 
  tooltip = "Changes the source to high or low based on the trend")
isDynamic4 = input(title = "Dynamic M.A.?", defval = true, group = "Moving Average 4", inline = "line 2", 
  tooltip = "Changes the source to high or low based on the trend")
isDynamic5 = input(title = "Dynamic M.A.?", defval = true, group = "Moving Average 5", inline = "line 2", 
  tooltip = "Changes the source to high or low based on the trend")
isDynamic6 = input(title = "Dynamic M.A.?", defval = true, group = "Moving Average 6", inline = "line 2", 
  tooltip = "Changes the source to high or low based on the trend")

//Create Options for Type of Moving Average
maType0 = input.string(defval = "sma", options = ["sma", "ema"], title = "MA Type", group = "Long Term Trend MA", inline = "inline 2")
maType1 = input.string(defval = "sma", options = ["sma", "ema"], title = "MA Type", group = "Moving Average 1", inline = "inline 2")
maType2 = input.string(defval = "sma", options = ["sma", "ema"], title = "MA Type", group = "Moving Average 2", inline = "inline 2")
maType3 = input.string(defval = "sma", options = ["sma", "ema"], title = "MA Type", group = "Moving Average 3", inline = "inline 2")
maType4 = input.string(defval = "sma", options = ["sma", "ema"], title = "MA Type", group = "Moving Average 4", inline = "inline 2")
maType5 = input.string(defval = "sma", options = ["sma", "ema"], title = "MA Type", group = "Moving Average 5", inline = "inline 2")
maType6 = input.string(defval = "sma", options = ["sma", "ema"], title = "MA Type", group = "Moving Average 6", inline = "inline 2")


//Create MAs based on Dynamics and Type of EMA
ma0 = maType0 == "sma" ? ta.sma(src0,len0) : ta.ema(src0,len0)
ma1 = maType1 == "sma" ? ta.sma(isDynamic1 ? (close > ma0 ? low : high) : src1,len1) : ta.ema(isDynamic1 ? (close > ma0 ? low : high) : src1,len1)
ma2 = maType2 == "sma" ? ta.sma(isDynamic2 ? (close > ma0 ? low : high) : src2,len2) : ta.ema(isDynamic2 ? (close > ma0 ? low : high) : src2,len2)
ma3 = maType3 == "sma" ? ta.sma(isDynamic3 ? (close > ma0 ? low : high) : src3,len3) : ta.ema(isDynamic3 ? (close > ma0 ? low : high) : src3,len3)
ma4 = maType4 == "sma" ? ta.sma(isDynamic4 ? (close > ma0 ? low : high) : src4,len4) : ta.ema(isDynamic4 ? (close > ma0 ? low : high) : src4,len4)
ma5 = maType5 == "sma" ? ta.sma(isDynamic5 ? (close > ma0 ? low : high) : src5,len5) : ta.ema(isDynamic5 ? (close > ma0 ? low : high) : src5,len5)
ma6 = maType6 == "sma" ? ta.sma(isDynamic6 ? (close > ma0 ? low : high) : src6,len6) : ta.ema(isDynamic6 ? (close > ma0 ? low : high) : src6,len6)


//Plot the Moving Averages
plotma0 = plot(ma0, title = "ma0", color = color.red)
plotma1 = plot(ma1, title = "ma1", color = color.orange)
plotma2 = plot(ma2, title = "ma2", color = color.yellow)
plotma3 = plot(ma3, title = "ma3", color = color.green)
plotma4 = plot(ma4, title = "ma4", color = color.blue)
plotma5 = plot(ma5, title = "ma5", color = color.navy)
plotma6 = plot(ma6, title = "ma6", color = color.purple)


