Highlight Deduction
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EddisonShyi
// Highlight deduction OHLC bar(and adjacent bars), and draw price line of it. This will make us easier to observe SMA direction.

//@version=5
indicator("Highlight Deduction", overlay=true)
int fast_ma = input(title="Fast SMA", defval=24)
int slow_ma = input(title="Slow SMA", defval=60)
int deduction_highlight_width = input(title="Highlight Width", defval=5)

string chart_period = timeframe.period
int next_fast_ma = switch chart_period
    // Edit below area if you need
    "1" => 5*fast_ma
    "5" => 6*fast_ma
    "30" => 4*fast_ma
    "120" => 12*fast_ma
    "D" => 5*fast_ma
    "W" => 5*fast_ma
    => 
        runtime.error("Undefined Period")
        int(na)
    // Edit above area if you need

fast_deduction_from = last_bar_index - fast_ma + 1
fast_deduction_to = last_bar_index - fast_ma + deduction_highlight_width + 1
slow_deduction_from = last_bar_index - slow_ma + 1
slow_deduction_to = last_bar_index - slow_ma + deduction_highlight_width + 1
next_fast_deduction_from = last_bar_index - next_fast_ma + 1
next_fast_deduction_to = last_bar_index - next_fast_ma + deduction_highlight_width + 1

var fast_ma_deduction = line.new(fast_deduction_from, close[fast_ma], last_bar_index, close[fast_ma], extend=extend.right, style=line.style_dotted, color=color.rgb(82, 252, 108, 25))
if bar_index > fast_deduction_from
    line.set_xy1(fast_ma_deduction, fast_deduction_from, close[fast_ma])
    line.set_xy2(fast_ma_deduction, last_bar_index, close[fast_ma])
    
var slow_ma_deduction = line.new(slow_deduction_from, close[slow_ma], last_bar_index, close[slow_ma], extend=extend.right, style=line.style_dotted, color=color.rgb(252, 252, 82, 25))
if bar_index > slow_deduction_from
    line.set_xy1(slow_ma_deduction, slow_deduction_from, close[slow_ma])
    line.set_xy2(slow_ma_deduction, last_bar_index, close[slow_ma])

var next_fast_ma_deduction = line.new(next_fast_deduction_from, close[next_fast_ma], last_bar_index, close[next_fast_ma], extend=extend.right, style=line.style_dotted, color=color.rgb(236, 80, 80, 25))
if bar_index > next_fast_deduction_from
    line.set_xy1(next_fast_ma_deduction, next_fast_deduction_from, close[next_fast_ma])
    line.set_xy2(next_fast_ma_deduction, last_bar_index, close[next_fast_ma])
    
plot(ta.sma(close, fast_ma), title="FastMA", color=color.lime)
plot(ta.sma(close, slow_ma), title="SlowMA", color=color.yellow)
plot(ta.sma(close, next_fast_ma), title="NFastMA", color=color.red)
bgcolor(bar_index >= fast_deduction_from and bar_index < fast_deduction_to ? color.rgb(82, 252, 108, 80): na, title="FastMADeduction")
bgcolor(bar_index >= slow_deduction_from and bar_index < slow_deduction_to ? color.rgb(252, 252, 82, 80): na, title="SlowMADeduction")
bgcolor(bar_index >= next_fast_deduction_from and bar_index < next_fast_deduction_to ? color.rgb(255, 65, 65, 80): na, title="NextFastMADeduction")