MTF Stationary Extreme Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeafAlgo

//@version=5
indicator('Multiple Timeframe Stationary Extreme Indicator', shorttitle='MTF SEI')

// Input parameters
periodsBack = input(5, title='Periods Back')
higherTF = input('240', title='Higher Timeframe', tooltip = 'Higher Timeframe defaulted to 240 minutes.')

// Calculate stationary extreme indicator on current timeframe
highDiff = (high - high[periodsBack]) * 100
lowDiff = (low - low[periodsBack]) * 100

// Calculate stationary extreme indicator on higher timeframe
higherTFHighDiff = request.security(syminfo.tickerid, higherTF, high - high[periodsBack]) * 100
higherTFLowDiff = request.security(syminfo.tickerid, higherTF, low - low[periodsBack]) * 100

// Plotting
plot(highDiff, title='High Difference', color=color.new(color.green, 0), linewidth=3)
plot(lowDiff, title='Low Difference', color=color.new(color.red, 0), linewidth=3)
hline(0)
h1 = hline(1)
h11 = hline(-1)
h2 = hline(2)
h22 = hline(-2)
h3 = hline(3)
h33 = hline(-3)
h5 = hline(5)
h55 = hline(-5)

fill(h1, h2, color=color.new(color.lime, 90))
fill(h2, h3, color=color.new(color.lime, 70))
fill(h3, h5, color=color.new(color.lime, 50))

fill(h11, h22, color=color.new(color.fuchsia, 90))
fill(h22, h33, color=color.new(color.fuchsia, 70))
fill(h33, h55, color=color.new(color.fuchsia, 50))

// Plotting higher timeframe stationary extreme indicator
plot(higherTFHighDiff, title='Higher Timeframe High Difference', color=color.new(color.blue, 0), linewidth=2, offset=-periodsBack)
plot(higherTFLowDiff, title='Higher Timeframe Low Difference', color=color.new(color.orange, 0), linewidth=2, offset=-periodsBack)

