AdxLib
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Hamster-Coder

//@version=5

// @description TODO: add library description here
library("AdxLib", overlay = true)

dirmov(simple int length) =>
	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, length)
	plus = fixnan(100 * ta.rma(plusDM, length) / truerange)
	minus = fixnan(100 * ta.rma(minusDM, length) / truerange)
	[plus, minus]

export create(simple int di_length, simple int adx_length) =>
	[plus, minus] = dirmov(di_length)
	sum = plus + minus
	adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adx_length)

export create(simple int adx_length) =>
	create(adx_length, adx_length)