MTF Stochastic, A version
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version=5
// @ A3Sh

// Multi Time Frame Stochastic based on the formula RSI MTF created by PeterO
// https://www.tradingview.com/script/JwWWwZOD-RSI-MTF-by-PeterO/
// Like the security based Non Repainting MTF scripts, it has a delay compared to the build in version.
// Simple MTF Stoch script that can be used as trend indicator or filter in your scripts.
// Additional functions to plot the Up and Down trend, Buy and Sell Signals and the Overbought and Oversold areas.

indicator(title='MTF Stochastic', shorttitle='MTF Stoch')


// Inputs
periodK = input.int(14,     group='MTF Stochastic Settings', title='K', minval=1)
periodD = input.int(3,      group='MTF Stochastic Settings', title='D', minval=1)
smoothK = input.int(3,      group='MTF Stochastic Settings', title='Smooth', minval=1)
upper   = input.int(80,     group='MTF Stochastic Settings', title='Overbought Limit')
lower   = input.int(20,     group='MTF Stochastic Settings', title='Oversold Limit')
mtf_    = input.int(8,      group='MTF Stochastic Settings', title='Higher TimeFrame Multiplier', tooltip='Multiplies the current timeframe by specified value')
pud     = input.bool(true,  group='Plot Options', title='Plot Up and Down Trends')
poo     = input.bool(true,  group='Plot Options', title='Plot Overbought and Oversold Areas')
pbs     = input.bool(true,  group='Plot Options', title='Plot Buy and Sell Signals')


// MTF Stoch Calcualation
stoch_mtfK(source, mtf, len) =>

    k = ta.sma(ta.stoch(source, high, low, periodK * mtf), smoothK * mtf)
    
stoch_mtfD(source, mtf, len) =>

    k = ta.sma(ta.stoch(source, high, low, periodK * mtf), smoothK * mtf)
    d = ta.sma(k, periodD * mtf)
    
mtfK = stoch_mtfK(close, mtf_, periodK)
mtfD = stoch_mtfD(close, mtf_, periodK)


// Plots
// Stoch up and down trend plots
trendChange = ta.change(mtfD)
trendColor  = trendChange > 0 ? color.new(color.blue,80) : color.new(color.red,80) 
plot(pud ? upper : na, title='Background Up/Down Trends', color=trendColor, style = plot.style_areabr, histbase=lower)

// Stoch K and D line plots
plot(mtfK, title="%K", color=#2962FF)
plot(mtfD, title="%D", color=#FF6D00)

// The dashed horizontal lines and the fill.
h0 = hline(upper, title= 'Upper Band', color=#606060)
h1 = hline(lower, title= 'Lower Band', color=#606060)
fill(h0, h1,      title='Background', color=color.new(#9915FF, 95))

// Overbought and oversold plots
plot1 = plot(mtfK,  title='PlotforFill_1', color= color.new(color.black,100))
plot2 = plot(upper, title='PlotforFill_2', color= color.new(color.black,100))
plot3 = plot(lower, title='PlotforFill_3', color= color.new(color.black,100))
fill(plot1, plot2, title='Oversold Area',   color= poo and mtfK > upper ? color.new(color.red,  50) : na)
fill(plot1, plot3, title='Overbought Area', color= poo and mtfK < lower ? color.new(color.blue, 50) : na)

// Buy and Sell signal plots
crossUp = mtfK[1] < mtfD[1] and mtfK[1] < lower[1] and mtfK > mtfD ? 1 : 0
crossDn = mtfK[1] > mtfD[1] and mtfK[1] > upper[1] and mtfK < mtfD ? 1 : 0
bgcolor(pbs and crossUp ? color.new(color.blue,0) : na, title = 'Buy Signal')
bgcolor(pbs and crossDn ? color.new(color.red ,0) : na, title = 'Sell Signal')





