Flying Dragon Trend Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MarkoP010 2023

//@version=5
//Indicator plots user selectable Moving Average lines and a colored trend band between the MA lines. The trend bands can be turned off individually if required.
//Trend pivot indicators are shown by selected risk level either when there is MA crossover with price (Leading MA Offset1 on Highest risk level, Lagging MA on Low risk level) or three bands with the same color at the same time (on Lowest risk level, Leading MA Offset1 crossover with Lagging MA). Pivot indicators can be turned off if required.
//The Offset option shifts the selected MA with the set number of steps to the right. That is where the Magic happens and the Dragon roars!

//Indicator version history:
//Version 1 - Initial script
//Version 2 - Bug fix for Lowest risk level to show the indicator
//Version 3 - Alert function added, enabled when indicators are on

indicator("Flying Dragon Trend Indicator", shorttitle="Flying Dragon Trend Indicator", overlay=true)

//Moving Averages function
MA(source, length, type) =>
    type == "EMA" ? ta.ema(source, length) :
     type == "HMA" ? ta.hma(source, length) :
     type == "RMA" ? ta.rma(source, length) :
     type == "SMA" ? ta.sma(source, length) :
     type == "SWMA" ? ta.swma(source) :
     type == "VWMA" ? ta.vwma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     na

//Indicator options
riskLevel = input.string(defval="Medium", title="Risk Level", options=["Highest", "High", "Medium", "Low", "Lowest"], inline="Options", group="Indicator Options")
indicatorsOn = input(defval=true, title="Indicators", inline="Options", tooltip="Turn on or off pivot indicators. When risk level Highest then Leading MA Offset1 crossover with price triggers the indicator, when Low then Lagging MA crossover with price, when Lowest then all the Bands are the same color (Leading MA Offset1 crossover with Lagging MA). Alerts are enabled when indicators are on. Create Alert on selected equity and timeframe, select on Condition this indicator and Any alert () function call, Create.", group="Indicator Options")

//Inputs
ma1Type = input.string(defval="HMA", title="", inline="MA1", options=["EMA", "HMA", "RMA", "SMA","SWMA", "VWMA", "WMA"], group="Leading Moving Average") 
ma1Length = input.int(defval=35, title="",minval=1, inline="MA1", group="Leading Moving Average")
ma1Source = input(defval=close, title="", tooltip="For short timeframes, minutes to hours, instead of Default values try Lowest risk level and HMA75 with Offsets 0,1,4 and SMA12 with Offset 6.", inline="MA1", group="Leading Moving Average")
ma1Color  = input(defval=color.purple, title="", inline="MA-1", group="Leading Moving Average")
ma1Offset = input.int(defval=0, title="Offset1 Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-1", group="Leading Moving Average")
ma1 = MA(ma1Source, ma1Length, ma1Type)[ma1Offset]

ma2Color  = input(defval=color.lime, title="", inline="MA-2", group="Leading Moving Average")
ma2Offset = input.int(defval=4, title="Offset2 Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-2", group="Leading Moving Average")
ma2 = ma1[ma2Offset]

ma3Color  = input(defval=color.aqua, title="", inline="MA-3", group="Leading Moving Average")
ma3Offset = input.int(defval=6, title="Offset3 Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-3", group="Leading Moving Average")
ma3 = ma1[ma3Offset]

ma4Type = input.string(defval="SMA", title="", inline="MA4", options=["EMA", "HMA", "RMA", "SMA","SWMA", "VWMA", "WMA"], group="Lagging Moving Average") 
ma4Length = input.int(defval=22, title="",minval=1, inline="MA4", group="Lagging Moving Average")
ma4Source = input(defval=close, title="", inline="MA4", group="Lagging Moving Average")
ma4Color  = input(defval=color.yellow, title="", inline="MA-4", group="Lagging Moving Average")
ma4Offset = input.int(defval=2, title="Offset Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-4", group="Lagging Moving Average")
ma4 = MA(ma4Source, ma4Length, ma4Type)[ma4Offset]

bandTransp = input.int(defval=60, title="Band Transparency", minval=20, maxval=80, step=10, group="Banding")
useBand1 = input(defval=true, title="Band 1", inline="Band", group="Banding")
band1Transp = useBand1 ? bandTransp : 100
band1clr = ma1 > ma2 ? color.new(#00ff00, transp=band1Transp) : color.new(#ff0000, transp=band1Transp)
useBand2 = input(defval=true, title="Band 2", inline="Band", group="Banding")
band2Transp = useBand2 ? bandTransp : 100
band2clr = ma1 > ma3 ? color.new(#00ff00, transp=band2Transp) : color.new(#ff0000, transp=band2Transp)
useBand3 = input(defval=true, title="Band 3", tooltip="Up trend green, down trend red. Colors get reversed if MA1 lenght is greater than MA2 lenght, or they are different type and MA2 quicker. In that case, just reverse your selections for MA1 and MA2, or let it be as is.", inline="Band", group="Banding")
band3Transp = useBand3 ? bandTransp : 100
band3clr = ma1 > ma4 ? color.new(#00ff00, transp=band3Transp) : color.new(#ff0000, transp=band3Transp)

//Graphs
piirto1 = plot(ma1, color = ma1Color, title="MA1")
piirto2 = plot(ma2, color = ma2Color, title="MA2")
piirto3 = plot(ma3, color = ma3Color, title="MA3")
piirto4 = plot(ma4, color = ma4Color, title="MA4")

fill(piirto1, piirto2, color=band1clr)
fill(piirto1, piirto3, color=band2clr)
fill(piirto1, piirto4, color=band3clr)

//Indicator plot conditions
longCondition = riskLevel == "Highest" ? ma1Source > ma1 : riskLevel == "High" ? ma1Source > ma2 : riskLevel == "Medium" ? ma1Source > ma3 : riskLevel == "Low" ? ma4Source > ma4 : riskLevel == "Lowest" ? ma1 > ma4 : na
shortCondition = riskLevel == "Highest" ? ma1Source < ma1 : riskLevel == "High" ? ma1Source < ma2 : riskLevel == "Medium" ? ma1Source < ma3 : riskLevel == "Low" ? ma4Source < ma4 : riskLevel == "Lowest" ? ma1 < ma4 : na
pivotUp = longCondition and shortCondition[1] and indicatorsOn ? true : na
pivotDown = shortCondition and longCondition[1] and indicatorsOn ? true : na
plotshape(pivotUp, title="Up", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(pivotDown, title="Down", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)

//Alerts
if pivotUp
    alertMessage = syminfo.ticker + ' - ' + timeframe.period + ": Pivot Up!"
    alert(alertMessage, alert.freq_once_per_bar_close)
if pivotDown
    alertMessage = syminfo.ticker + ' - ' + timeframe.period + ": Pivot Down!"
    alert(alertMessage, alert.freq_once_per_bar_close)

//End
