Range of a source displayed in thirds
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © m_b_round

//@version=5
indicator("Source Range in thirds", overlay=false)

bandSource = input.source(close, "Source")
// bandQuantity = input.int(3, "Number of Bands")
lookbackPeriod = input.int(100, "Lookback Period")
addMA = input.bool(false, "Add Moving Average")
maType = input.string("EMA", "Moving Average Type", options=["SMA", "EMA", "WMA"])
maLength = input.int(9, "MA Length")

sourceLine = input.color(color.purple, "Source Plot", group="Colours")
highestLine = input.color(color.green, "High Point", group="Colours")
midLines = input.color(color.white, "Mid Lines", group="Colours")
lowestLine = input.color(color.red, "Low Point", group="Colours")
maLine = input.color(color.yellow, "MA Plot", group="Colours")

highPoint = ta.highest(bandSource, lookbackPeriod)
lowPoint = ta.lowest(bandSource, lookbackPeriod)

plot(bandSource, "Indicator", color=sourceLine, linewidth = 2)
plot(highPoint, "Highest Point", color=highestLine)
plot(lowPoint + ((highPoint-lowPoint)*(float(2)/3)), "Top Third", color=midLines)

plot(lowPoint + ((highPoint-lowPoint)*(float(1)/3)), "Bottom Third", color=midLines)
plot(lowPoint, "Lowest Point", color=lowestLine)

calculatedMA = switch maType
    "SMA" => ta.sma(bandSource, maLength)
    "EMA" => ta.ema(bandSource, maLength)
    "WMA" => ta.wma(bandSource, maLength)

plot(addMA ? calculatedMA : na, "Average (Source)", color=maLine)