MA total distance on chart
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AliSignals

//@version=5
indicator('MA total distance on chart',shorttitle="MATD", overlay=true, timeframe="")

//declaring function to use different types of MA
maf(source, length, type) =>
    type == 'SMA' ? ta.sma(source, length) : type == 'EMA' ? ta.ema(source, length) : type == 'SMMA (RMA)' ? ta.rma(source, length) : type == 'WMA' ? ta.wma(source, length) : type == 'VWMA' ? ta.vwma(source, length) : na
// inputs
Malength = input(100, title = "MA Length")
src = input(close, title = "MA Source")
type = input.string(     'SMA', title='MA type', inline='MA Type', options=['SMA', 'EMA', 'SMMA (RMA)', 'WMA', 'VWMA', 'linreg'])
Madistancelength = input(40, title = "Distance Length")
srcdistance = input(hl2, title = "Distance Source")
smoothing = input(40, title = "smooothing")




// running fuction 
ma = maf(src, Malength, type)




// calculating distance
distance = srcdistance - ma

// Average of distances
smadistance = ta.sma(distance, Madistancelength)

// MATD and smoothing of it
MATD = srcdistance - smadistance
Smoothed = ta.sma(MATD, smoothing)

fillcolor= color.rgb(55,198,255,90)
p1 = plot(Smoothed, color=color.new(color.red, 0), title="MATD")
p2 = plot(ma, color=color.new(color.blue, 0), title = "MA")
// I know it's not a real cloud. LOL
fill(p1, p2, color=fillcolor, title = "cloud")

