BTC Dominance Trend
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// This simple script applies the DMI indicator to the BTC dominance chart (BTC.D).
// © gi9opx

//@version=4
study("BTC Dominance Trend", overlay=true)

res = input("D", title="Timeframe", type=input.resolution)

BTCDominance = security ("BTC.D", res, close)
BTCDomHigh = security ("BTC.D", res, high)
BTCDomLow = security ("BTC.D", res, low)
BTCDomTr = security ("BTC.D", res, tr)

//================================DMI
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
threshold = input(80, title="Threshold")


//Script for Indicator
dirmov(len) =>
	up = change(BTCDomHigh)
	down = -change(BTCDomLow)
	truerange = rma(BTCDomTr, len)
	plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange)
	minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange)
	[plus, minus]
	
adx(dilen, adxlen) => 
	[plus, minus] = dirmov(dilen)
	sum = plus + minus
	adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
	[adx, plus, minus]

[sig, up, down] = adx(dilen, adxlen)

uptrend = up>down
downtrend = down>up
trending = sig>threshold
ranging = sig<threshold

bgcolor(trending and downtrend? color.purple : na, transp = 90)
bgcolor(trending and uptrend? color.olive : na, transp = 90)

