Tunable SWMA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EsIstTurnt
//Modified From long form of ta.swma function :

//pine_swma(source) =>
	//source[3] * 1 / 6 + source[2] * 2 / 6 + 
	//source[1] * 2 / 6 + source[0] * 1 / 6
//plot(pine_swma(close)


//SYMMETRICALLY WEIGHTED
//        V 
//SELECTABLY WEIGHTED 


//@version=5
indicator("Tunable Selectably Weighed Moving Average",shorttitle='TSWMA',overlay=true)
tf        = input.timeframe('','TimeFrame')
maInput   = input.string(title="Input", defval="None", options=["None","EMA", "SMA", "VWMA", "WMA","VWAP","LRC"],group='Filter')
maOutput  = input.string(title="Output", defval="None", options=["None","EMA", "SMA", "VWMA", "WMA","VWAP","LRC"],group='Filter')

length    = input.int(64,'Input/Output Length',group="Filter")
customsrc = input.source(close,'Source Input')
togglesrc = input.bool(true,'Use User Selected Source',group="Source") 
input     = maInput == "EMA"?  ta.ema(customsrc, length)
  :maInput == "SMA" ? ta.sma   (customsrc, length)
  :maInput == "VWMA"? ta.vwma  (customsrc, length)
  :maInput == "WMA" ? ta.wma   (customsrc, length)
  :maInput == "VWAP"? ta.vwap  (customsrc)
  :maInput == "LRC" ? ta.linreg(customsrc, length,0)
  :maInput == "None"?customsrc:na
Weight1   = input.int(16,'Weight for Bar 1 ',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight2   = input.int(15,'Weight for Bar 2 ',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight3   = input.int(14,'Weight for Bar 3 ',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight4   = input.int(13,'Weight for Bar 4 ',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight5   = input.int(12,'Weight for Bar 5 ',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight6   = input.int(11,'Weight for Bar 6 ',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight7   = input.int(10,'Weight for Bar 7 ',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight8   = input.int(9 ,'Weight for Bar 8 ',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')

Weight9   = input.int(8 ,'Weight for Bar 9 ',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight10  = input.int(8 ,'Weight for Bar 10',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight11  = input.int(7 ,'Weight for Bar 11',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight12  = input.int(7 ,'Weight for Bar 12',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight13  = input.int(6 ,'Weight for Bar 13',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight14  = input.int(6 ,'Weight for Bar 14',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight15  = input.int(5 ,'Weight for Bar 15',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Weight16  = input.int(5 ,'Weight for Bar 16',options=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],group='Weights')
Divisor1 = 1+2+2+1 // For default swma => Sum of Weight's (1,2,2,1) == Divisor
Divisor2 = (Weight1+Weight2 +Weight3 +Weight4 +Weight5 +Weight6 +Weight7 +Weight8 +Weight9 + Weight10+ Weight11+ Weight12+ Weight13+ Weight14+ Weight15+ Weight16 ) //Add weight Selections From above 
int1    = input.int(0   ,' Most Recent Bar (#1)',group='Lookback')
int2    = input.int(1   ,' Previous Bar (#2)   ',group='Lookback')
int3    = input.int(2   ,' Previous Bar (#3)   ',group='Lookback')
int4    = input.int(3   ,' Previous Bar (#4)   ',group='Lookback')
int5    = input.int(6   ,' Previous Bar (#5)   ',group='Lookback')
int6    = input.int(12  ,' Previous Bar (#6)   ',group='Lookback')
int7    = input.int(16  ,' Previous Bar (#7)   ',group='Lookback')
int8    = input.int(24  ,' Previous Bar (#8)   ',group='Lookback')

H    = input.bool(true,'Use ta.highest value for 4 selections below ')
L    = input.bool(true,'Use ta.lowest  value for 4 selections below ')
intL = input.int(32 ,'Lowest' ,group='Lowest bar lookback           ')
intH = input.int(32 ,'Highest',group='Highest bar  lookback         ')

int9    = input.int(64  ,' Previous Bar (#9 ) (Toggle Highest) ',group="Lookback Highest")
int10   = input.int(64  ,' Previous Bar (#10) (Toggle Lowest ) ',group="Lookback Lowest ")
int11   = input.int(96  ,' Previous Bar (#11) (Toggle Highest) ',group="Lookback Highest")
int12   = input.int(96  ,' Previous Bar (#12) (Toggle Lowest ) ',group="Lookback Lowest ")
int13   = input.int(128 ,' Previous Bar (#13) (Toggle Highest) ',group="Lookback Highest")
int14   = input.int(128 ,' Previous Bar (#14) (Toggle Lowest ) ',group="Lookback Lowest ")
int15   = input.int(256 ,' Previous Bar (#15) (Toggle Highest) ',group="Lookback Highest")
int16   = input.int(256 ,' Previous Bar (#16) (Toggle Lowest ) ',group="Lookback Lowest ")


src     = ta.ema(togglesrc?input:ta.sma(math.avg(open,open[int3],open[int5])<math.avg(close,close[int3],close[int5]) ? math.avg(ta.lowest(low,16),ta.highest(high,8),close):math.avg(ta.highest(high,16),ta.lowest(low,8),close),int5),int6)


srcH1 = H? ta.highest(src[int9 ],intH):src[int9 ] 
srcL1 = L? ta.lowest (src[int10],intL):src[int10] 
srcH2 = H? ta.highest(src[int11],intH):src[int11] 
srcL2 = L? ta.lowest (src[int12],intL):src[int12] 
srcH3 = H? ta.highest(src[int13],intH):src[int13] 
srcL3 = L? ta.lowest (src[int14],intL):src[int14] 
srcH4 = H? ta.highest(src[int15],intH):src[int15] 
srcL4 = L? ta.lowest (src[int16],intL):src[int16] 

//Default SWMA
pine_swma(src) => 
	src[int4] * 1 / Divisor1 +
       src[int3] * 2 / Divisor1 +
       src[int2] * 2 / Divisor1 +
       src[int1] * 1 / Divisor1

//Configurable SWMA
tuned_swma(src) => //if Weight is Changed resulting in a different total when added together, you must change Divisor2 Above
	 src[int1] * Weight1 / Divisor2 +
       src[int2] * Weight2  / Divisor2 +
       src[int3] * Weight3  / Divisor2 +
       src[int4] * Weight4  / Divisor2 +
       src[int5] * Weight5  / Divisor2 +
       src[int6] * Weight6  / Divisor2 +
       src[int7] * Weight7  / Divisor2 +
       src[int8] * Weight8  / Divisor2 +
       srcH1  * Weight9  / Divisor2 +
       srcL1  * Weight10 / Divisor2 +
       srcH2  * Weight11 / Divisor2 +
       srcL2  * Weight12 / Divisor2 +
       srcH3  * Weight13 / Divisor2 +
       srcL3  * Weight14 / Divisor2 +
       srcH4  * Weight15 / Divisor2 +
       srcL4  * Weight16 / Divisor2 
basis = maOutput == "EMA"?ta.ema(tuned_swma(src), length)
  :maOutput == "SMA" ? ta.sma   (tuned_swma(src), length)
  :maOutput == "VWMA"? ta.vwma  (tuned_swma(src), length)
  :maOutput == "WMA" ? ta.wma   (tuned_swma(src), length)
  :maOutput == "VWAP"? ta.vwap  (tuned_swma(src))
  :maOutput == "LRC" ? ta.linreg(tuned_swma(src), length,0)
  :maOutput == "None"? tuned_swma(src):na

//Plot
plot(request.security(syminfo.ticker,tf,pine_swma (src)),color=#acfb00,title='Stock SWMA ')
plot(request.security(syminfo.ticker,tf,basis),color=#ff0000,title='Tuned SWMA')
