Stochastic Oversold / Overbought Multi Time Frame on Candle
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MirNader_

//@version=5
indicator(title="Stochastic Oversold / Overbought Multi Time Frame on Candle", overlay = true )

// The user can select the %K Length and %K Smoothing for the Stochastic oscillator calculation.
string Gr1          = "Stochastic Overaly "
// periodK - %K Length
int periodK         = input.int(14                  , "%K Length"       , minval=1 , group = Gr1)
// smoothK - %K Smoothing
int smoothK         = input.int(1                   , "%K Smoothing"    , minval=1 , group = Gr1)

string Gr2          = "Stochastic Time Frame 1️⃣ "
// showtf1 - Boolean to show/hide the first time frame signal
bool showtf1        = input.bool(true               , ""                , inline = "01" , group = Gr2)
// tf1 - First time frame (15 minutes)
string tf1          = input.timeframe('15'          , "Time Frame 1️⃣"   , inline = "01" , group = Gr2)
// colortf1 - Color for the first time frame signal
color colortf1      = input.color(color.yellow    , ""                , inline = "01" , group = Gr2)
// overtf1 - Overbought threshold for the first time frame
float overtf1       = input.float(80                , " K > "           , inline = "02" , group = Gr2)
// undertf1 - Oversold threshold for the first time frame
float undertf1      = input.float(20                , "    or     K < " , inline = "02" , group = Gr2)

string Gr3          = "Stochastic Time Frame 2️⃣ "
// showtf2 - Boolean to show/hide the second time frame signal
bool showtf2        = input.bool(false              , ""                , inline = "01" , group = Gr3)
// tf2 - Second time frame (1 hour)
string tf2          = input.timeframe('60'          , "Time Frame 2️⃣"   , inline = "01" , group = Gr3)
// colortf2 - Color for the second time frame signal
color colortf2      = input.color(color.red       , ""                , inline = "01" , group = Gr3)
// overtf2 - Overbought threshold for the second time frame
float overtf2       = input.float(80                , "K > "            , inline = "02" , group = Gr3)
// undertf2 - Oversold threshold for the second time frame
float undertf2      = input.float(20                , "    or     K < " , inline = "02" , group = Gr3)

string Gr4          = "Stochastic Time Frame 3️⃣ "
// showtf3 - Boolean to show/hide the third time frame signal
bool showtf3        = input.bool(false              , ""                , inline = "01" , group = Gr4)
// tf3 - Third time frame (4 hours)
string tf3          = input.timeframe('240'         , "Time Frame 3️⃣"   , inline = "01" , group = Gr4)
// colortf3 - Color for the third time frame signal
color colortf3      = input.color(color.black     , ""                , inline = "01" , group = Gr4)
// overtf3 - Overbought threshold for the third time frame
float overtf3       = input.float(80                , "K > "            , inline = "02" , group = Gr4)
// undertf3 - Oversold threshold for the third time frame
float undertf3      = input.float(20                , "    or     K < " , inline = "02" , group = Gr4)

// Define the TimeframeFuction to return the stochastic signal for the selected time frame
// request the security data for the selected symbol and time frame, with lookahead to handle bar merging
TimeframFuction(_symbol, _res, _src) => 
    request.security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)

// Calculate the Stochastic %K
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)

// Get the stochastic signal for each of the three timeframes
ktf1 = TimeframFuction(syminfo.tickerid, tf1, k)
ktf2 = TimeframFuction(syminfo.tickerid, tf2, k)
ktf3 = TimeframFuction(syminfo.tickerid, tf3, k)

// Color the bars based on whether the Stochastic %K is above or below the overbought/oversold thresholds for each time frame
// showtf1 - If the first time frame is selected to be shown, color the bars based on the overbought/oversold thresholds for ktf1
barcolor(showtf1 ? ktf1 > overtf1 or ktf1 < undertf1  ? colortf1 : na : na)

// showtf2 - If the second time frame is selected to be shown, color the bars based on the overbought/oversold thresholds for ktf2
barcolor(showtf2 ? ktf2 > overtf2 or ktf2 < undertf2  ? colortf2 : na : na)

// showtf3 - If the third time frame is selected to be shown, color the bars based on the overbought/oversold thresholds for ktf3
barcolor(showtf3 ? ktf3 > overtf3 or ktf3 < undertf3  ? colortf3 : na : na)

//End