OnChart Stochastic
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Shuttle_Club
//@version=5
indicator(title='OnChart Stochastic', overlay=true)
periodK = input.int(26, title='%K Leng', minval=1, inline='s1', group='===== STOCHASTIC OSCILLATOR =====')
smoothK = input.int(5, title='%K', minval=1, inline='s1', group='===== STOCHASTIC OSCILLATOR =====')
periodD = input.int(5, title='%D', minval=1, inline='s1', group='===== STOCHASTIC OSCILLATOR =====', tooltip='Parameters of a standard stochastic oscillator.\n\nПараметры стандартного стохастического осциллятора.')
MAtype = input.string(defval='SMA',title='MA type', options=['SMA','EMA','WMA','RMA'], inline='s2', group='===== MA RANGE FOR STOCHASTIC =====')
MAperiod = input.int(20, title='> >', minval=1, inline='s2', group='===== MA RANGE FOR STOCHASTIC =====')
MAsrc = input.source(close, title='>>', inline='s2', group='===== MA RANGE FOR STOCHASTIC =====', tooltip='Parameters of the middle price line, around which the upper and lower boundaries of the "stochastic" are built.\n\nПараметры средней ценовой линии, вокруг которой выстраивается верхняя и нижняя границы "стохастика".')
ATRperiod = input.int(20, title='ATR Len', minval=1, inline='s3', group='===== MA RANGE FOR STOCHASTIC =====')
up_level = input.int(80, title='UP', minval=1, inline='s3', group='===== MA RANGE FOR STOCHASTIC =====')
dn_level = input.int(20, title='DN', minval=1, inline='s3', group='===== MA RANGE FOR STOCHASTIC =====', tooltip='The period of daily volatility for building the range in which the "stochastic" moves.\n\nПериод дневной волатильности для построения рейнджа, в котором двигается "стохастик".')
showrange = input.bool(true,title='Show range      ', inline='s4', group='===== MA RANGE FOR STOCHASTIC =====')
showcross = input.bool(false,title='Show cross', inline='s4', group='===== MA RANGE FOR STOCHASTIC =====')
Dhigh = request.security(syminfo.tickerid,'D',high[1])
Dlow = request.security(syminfo.tickerid,'D',low[1])
Dclose = request.security(syminfo.tickerid,'D',close[1])
dTR = float(0)
for i = 0 to ATRperiod-1
    dTR := dTR+math.max(math.abs(Dhigh[i]-Dlow[i]),math.max(math.abs(Dhigh[i]-Dclose[i+1]),math.abs(Dlow[i]-Dclose[i+1])))
avgRange = dTR/ATRperiod
float maValue = switch MAtype
	'SMA' => ta.sma(MAsrc,MAperiod)
	'EMA' => ta.ema(MAsrc,MAperiod)
	'WMA' => ta.wma(MAsrc,MAperiod)
	'RMA' => ta.rma(MAsrc,MAperiod)
	=> ta.sma(MAsrc,MAperiod)
stochValue = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
signalValue = ta.sma(stochValue, periodD)
up_line = maValue+(avgRange*(up_level-50)/100)
dn_line = maValue-(avgRange*(50-dn_level)/100)
k_stoch = maValue+(stochValue-50)/100*avgRange
d_stoch = maValue+(signalValue-50)/100*avgRange
plot(showrange?up_line:na,title='High band')
plot(showrange?maValue:na,title='MA_center')
plot(showrange?dn_line:na,title='Low band')
plot(k_stoch,title='%K',color=color.new(#12b71a,0),linewidth=2)
plot(d_stoch,title='%D',color=color.new(#f41410,0),linewidth=2)
up = ta.crossover(k_stoch,d_stoch) and (k_stoch+d_stoch)/2<dn_line
dn = ta.crossunder(k_stoch,d_stoch) and (k_stoch+d_stoch)/2>up_line
plotshape(up and showcross, title='up cross', text='', textcolor=#08d510, style=shape.triangleup, size=size.tiny, location=location.belowbar, color=color.new(#12b71a,0)) 
plotshape(dn and showcross, title='dn cross', text='', textcolor=#da0a0a, style=shape.triangledown, size=size.tiny, location=location.abovebar, color=color.new(#f41410,0)) 
alertcondition(up, "UP cross", "Stochastic up-cross")
alertcondition(dn, "DN cross", "Stochastic dn-cross")
