Mazuuma Churn Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Mazuuma

//@version=5
indicator("Mazuuma Churn Indicator", overlay=true, timeframe="")

priceEmaInput = input.int(title="Price EMA",  minval = 1, maxval = 1000, defval = 200)
rsiEmaInput = input.int(title="RSI EMA",  minval = 1, maxval = 1000, defval = 12)
atrMaInput = input.int(title="ATR MA",  minval = 1, maxval = 1000, defval = 20)
adxThresholdInput = input.int(title="ADX Threshold",  minval = 1, maxval = 1000, defval = 25)

priceEma = ta.ema(close, priceEmaInput)
rsi = ta.rsi(close, 14)
rsiEma = ta.ema(rsi, rsiEmaInput)
atr = ta.atr(14)
atrMa = ta.sma(atr, atrMaInput)

// ADX code is copied directly from ADX indicator
adxlen = 14
dilen = 14
dirmov(len) =>
	up = ta.change(high)
	down = -ta.change(low)
	plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
	minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
	truerange = ta.rma(ta.tr, len)
	plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
	minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
	[plus, minus]
adx(dilen, adxlen) =>
	[plus, minus] = dirmov(dilen)
	sum = plus + minus
	adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
// --------------

// Define background colors
unconfirmedColor = color.new(#FFFF00, 75) // Yellow
partiallyConfirmedColor = color.new(#FF8000, 75) // Orange
confirmedColor = color.new(#ff0000, 75) // Red

priceEmaPercentage = math.abs((1 - close / priceEma) * 100)

weight = 0
if priceEmaPercentage < 16
    weight := weight + 1
if rsiEma > 40 and rsiEma < 60
    weight := weight + 1
if sig < adxThresholdInput
    weight := weight + 1
    
plotchar(weight, "Weight", "", location = location.top)
    
unconfirmed = atr < atrMa and weight == 1
partiallyConfirmed = atr < atrMa and weight == 2
confirmed = atr < atrMa and weight == 3

sessioncolor = 
  unconfirmed ? unconfirmedColor : 
  partiallyConfirmed ? partiallyConfirmedColor : 
  confirmed ? confirmedColor : na

bgcolor(sessioncolor)
