Moving Average Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EsIstTurnt

//@version=5
indicator("Moving Average Candles",overlay=true)
t_Body  = input.int(95,"Body Transparency"   ,0,100,5,group="Transparency")// \
t_Border= input.int(70,"Border Transparency" ,0,100,5,group="Transparency")//  ----Candle Transparency Settings
t_Wick  = input.int(100,"Wick Transparency"  ,0,100,5,group="Transparency")// /

maType  = input.string("ALMA","Average Type",["SMA","EMA","WMA","RMA","HMA","VWMA","ALMA","MED","LRC"],group="Inputs")// Averaging method selection

len1    = input.int(16 ,"Length #1",1,group="Inputs")// \
len2    = input.int(32 ,"Length #2",1,group="Inputs")//  \
len3    = input.int(64 ,"Length #3",1,group="Inputs")//   \____ Lengths to use for Moving Averages
len4    = input.int(96 ,"Length #4",1,group="Inputs")//   /
len5    = input.int(128,"Length #5",1,group="Inputs")//  /
len6    = input.int(160,"Length #6",1,group="Inputs")// /

useClose= input.bool(false,"Draw each candle from MA to close instead of sequential MA")//Alternative Candle plotting style because why not


o= input.float(1.0  ,"Offset",tooltip="Relevant only if maType == LRC or ALMA",group="Inputs")//Float offset value for ALMA/LRC
f= math.round(o)// Round it for use in LRC calculation
s= input.float(3.0  ,"Sigma" ,tooltip="Relevant only if maType == ALMA",group="Inputs")// Float sigma value for ALMA
maCandles(l1,l2,l3,l4,l5,l6,src=close,type)=>
    [ma1,ma2,ma3,ma4,ma5,ma6] = switch type//
        "SMA"  => [ta.sma   (src,l1)    ,ta.sma   (src,l2)    ,ta.sma   (src,l3)    ,ta.sma   (src,l4)    ,ta.sma   (src,l5)    ,ta.sma   (src,l6)    ]// \
        "EMA"  => [ta.ema   (src,l1)    ,ta.ema   (src,l2)    ,ta.ema   (src,l3)    ,ta.ema   (src,l4)    ,ta.ema   (src,l5)    ,ta.ema   (src,l6)    ]//  \
        "WMA"  => [ta.wma   (src,l1)    ,ta.wma   (src,l2)    ,ta.wma   (src,l3)    ,ta.wma   (src,l4)    ,ta.wma   (src,l5)    ,ta.wma   (src,l6)    ]//   \
        "RMA"  => [ta.sma   (src,l1)    ,ta.sma   (src,l2)    ,ta.sma   (src,l3)    ,ta.sma   (src,l4)    ,ta.sma   (src,l5)    ,ta.sma   (src,l6)    ]//    \
        "HMA"  => [ta.ema   (src,l1)    ,ta.ema   (src,l2)    ,ta.ema   (src,l3)    ,ta.ema   (src,l4)    ,ta.ema   (src,l5)    ,ta.ema   (src,l6)    ]//     ---- Get the raw MA value for each length
        "VWMA" => [ta.vwma  (src,l1)    ,ta.vwma  (src,l2)    ,ta.vwma  (src,l3)    ,ta.vwma  (src,l4)    ,ta.vwma  (src,l5)    ,ta.vwma  (src,l6)    ]//    /
        "ALMA" => [ta.alma  (src,l1,o,s),ta.alma  (src,l2,o,s),ta.alma  (src,l3,o,s),ta.alma  (src,l4,o,s),ta.alma  (src,l5,o,s),ta.alma  (src,l6,o,s)]//   /
        "MED"  => [ta.median(src,l1)    ,ta.median(src,l2)    ,ta.median(src,l3)    ,ta.median(src,l4)    ,ta.median(src,l5)    ,ta.median(src,l6)    ]//  / 
        "LRC"  => [ta.linreg(src,l1,f)  ,ta.linreg(src,l2,f)  ,ta.linreg(src,l3,f)  ,ta.linreg(src,l4,f)  ,ta.linreg(src,l5,f)  ,ta.linreg(src,l6,f)  ]// /

    ma1_=useClose?close:ma1// \
    ma2_=useClose?close:ma2//  \
    ma3_=useClose?close:ma3//   \____If bool "useClose" is true swap out the ma value with the close value
    ma4_=useClose?close:ma4//   /
    ma5_=useClose?close:ma5//  /
    ma6_=useClose?close:ma6// /
    [c1o,c1h,c1l,c1c]= request.security(syminfo.ticker,timeframe.period,[ma1,math.max(ma1,close),math.min(ma1,close),close])// \
    [c2o,c2h,c2l,c2c]= request.security(syminfo.ticker,timeframe.period,[ma2,math.max(ma2,ma1_),math.min(ma2,ma1_),ma1_]   )//  \
    [c3o,c3h,c3l,c3c]= request.security(syminfo.ticker,timeframe.period,[ma3,math.max(ma3,ma2_),math.min(ma3,ma2_),ma2_]   )//   \____Get "OHLC" data as a tuple via request.security using the values above rather than the standard open,high,low,close
    [c4o,c4h,c4l,c4c]= request.security(syminfo.ticker,timeframe.period,[ma4,math.max(ma4,ma3_),math.min(ma4,ma3_),ma3_]   )//   /
    [c5o,c5h,c5l,c5c]= request.security(syminfo.ticker,timeframe.period,[ma5,math.max(ma5,ma4_),math.min(ma5,ma4_),ma4_]   )//  /
    [c6o,c6h,c6l,c6c]= request.security(syminfo.ticker,timeframe.period,[ma6,math.max(ma6,ma5_),math.min(ma6,ma5_),ma5_]   )// /
    
    [c1o,c1h,c1l,c1c,c2o,c2h,c2l,c2c,c3o,c3h,c3l,c3c,c4o,c4h,c4l,c4c,c5o,c5h,c5l,c5c,c6o,c6h,c6l,c6c]// Return All of the requested data as tuple

[c1o,c1h,c1l,c1c,c2o,c2h,c2l,c2c,c3o,c3h,c3l,c3c,c4o,c4h,c4l,c4c,c5o,c5h,c5l,c5c,c6o,c6h,c6l,c6c]=maCandles(len1,len2,len3,len4,len5,len6,close,maType) // Use "maCandles()" function defined above with user inputs to get the data for plotting
upCol=input.color(#acfb00)//Color if ma1,2,3,etc > ma2,3,4,etc
dnCol=input.color(#ff0000)//Color if ma1,2,3,etc < ma2,3,4,etc

color(o,c,t=85)=>o<c?color.new(upCol,t):color.new(dnCol,t)//Function for coloring 

plotcandle(c1o,c1h,c1l,c1c,"MA1-->(Close)        ",color=color(c1o,c1c,t_Body),bordercolor=color(c1o,c1c,t_Border),wickcolor=color(c1o,c1c,t_Wick),display = display.pane)// \
plotcandle(c2o,c2h,c2l,c2c,"MA2-->( MA1 or Close)",color=color(c2o,c2c,t_Body),bordercolor=color(c2o,c2c,t_Border),wickcolor=color(c2o,c2c,t_Wick),display = display.pane)//  \
plotcandle(c3o,c3h,c3l,c3c,"MA3-->( MA2 or Close)",color=color(c3o,c3c,t_Body),bordercolor=color(c3o,c3c,t_Border),wickcolor=color(c3o,c3c,t_Wick),display = display.pane)//   \____ Now plot each candle
plotcandle(c4o,c4h,c4l,c4c,"MA4-->( MA3 or Close)",color=color(c4o,c4c,t_Body),bordercolor=color(c4o,c4c,t_Border),wickcolor=color(c4o,c4c,t_Wick),display = display.pane)//   /   
plotcandle(c5o,c5h,c5l,c5c,"MA5-->( MA4 or Close)",color=color(c5o,c5c,t_Body),bordercolor=color(c5o,c5c,t_Border),wickcolor=color(c5o,c5c,t_Wick),display = display.pane)//  /
plotcandle(c6o,c6h,c6l,c6c,"MA6-->( MA5 or Close)",color=color(c6o,c6c,t_Body),bordercolor=color(c6o,c6c,t_Border),wickcolor=color(c6o,c6c,t_Wick),display = display.pane)// /