[TT] Sectors Dist % From MA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tradersTerminal

//@version=5
//study("Percentage Distance From Moving Average")

indicator("[TT] Sectors Dist % From MA")

MA_Period = input(200)

//list of all sectors
sector1 = input("XLC")
sector2 = input("XLY")
sector3 = input("XLP")
sector4 = input("XLE")
sector5 = input("XLF")
sector6 = input("XLV")
sector7 = input("XLI")
sector8 = input("XLB")
sector9 = input("XLRE")
sector10 = input("XLK")
sector11 = input("XLU")

color1 =input(color.new(color.purple, 0))
color2 =input(color.new(#9cbd51, 0))
color3 =input(color.new(#51a4bd, 0))
color4 =input(color.new(#cc904b, 0))
color5 =input(color.new(#34a039, 0))
color6 =input(color.new(#2f498f, 0))
color7 =input(color.new(#af45aa, 0))
color8 =input(color.new(#868686, 0))
color9 =input(color.new(#ff3e3e, 0))
color10 =input(color.new(#faf740, 0))
color11 =input(color.new(#ffffff, 0))

//get last close and MA
price1 = request.security(sector1,timeframe.period, close)
ma1 = ta.sma(price1,MA_Period)
PercentageDifference1 = (price1 - ma1) / ma1 * 100
plot(PercentageDifference1,"1",color1)

price2 = request.security(sector2,timeframe.period, close)
ma2 = ta.sma(price2,MA_Period)
PercentageDifference2 = (price2 - ma2) / ma2 * 100
plot(PercentageDifference2,"2",color2)

price3 = request.security(sector3,timeframe.period, close)
ma3 = ta.sma(price3,MA_Period)
PercentageDifference3 = (price3 - ma3) / ma3 * 100
plot(PercentageDifference3,"3",color3)


price4 = request.security(sector4,timeframe.period, close)
ma4 = ta.sma(price4,MA_Period)
PercentageDifference4 = (price4 - ma4) / ma4 * 100
plot(PercentageDifference4,"4",color4)

price5 = request.security(sector5,timeframe.period, close)
ma5 = ta.sma(price5,MA_Period)
PercentageDifference5 = (price5 - ma5) / ma5 * 100
plot(PercentageDifference5,"5",color5)


price6 = request.security(sector6,timeframe.period, close)
ma6 = ta.sma(price6,MA_Period)
PercentageDifference6 = (price6 - ma6) / ma6 * 100
plot(PercentageDifference6,"6",color6)


price7 = request.security(sector7,timeframe.period, close)
ma7 = ta.sma(price7,MA_Period)
PercentageDifference7 = (price7 - ma7) / ma7 * 100
plot(PercentageDifference7,"7",color7)

price8 = request.security(sector8,timeframe.period, close)
ma8 = ta.sma(price8,MA_Period)
PercentageDifference8 = (price8 - ma8) / ma8 * 100
plot(PercentageDifference8,"8",color8)


price9 = request.security(sector9,timeframe.period, close)
ma9 = ta.sma(price9,MA_Period)
PercentageDifference9 = (price9 - ma9) / ma9 * 100
plot(PercentageDifference9,"9",color9)

price10 = request.security(sector10,timeframe.period, close)
ma10 = ta.sma(price10,MA_Period)
PercentageDifference10 = (price10 - ma10) / ma10 * 100
plot(PercentageDifference10,"10",color10)

price11 = request.security(sector11,timeframe.period, close)
ma11 = ta.sma(price11,MA_Period)
PercentageDifference11 = (price11 - ma11) / ma11 * 110
plot(PercentageDifference11,"11",color11)