Heiken Ashi Swing Range Filter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wallneradam

//@version=5
indicator("Heiken Ashi Swing Range Filter", "HARF", overlay=true, timeframe="", timeframe_gaps=false)

import wallneradam/TAExt/10

swing_use_ha = input.bool(false, "Use HA Open/Close as swing high/low", group="Range")

// Heiken-ashi candles
[o, h, l, c] = TAExt.heiken_ashi()

// Calculate swing range
var float swing_high = na
var float swing_low = na
if c[1] > o[1] and c < o
    swing_high := swing_use_ha ? c[1] : high[1] 
else if c[1] < o[1] and c > o
    swing_low := swing_use_ha ? o[1] : low[1]

// Check if bar is inside the range
in_range = (high > swing_high and low < swing_low) or (high < swing_high and low > swing_low)
if not in_range
    // Check if bar is more inside than outside
    if high > swing_high
        in_range := high - swing_high <= swing_high - low
    else
        in_range := swing_low - low <= high - swing_low

barcolor(in_range ? #444444 : na)
bgcolor(in_range ? color.new(color.gray, 98) : na)

plot(swing_high, "Swing High", color=color.new(color.green, 30), style=plot.style_stepline, display=display.none)
plot(swing_low, "Swing Low", color=color.new(color.red, 30), style=plot.style_stepline, display=display.none)