Market Relative Candle Ratio Comparator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NoaTrader

//@version=5
indicator("Market Relative Candle Ratio Comparator","MRCRC",overlay = false)
var source = input.symbol("TVC:DXY","Source of comparision")
var reverse = input.bool(true,"Reverse? (set checked for DXY)","If the comparision source going up should make the current symbol go down, mark this checked")
var avg_len = input.int(1000,"Avg Len",0,tooltip = "This is for comparing each symbol's candle to it's average movement to have a common base for comparision between the two")
var ma_len = input.int(9,"Moving Average's Length",0)
var show_acuumulated = input.bool(true,"Show Accumulated Divergence")
var show_acuumulated_ma = input.bool(true,"Show Accumulated Divergence MA")
var accumulation_lookback = input.int(9,"Accumulation Sum Lookback",tooltip = "How many past candle's divergence should be considered for showing the divergence")

btc_co = math.abs((close/open)-1)
btc_co_ma_1000 = ta.sma(btc_co,avg_len)
btc_sign = close >= open ? 1 : -1


dxy_c = request.security(source,"",close,barmerge.gaps_on)
dxy_o = request.security(source,"",open,barmerge.gaps_on)
dxy_co = math.abs((dxy_c/dxy_o)-1)
dxy_sign = dxy_c >= dxy_o ? 1 : -1
dxy_co_ma_1000 = request.security(source,"",ta.sma(math.abs((close-open)/open),avg_len),barmerge.gaps_on)

btc_var = btc_co/btc_co_ma_1000*btc_sign
dxy_var = dxy_co/dxy_co_ma_1000*dxy_sign * (reverse?-1:1)

plot(btc_var,color = color.orange,style = plot.style_stepline)
plot(dxy_var,color = color.blue,style = plot.style_stepline)

bdrs = btc_var - dxy_var
plot(bdrs ,"Realtive Strength",color = bdrs > 0 ? color.rgb(88, 255, 66) : color.rgb(255, 61, 61),style = plot.style_columns)

sum_acc = 0.0
for i = 1 to accumulation_lookback
    sum_acc := sum_acc + (not na(bdrs[i-1]) ? bdrs[i-1] : 0)
sum_ma = ta.hma(sum_acc,ma_len)
plot(sum_acc ,"Accumulated",color = sum_acc > 0 ? color.rgb(76, 175, 79, 50) : color.rgb(255, 82, 82, 50),style = plot.style_columns,display = show_acuumulated ? display.all : display.none)
plot(sum_ma ,"Accumulated MA",color = sum_ma > 0 ? color.green : color.red,linewidth = 2,display = show_acuumulated_ma ? display.all : display.none)

if bar_index == last_bar_index
    label.new(bar_index+5,0,source,textcolor = color.white,style = label.style_label_left)