WillyCycle Oscillator&DoubleMa/ErkOzi/version 2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ErkOzi

//@version=5
indicator(title='WillyCycle Oscillator&DoubleMa/ErkOzi/version 2x', shorttitle='WCODMA')

// Willy period
length = input.int(240, title='LTF Period')
upper = ta.highest(length)
lower = ta.lowest(length)

// Willy period HTF
length2 = input.int(480, title='HTF Period')
upper2 = ta.highest(length2)
lower2 = ta.lowest(length2)

// Calculate willy
out = 100 * (close - lower) / (upper - lower)
src = out
len = input.int(55, title='LTF EMA Period')

// Compute the EMA of Willy
out2 = ta.ema(out, len)

// Add the top and bottom ranges for Willy to be considered
band1 = hline(78.6, 'Upper Band', color=#2F4F4F)
band0 = hline(23.6, 'Lower Band', color=#2F4F4F)

// Add Slow Stochastic Oscillator
lengthK = input(14, title="K Length")
lengthD = input(3, title="D Length")
smoothD = input(3, title="D Smooth")
k = ta.stoch(out2, out2, out2, lengthK)
d = ta.sma(k, lengthD)
slowK = ta.sma(k, smoothD)

// Plot the Slow Stochastic Oscillator
plot(slowK, title="Slow %K", color=color.blue, linewidth=1)
plot(d, title="Slow %D", color= color.orange, linewidth=1)

// Calculate willy HTF
out22 = 100 * (close - lower2) / (upper2 - lower2)  // Change the formula for HTF Willy
src22 = out22
len22 = input.int(618, title='HTF EMA Period')

// Compute the EMA of Willy HTF
out222 = ta.ema(out22, len22)

// Color the background where Willy
fill(hline(100, 'Upper Limit', color.red), band1, out2 > 80 ? color.red : color.black, 'overbought1', transp=70)
fill(band0, hline(0, 'Lower Limit', color.green), out2 < 20 ? color.lime : color.black, 'oversold1', transp=70)

// Color the background where Willy HTF
fill(hline(100, 'Upper Limit', color.black), band1, out222 > 80 ? color.teal : color.black, 'overbought2-HTF', transp=90)
fill(band0, hline(0, 'Lower Limit', color.black), out222 < 20 ? color.fuchsia : color.black, 'oversold2-HTF', transp=90)

// Draw the mid point 
midpoint1 = hline(61.8, title='Midpoint61.8', color=color.yellow, linestyle=hline.style_dotted, linewidth=1)
midpoint = hline(50, title='Midpoint', color= color.red, linestyle=hline.style_dotted, linewidth=2)
midpoint2 = hline(38.2, title='Midpoint38.2', color= color.yellow, linestyle=hline.style_dotted, linewidth=1)

// Smoothed WillyCycle Line settings
smoothed = input(false, title='Smoothed WillyCycle Line?')
smooth_len = input.int(5, minval=1, maxval=20, title='Smooth Length')

// Calculate the smoothed WillyCycle Line
smoothed_willy = smoothed ? ta.ema(out, smooth_len) : out

// Draw the Willy and EMA
plot(smoothed ? ta.ema(out, 10) : out, title='WillyCycle', color=color.new(color.aqua,10), linewidth=2)
plot(out2, title='WillyCycle EMA', color=color.new(color.green, 5), linewidth=3)
plot(out22, title='WillyCycle HTF', color=color.new(color.gray, 1), linewidth=1)
plot(out222, title='WillyCycle EMA HTF', color=color.new(color.maroon, 5), linewidth=3)

// ADX calculation
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
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)
plot(sig, color= color.red, title="ADX")





