All TimeFrame Oscillators
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @author: Engemcsakugyhoztak

//@version=4

study(title="All TimeFrame Oscillators", shorttitle="All Tf Osc")

oscType = input("Stoch", title="Oscillator", options=["Stoch", "SMI", "Rsi", "StochRsi", "WaveTrend", "CCI", "CCIStoch", "Williams Percent Range", "Norm. MACD", "Norm. MACD Hist", "PVT", "MFI", "CMF", "Chande Momentum", "Volume", "CandleValue", "BBWP"], group="Oscillator")

gps = input("Smooth", title="Line type", options=["Smooth", "Step"], group="Oscillator")
f_secureSecurity(_symbol, _res, _src) =>
  (gps == "Step"  ? security(_symbol, _res, _src, lookahead = barmerge.lookahead_on, gaps = barmerge.gaps_off) : security(_symbol, _res, _src, lookahead = barmerge.lookahead_on, gaps = barmerge.gaps_on))
  //security(_symbol, _res, _src, lookahead = barmerge.lookahead_off, gaps = barmerge.gaps_off)
plot_osc = input("Oscillator", title="Plot Oscillator or it's Slope", options=["Oscillator", "Slope"], group="Oscillator")

dot_type = input("Cross Up/Down oversold/overbougt level", title="Print dots when", options=["Cross Up/Down oversold/overbougt level", "Cross os/ob and the one higher TF is about to cross", "Cross above/below middle line"], group="signals")
triangle_type = input("All above/below oversold/overbougt", title="Print triangles when", options=["All Slope Match", "All above/belove middle line", "All above/belove middle line and slope match", "All above/below oversold/overbougt", "Lower TF in order", "Higher TF in order", "4H-1D in order"], group="signals")
triangle_gaps = input("Print only last triangles", title="Print triangles", options=["Print all triangles", "Print only first triangles", "Print only last triangles"], group="signals")

is1 = input(true, title="1m", inline ="1", group="Timeframes to show") 
is5 = input(true,  title="5m", inline ="1", group="Timeframes to show") 
is15 = input(true,  title="15m", inline ="1", group="Timeframes to show") 
is30 = input(true,  title="30m", inline ="2", group="Timeframes to show") 
is1H = input(true,  title="1H", inline ="2", group="Timeframes to show") 
is4H = input(true,  title="4H", inline ="2", group="Timeframes to show") 
//is12H = input(false,  title="12H") 
is1D = input(true,  title="1D", inline ="3", group="Timeframes to show") 
is5D = input(false,  title="5D", inline ="3", group="Timeframes to show") 
is1W = input(true,  title="1W", inline ="3", group="Timeframes to show") 
is1M = input(false,  title="1M", inline ="3", group="Timeframes to show") 


// Stochastic Main Parameters
len = input(14, minval=1, title="Stoch Length", group="Stoch") 
smoothK = input(3, minval=1, title="Stoch Smooth", group="Stoch")
smik = input(10, "Stochastic momentum Index (SMI) Percent K Length", group="Stochastic momentum Index (SMI)")
smid = input(3, "Stochastic momentum Index (SMI) Percent D Length", group="Stochastic momentum Index (SMI)")
ccilen = input(defval = 20, title="CCI Length", minval = 1, group="Comodity channel index (CCI)")
//momlen = input(defval = 10, title="Momentum Length", minval = 1)
CCI_stoch_period = input(28, "CCI Stoch Period", group="Stochastic Comodity channel index (CCIStoch)")
cmf_length = input (20, minval=1, title="Chaikin money flow Length", group="Chaikin money flow (CMF)")
cmo_length = input(title="Chande Momentum Oscillator Length", type=input.integer, defval=9, group="Chande Momentum Oscillator")

/////////////////////
// "Williams Percent Range"  shorttitle="Williams %R"
wpr_length = input(14, title="Williams %R Length", group="Williams %R")
wpr_src = input(close, "Source", group="Williams %R")


/////////////////////
// Macd


//study("Normalized MACD",shorttitle='N MACD')
macd_sma = input(12,title='Fast MA', group="Normalized MACD")
macd_lma = input(26,title='Slow MA', group="Normalized MACD")
macd_tsp = input(9,title='Trigger', group="Normalized MACD")
macd_np = input(50,title='Normalize', group="Normalized MACD")
//macd_type = input(1,minval=1,maxval=3,title="1=Ema, 2=Wma, 3=Sma")

get_macd() =>
    macd_sh = sma(close,macd_sma) //macd_type == 1 ? ema(close,macd_sma)  
//     : macd_type == 2 ? wma(close, macd_sma)
//     : macd_sma(close, macd_sma)
    
    macd_lon=sma(close,macd_lma)  //macd_type == 1 ? ema(close,macd_lma) 
     //: macd_type == 2 ? wma(close, macd_lma)
     //: sma(close, macd_lma)
    
    macd_ratio = min(macd_sh,macd_lon)/max(macd_sh,macd_lon)
    macd_Mac = (iff(macd_sh>macd_lon,2-macd_ratio,macd_ratio)-1)
    macd_MacNorm = ((macd_Mac-lowest(macd_Mac, macd_np)) /(highest(macd_Mac, macd_np)-lowest(macd_Mac, macd_np)+.000001)*2)- 1
    macd_MacNorm2 = iff(macd_np<2,macd_Mac,macd_MacNorm)
    macd_Trigger = wma(macd_MacNorm2, macd_tsp)
    macd_Hist = (macd_MacNorm2-macd_Trigger)
    macd_Hist2 = macd_Hist>1?1:macd_Hist<-1?-1:macd_Hist
    //macd_swap=macd_Hist2>macd_Hist2[1]?color.green:color.red
    //macd_swap2 = macd_docol ? macd_MacNorm2 > macd_MacNorm2[1] ? #0094FF : #FF006E : color.red
    //plot(h?Hist2:na,color=swap,style=columns,title='Hist',histbase=0)
    //plot(MacNorm2,color=swap2,title='MacNorm')
    //plot(dofill?MacNorm2:na,color=MacNorm2>0?green:red,style=columns)
    //plot(Trigger,color=yellow,title='Trigger')
    //hline(0)
    ret = (macd_MacNorm2+1)*50
    ret_h = (macd_Hist2+1)*50
    [ret, ret_h]

///////////////////

/////////////////////
// pvt 

//pvt_maType = input(kEma, title="Smoothing Type", options=[kEma, kSma, kVwma, kWma])
pvt_short = input(3, title="Short Period", minval=1, group="Normalized Price Volume Trend (PVT)")
pvt_long = input(10, title="Long Period", minval=2, group="Normalized Price Volume Trend (PVT)")
pvt_shouldNormalize = input(true, title="Normalize data", group="Normalized Price Volume Trend (PVT)")
pvt_normalizeLookback = input(25, title="Normalization look-back period", group="Normalized Price Volume Trend (PVT)")

// Functionality
smoothpvt(sig, len) =>
    ma = ema(sig, len)

calc_pvt( sig, short, long) =>
    src = sig
    vt = cum(change(src) / src[1] * volume)
    sh = smoothpvt(vt, short)
    lg = smoothpvt(vt, long)
    sh - lg

maxAbs(series, len) =>
    max = float(0)
    for i = 0 to len-1
        max := max(max, abs(series[i]))
    max

// PVT
/////////////// 



///////////////
// Candle Value
cvLen       = input (60, 'Candle Value Length', minval=1, group='Candle Value')
cvMult       = input (300, 'Candle Value multiplier', minval=1, group='Candle Value')
candleValue()=>
    MVC=  vwma((close-open)/(high-low), cvLen)
    MVC := MVC * cvMult + 50
    MVC
// Candle Value
///////////////


//////////////
// WaveTrend
wtShow = true
//wtBuyShow = input(true, title = 'Show Buy dots', type = input.bool)
//wtGoldShow = input(true, title = 'Show Gold dots', type = input.bool)
//wtSellShow = input(true, title = 'Show Sell dots', type = input.bool)
//wtDivShow = input(true, title = 'Show Div. dots', type = input.bool)
//vwapShow = input(true, title = 'Show Fast WT', type = input.bool)
wtChannelLen = input(9, title = 'WT Channel Length', type = input.integer, group="WaveTrend")
wtAverageLen = input(12, title = 'WT Average Length', type = input.integer, group="WaveTrend")
wtMASource = input(hlc3, title = 'WT MA Source', type = input.source, group="WaveTrend")
wtMALen = input(3, title = 'WT MA Length', type = input.integer, group="WaveTrend")

// WaveTrend
f_wavetrend(tfsrc, chlen, avg, malen) =>
    esa = ema(tfsrc, chlen)
    de = ema(abs(tfsrc - esa), chlen)
    ci = (tfsrc - esa) / (0.015 * de)
    wt1 = ema(ci, avg)
    //wt2 = sma(wt1, malen)
    //wtVwap = wt1 - wt2
    //wtOversold = wt2 <= l //osLevel
    //wtOverbought = wt2 >= h //obLevel
    //wtCross = cross(wt1, wt2)
    //wtCrossUp = wt2 - wt1 <= 0
    //wtCrossDown = wt2 - wt1 >= 0
    //wtCrosslast = cross(wt1[2], wt2[2])
    //wtCrossUplast = wt2[2] - wt1[2] <= 0
    //wtCrossDownlast = wt2[2] - wt1[2] >= 0
    //[wt1, wt2, wtOversold, wtOverbought, wtCross, wtCrossUp, wtCrossDown, wtCrosslast, wtCrossUplast, wtCrossDownlast, wtVwap]
    wt1

// WaveTrend
/////////////

// Volume OSC
/////////////
volshortlen = input(5, minval=1, title = "Short Length", group = "Volume Oscillator")
vollonglen = input(10, minval=1, title = "Long Length", group = "Volume Oscillator")
/////////////
// Volume Osc


///////////////
// BBWP

i_priceSrc      = input  ( close,    'Price Source',                                                                         group='Bollinger Band Width Percentile (BBWP)')
i_bbwpLen       = input  ( 13,       'Length',                           minval=1,                                           group='Bollinger Band Width Percentile (BBWP)')
i_basisType     = input  ( 'SMA',    'Basis Type',                       options=['SMA', 'EMA', 'VWMA'],                     group='Bollinger Band Width Percentile (BBWP)')
i_bbwpLkbk      = input  ( 100,      'Lookback',                         minval=1,                                           group='Bollinger Band Width Percentile (BBWP)')


f_bbwp ( _priceSrc, _bbwLen, _type, _lookback ) =>
    _basis = sma(_priceSrc, _bbwLen)
    if _type == "EMA"
        _basis := ema(_priceSrc, _bbwLen)
    if _type == "VWMA"
        _basis := vwma(_priceSrc, _bbwLen)

    _dev = stdev(_priceSrc, _bbwLen)
    _upper = _basis + _dev
    _lower = _basis - _dev
    _bbw = (_upper-_lower)/_basis
    //_dev = stdev ( _priceSrc, _bbwLen )
    //_bbw = ( _basis + _dev - ( _basis - _dev )) / _basis
    
    _bbwSum = 0.0
    _len = bar_index < _lookback ? bar_index : _lookback
    for _i = 1 to _len by 1
        _bbwSum += ( _bbw[_i] > _bbw ? 0 : 1 )
        _bbwSum
    _return = bar_index >= _lookback ? ( _bbwSum / _len) * 100 : na    
    _return
    
// BBWP
///////////////


// Plot Tresholds
h = (plot_osc=="Slope" ? 0 : oscType=="SMI" ?  40 : oscType=="Rsi" ? 70 : oscType=="CCI" ?  100 : oscType=="CMF" ?  20 : oscType=="WaveTrend" ?  60 : oscType=="Volume" ?  20 : oscType == "Chande Momentum" ? 50  : oscType == "Williams Percent Range" ? -20 : 80)
l = (plot_osc=="Slope" ? 0 : oscType=="SMI" ? -40 : oscType=="Rsi" ? 30 : oscType=="CCI" ? -100 : oscType=="CMF" ? -20 : oscType=="WaveTrend" ? -60 : oscType=="Volume" ? -20 : oscType == "Chande Momentum" ? -50 : oscType == "Williams Percent Range" ? -80 : 20)
m = (plot_osc=="Slope" ? 0 : oscType=="Rsi" or oscType=="Norm. MACD Hist" or oscType=="PVT" or oscType=="BBWP" ? 50 : oscType=="SMI" or oscType=="CCI" or oscType=="CMF" or oscType=="WaveTrend" or oscType=="Volume" or oscType == "Chande Momentum" ? 0 : oscType == "Williams Percent Range" ? -50 : 50)

doth = (plot_osc=="Slope" ? 30  : oscType=="SMI" ?  100 : oscType=="Rsi" ? 90 : oscType=="CCI" ?  300 : oscType=="CMF" ?  30 : oscType=="WaveTrend" ?  100 : oscType=="Volume" ?  30 : oscType == "Chande Momentum" ? 80  : oscType == "Williams Percent Range" ?    0 : 100)
dotl = (plot_osc=="Slope" ? -30 : oscType=="SMI" ? -100 : oscType=="Rsi" ? 10 : oscType=="CCI" ? -300 : oscType=="CMF" ? -30 : oscType=="WaveTrend" ? -100 : oscType=="Volume" ? -30 : oscType == "Chande Momentum" ? -80 : oscType == "Williams Percent Range" ? -100 :   0)

span = ( oscType=="CCI" ?  12 : oscType=="WaveTrend" ?  6 : oscType=="CMF" ?  3 : 3)


p1 = hline(plot_osc != "Slope" ? h : na, title='Overbought',  color=color.gray, linestyle=hline.style_dashed, linewidth=1)
p2 = hline(plot_osc != "Slope" ? l : na, title='Oversold', color=color.gray, linestyle=hline.style_dashed, linewidth=1)
p3 = hline(plot_osc != "Slope" ? m : 0, title='Middle', color=color.gray, linestyle=hline.style_dashed, linewidth=1)



// ————— Converts current chart resolution into a float minutes value.
f_resInMinutes() => 
    _resInMinutes = timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)
// ————— Returns the float minutes value of the string _res.
f_tfResInMinutes(_res) =>
    // _res: resolution of any TF (in "timeframe.period" string format).
    // Dependency: f_resInMinutes().
    security(syminfo.tickerid, _res, f_resInMinutes())

// —————————— Determine if current timeframe is smaller that higher timeframe selected in Inputs.
currentTfInMinutes = f_resInMinutes()
// Compare current TF to higher TF to make sure it is smaller, otherwise our plots don't make sense.
//chartOnLowerTf = currentTfInMinutes < higherTfInMinutes



///////////////////////////
// calculate the oscillator

osc(t, len) =>
    k = sma(stoch(close, high, low, len), smoothK)         // Stoch
    if t == "Stoch"            // Stock
        k := sma(stoch(close, high, low, len), smoothK)
    if t == "SMI"
        // Range Calculation
        ll = lowest (low, smik)
        hh = highest (high, smik)
        diff = hh - ll
        rdiff = close - (hh+ll)/2
        avgrel = ema(ema(rdiff,smid),smid)
        avgdiff = ema(ema(diff,smid),smid)
        // SMI calculations
        SMI = avgdiff != 0 ? (avgrel/(avgdiff/2)*100) : 0
        k := ema(SMI,smid)    
    if t == "Rsi"            // RSI
        k := rsi(close, len)
    if t == "StochRsi"            // stochRsi
        k := sma(stoch(rsi(close, len), rsi(close, len), rsi(close, len), len), smoothK)
    if t == "CCI"
        k := cci(close, ccilen)
    if t == "CCIStoch"
        cci = cci(close, ccilen)
        stoch_cci_k = sma(stoch(cci, cci, cci, CCI_stoch_period), smoothK)
        stoch_cci_d = sma(stoch_cci_k, smoothK)
        k := stoch_cci_d
    if t == "Williams Percent Range"
    	max = highest(wpr_length)
    	min = lowest(wpr_length)
    	k := 100 * (wpr_src - max) / (max - min)
    if t == "Norm. MACD" or t == "Norm. MACD Hist"
        [macd, macd_hist] = get_macd()
        if t == "Norm. MACD"
            k := macd
        else
            k := macd_hist
    if t == "PVT"
        raw_pvt = calc_pvt( close, pvt_short, pvt_long)
        maxAbs_pvt = maxAbs(raw_pvt, pvt_normalizeLookback)
        range_pvt = 1.0 // range to fit values into
        k := (iff(pvt_shouldNormalize, (raw_pvt / maxAbs_pvt * range_pvt), raw_pvt) +1) * 50
    if t == "MFI"
        k := mfi(close, len)
    if t == "CMF"
        //"Chaikin Money Flow"
        var cumVol = 0.
        cumVol += nz(volume)
        //if barstate.islast and cumVol == 0 runtime.error("No volume is provided by the data vendor.")
        ad = close==high and close==low or high==low ? 0 : ((2*close-low-high)/(high-low))*volume
        k := 100 * sum(ad, cmf_length) / sum(volume, cmf_length)
    if t == "Chande Momentum"
        mom = change(close)
        upSum = sum(max(mom, 0), cmo_length)
        downSum = sum(-min(mom, 0), cmo_length)
        k := 100 * (upSum - downSum) / (upSum + downSum)
    if t == "Volume"
        var cumVol = 0.
        cumVol += nz(volume)
        short = ema(volume, volshortlen)
        long = ema(volume, vollonglen)
        k := 100 * (short - long) / long
    if t == "CandleValue"
        k := candleValue()
    if t == "WaveTrend"
        //[wt1, wt2, wtOversold, wtOverbought, wtCross, wtCrossUp, wtCrossDown, wtCross_last, wtCrossUp_last, wtCrossDown_last, wtVwap] = f_wavetrend(wtMASource, wtChannelLen, wtAverageLen, wtMALen)
        wt1 = f_wavetrend(wtMASource, wtChannelLen, wtAverageLen, wtMALen)
        k:= wt1
    if t == "BBWP"
        k := f_bbwp ( i_priceSrc, i_bbwpLen, i_basisType, i_bbwpLkbk )
    slope = k - k[1]
    [k, slope]


[k, t] = osc(oscType, len)

outK1 = is1   ? f_secureSecurity(syminfo.tickerid, tostring(1), k) : na
t1 = is1   ?  fixnan(f_secureSecurity(syminfo.tickerid, tostring(1), t)) : na
outK2 = is5   ? f_secureSecurity(syminfo.tickerid, tostring(5), k) : na
t2 = is5   ? fixnan(f_secureSecurity(syminfo.tickerid, tostring(5), t)) : na
outK3 = is15  ? f_secureSecurity(syminfo.tickerid, tostring(15), k) : na
t3 = is15  ? fixnan(f_secureSecurity(syminfo.tickerid, tostring(15), t)) : na
outK4 = is30  ? f_secureSecurity(syminfo.tickerid, tostring(30), k) : na
t4 = is30  ? fixnan(f_secureSecurity(syminfo.tickerid, tostring(30), t)) : na
outK5 = is1H  ? f_secureSecurity(syminfo.tickerid, tostring(60), k) : na
t5 = is1H  ? fixnan(f_secureSecurity(syminfo.tickerid, tostring(60), t)) : na
outK6 = is4H  ? f_secureSecurity(syminfo.tickerid, tostring(240), k) : na
t6 = is4H  ? fixnan(f_secureSecurity(syminfo.tickerid, tostring(240), t)) : na
//outK12H = is12H  ? f_secureSecurity(syminfo.tickerid, tostring(720), k) : na
//t12H = is12H  ? fixnan(f_secureSecurity(syminfo.tickerid, tostring(720), t)) : na
outK7 = is1D  ? f_secureSecurity(syminfo.tickerid, tostring(1440), k) : na
t7 = is1D  ? fixnan(f_secureSecurity(syminfo.tickerid, tostring(1440), t)) : na
outK8 = is1W  ? f_secureSecurity(syminfo.tickerid, 'W', k) : na
t8 = is1W  ? fixnan(f_secureSecurity(syminfo.tickerid, 'W', t)) : na
outK9 = is1M  ? f_secureSecurity(syminfo.tickerid, 'M', k) : na
t9 = is1M  ? fixnan(f_secureSecurity(syminfo.tickerid, 'M', t)) : na
outK10 = is5D ? f_secureSecurity(syminfo.tickerid, '5D', k) : na
t10 = is5D ? fixnan(f_secureSecurity(syminfo.tickerid, '5D', t)) : na


c1m=color.new(color.teal, transp=60)
c5m=color.new(color.aqua, transp=50)
c15m=color.new(color.rgb(165, 214, 167), transp=40)
c30m=color.new(color.yellow, transp=30)
c60m=color.new(color.white, transp=20)
c240m=color.new(color.red, transp=10)
//c720m=color.new(color.blue, transp=10)
c1440m=color.new(color.green, transp=0)
c7200m=color.new(color.olive, transp=10)
c1W=color.new(color.orange, transp=20)
c1M=color.new(color.maroon, transp=20)


plot(plot_osc != "Slope" and currentTfInMinutes<=1?     outK1 : na, title= "1 min",    style=plot.style_line, linewidth=2, color=c1m)
plot(plot_osc != "Slope" and currentTfInMinutes<=5?     outK2 : na, title= "5 min",    style=plot.style_line, linewidth=2, color=c5m)
plot(plot_osc != "Slope" and currentTfInMinutes<=15?    outK3 : na, title= "15 min",   style=plot.style_line, linewidth=2, color=c15m)
plot(plot_osc != "Slope" and currentTfInMinutes<=30?    outK4 : na, title= "30 min",   style=plot.style_line, linewidth=2, color=c30m)
plot(plot_osc != "Slope" and currentTfInMinutes<=60?    outK5 : na, title= "1 H",      style=plot.style_line, linewidth=2, color=c60m)
plot(plot_osc != "Slope" and currentTfInMinutes<=240?   outK6 : na, title= "4 H",      style=plot.style_line, linewidth=2, color=c240m)
//plot(plot_osc != "Slope" and currentTfInMinutes<=720? outK12H : na, title= "12 H",     style=plot.style_line, linewidth=2, color=c720m)
plot(plot_osc != "Slope" and currentTfInMinutes<=1440?  outK7 : na, title= "1 D",      style=plot.style_line, linewidth=2, color=c1440m)
plot(plot_osc != "Slope" and currentTfInMinutes<=7200?  outK10 : na, title= "5 D",      style=plot.style_line, linewidth=2, color=c7200m)
plot(plot_osc != "Slope" and currentTfInMinutes<=10080? outK8 : na, title= "1 W",      style=plot.style_line, linewidth=2, color=c1W)
plot(plot_osc != "Slope" and currentTfInMinutes<=45000? outK9 : na, title= "1 M",      style=plot.style_line, linewidth=2, color=c1M)



// plot trends
plot(plot_osc == "Slope" and currentTfInMinutes<=1?     t1 : na, title= "1 min",  style=plot.style_line, linewidth=2, color=c1m)
plot(plot_osc == "Slope" and currentTfInMinutes<=5?     t2 : na, title= "5 min",  style=plot.style_line, linewidth=2, color=c5m)
plot(plot_osc == "Slope" and currentTfInMinutes<=15?    t3 : na, title= "15 min", style=plot.style_line, linewidth=2, color=c15m)
plot(plot_osc == "Slope" and currentTfInMinutes<=30?    t4 : na, title= "30 min", style=plot.style_line, linewidth=2, color=c30m)
plot(plot_osc == "Slope" and currentTfInMinutes<=60?    t5 : na, title= "1 H",    style=plot.style_line, linewidth=2, color=c60m)
plot(plot_osc == "Slope" and currentTfInMinutes<=240?   t6 : na, title= "4 H",    style=plot.style_line, linewidth=2, color=c240m)
//plot(plot_osc == "Slope" and currentTfInMinutes<=720? t12H : na, title= "12 H",   style=plot.style_line, linewidth=2, color=c720m)
plot(plot_osc == "Slope" and currentTfInMinutes<=1440?  t7 : na, title= "1 D",    style=plot.style_line, linewidth=2, color=c1440m)
plot(plot_osc == "Slope" and currentTfInMinutes<=7200?  t10 : na, title= "5 D",    style=plot.style_line, linewidth=2, color=c7200m)
plot(plot_osc == "Slope" and currentTfInMinutes<=10080? t8 : na, title= "1 W",    style=plot.style_line, linewidth=2, color=c1W)
plot(plot_osc == "Slope" and currentTfInMinutes<=45000? t9 : na, title= "1 M",    style=plot.style_line, linewidth=2, color=c1M)


lowercond(_osc, _line, _hTfOsc, _htfSlope) =>
    c = crossover(_osc, _line)
    if dot_type == "Cross os/ob and the one higher TF is about to cross"
        c := crossover(_osc, _line) and fixnan(_hTfOsc) < _line and _htfSlope >=0
    if dot_type == "Cross above/below middle line"
        c := crossover(_osc, m)
    c

uppercond(_osc, _line, _hTfOsc, _htfSlope) =>
    c = crossunder(_osc, _line)
    if dot_type == "Cross os/ob and the one hiegher TF is about to cross"
        c := crossunder(_osc, _line) and fixnan(_hTfOsc) > _line and _htfSlope <=0
    if dot_type == "Cross above/below middle line"
        c := crossunder(_osc, m)
    c


shapeup = shape.circle
shapedown = shape.circle

dot1l = currentTfInMinutes<=1 and lowercond(outK1, l, outK2, t2)
dot1u = currentTfInMinutes<=1 and uppercond(outK1, h, outK2, t2)
dot5l = currentTfInMinutes<=5 and lowercond(outK2, l, outK3, t3)
dot5u = currentTfInMinutes<=5 and uppercond(outK2, h, outK3, t3)
dot15l = currentTfInMinutes<=15 and lowercond(outK3, l, outK4, t4)
dot15u = currentTfInMinutes<=15 and uppercond(outK3, h, outK4, t4)
dot30l = currentTfInMinutes<=30 and lowercond(outK4, l, outK5, t5)
dot30u = currentTfInMinutes<=30 and uppercond(outK4, h, outK5, t5)
dot1Hl = currentTfInMinutes<=60 and lowercond(outK5, l, outK6, t6)
dot1Hu = currentTfInMinutes<=60 and uppercond(outK5, h, outK6, t6)
//dot4Hl = currentTfInMinutes<=240 and lowercond(outK6, l, outK12H, t12H)
//dot4Hu = currentTfInMinutes<=240 and uppercond(outK6, h, outK12H, t12H)
dot4Hl = currentTfInMinutes<=240 and lowercond(outK6, l, outK7, t7)
dot4Hu = currentTfInMinutes<=240 and uppercond(outK6, h, outK7, t7)
//dot12Hl = currentTfInMinutes<=720 and lowercond(outK12H, l, outK7, t7)
//dot12Hu = currentTfInMinutes<=720 and uppercond(outK12H, h, outK7, t7)
dot1Dl = currentTfInMinutes<=1440 and lowercond(outK7, l, outK10, t10)
dot1Du = currentTfInMinutes<=1440 and uppercond(outK7, h, outK10, t10)
dot5Dl = currentTfInMinutes<=1440 and lowercond(outK10, l, outK8, t8)
dot5Du = currentTfInMinutes<=1440 and uppercond(outK10, h, outK8, t8)
dot1Wl = currentTfInMinutes<=10080 and lowercond(outK8, l, outK9, t9)
dot1Wu = currentTfInMinutes<=10080 and uppercond(outK8, h, outK9, t9)
dot1Ml = currentTfInMinutes<=45000 and lowercond(outK9, l, outK9, t9)
dot1Mu = currentTfInMinutes<=45000 and uppercond(outK9, h, outK9, t9)


plotshape(plot_osc != "Slope" and dot1l   ?  dotl-span   : na, title="Buy", style=shapeup, color=c1m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot1u   ?  doth+span   : na, title='Sell', style=shapedown, color=c1m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot5l   ?  dotl-2*span : na, title="Buy", style=shapeup, color=c5m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot5u   ?  doth+2*span : na, title='Sell', style=shapedown, color=c5m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot15l  ?  dotl-3*span : na, title="Buy", style=shapeup, color=c15m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot15u  ?  doth+3*span : na, title='Sell', style=shapedown, color=c15m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot30l  ?  dotl-4*span : na, title="Buy", style=shapeup, color=c30m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot30u  ?  doth+4*span : na, title='Sell', style=shapedown, color=c30m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot1Hl  ?  dotl-5*span : na, title="Buy", style=shapeup, color=c60m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot1Hu  ?  doth+5*span : na, title='Sell', style=shapedown, color=c60m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot4Hl  ?  dotl-6*span : na, title="Buy", style=shapeup, color=c240m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot4Hu  ?  doth+6*span : na, title='Sell', style=shapedown, color=c240m, location=location.absolute, size=size.auto)
//plotshape(plot_osc != "Slope" and dot12Hl ?  dotl-7*span : na, title="Buy", style=shapeup, color=c720m, location=location.absolute, size=size.auto)
//plotshape(plot_osc != "Slope" and dot12Hu ?  doth+7*span : na, title='Sell', style=shapedown, color=c720m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot1Dl  ?  dotl-8*span : na, title="Buy", style=shapeup, color=c1440m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot1Du  ?  doth+8*span : na, title='Sell', style=shapedown, color=c1440m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot5Dl  ?  dotl-9*span : na, title="Buy", style=shapeup, color=c7200m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot5Du  ?  doth+9*span : na, title='Sell', style=shapedown, color=c7200m, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot1Wl  ?  dotl-10*span : na, title="Buy", style=shapeup, color=c1W, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot1Wu  ?  doth+10*span : na, title='Sell', style=shapedown, color=c1W, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot1Ml  ? dotl-11*span : na, title="Buy", style=shapeup, color=c1M, location=location.absolute, size=size.auto)
plotshape(plot_osc != "Slope" and dot1Mu  ? doth+11*span : na, title='Sell', style=shapedown, color=c1M, location=location.absolute, size=size.auto)


//alertcondition( ((dot1l or dot1u or dot5l or dot5u or dot15l or dot15u or dot30l or dot30u or dot1Hl or dot1Hu or dot4Hl or dot4Hu or dot12Hl or dot12Hu or dot1Dl or dot1Du or dot5Dl or dot5Du or dot1Wl or dot1Wu or dot1Ml or dot1Mu)? true:na), title="Dot alert", message="Dot on the MTF Stoch!")
alertcondition( ((dot1l or dot1u or dot5l or dot5u or dot15l or dot15u or dot30l or dot30u or dot1Hl or dot1Hu or dot4Hl or dot4Hu or dot1Dl or dot1Du or dot5Dl or dot5Du or dot1Wl or dot1Wu or dot1Ml or dot1Mu)? true:na), title="Dot alert", message="Dot on the MTF Stoch!")


//4H 1D crosses
//plotshape(crossover(fixnan(outK6), fixnan(outK7)) and fixnan(t7)>0 and fixnan(outK7)<50 ?    0 : na, title="Buy", style=shape.triangleup, color=c1440m, location=location.absolute, size=size.tiny)
//plotshape(crossunder(fixnan(outK6), fixnan(outK7)) and fixnan(t7)<0 and fixnan(outK7)>50? 100 : na, title="Sell", style=shape.triangledown, color=c1440m, location=location.absolute, size=size.tiny)
//1D 1W crosses
//plotshape(crossover(fixnan(outK7), fixnan(outK8)) and fixnan(t8)>0 and fixnan(outK8)<50 ?    0 : na, title="Buy", style=shape.triangleup, color=c1W, location=location.absolute, size=size.tiny)
//plotshape(crossunder(fixnan(outK7), fixnan(outK8)) and fixnan(t8)<0 and fixnan(outK8)>50? 100 : na, title="Sell", style=shape.triangledown, color=c1W, location=location.absolute, size=size.tiny)
//1W 1M crosses
//plotshape(crossover(fixnan(outK8), fixnan(outK9)) and fixnan(t9)>0 and fixnan(outK9)<50 ?    0 : na, title="Buy", style=shape.triangleup, color=c1M, location=location.absolute, size=size.tiny)
//plotshape(crossunder(fixnan(outK8), fixnan(outK9)) and fixnan(t9)<0 and fixnan(outK9)>50? 100 : na, title="Sell", style=shape.triangledown, color=c1M, location=location.absolute, size=size.tiny)
 




alert_cond = ((currentTfInMinutes<=1 and is1 ? t1>=0 : true ) and 
 (currentTfInMinutes<=5 and is5 ? t2>=0 : true ) and 
 (currentTfInMinutes<=15 and is15 ? t3>=0 : true ) and 
 (currentTfInMinutes<=30 and is30 ? t4>=0 : true ) and 
 (currentTfInMinutes<=60 and is1H ? t5>=0 : true ) and 
 (currentTfInMinutes<=240 and is4H ? t6>=0 : true ) and 
// (currentTfInMinutes<=720 and is12H ? t12H>=0 : true ) and 
 (currentTfInMinutes<=1440 and is1D ? t7>=0 : true ) and 
 (currentTfInMinutes<=7200 and is5D ? t10>=0 : true ) and 
 (currentTfInMinutes<=10080 and is1W ? t8>=0 : true )  and 
 (currentTfInMinutes<=45000 and is1M ? t9>=0 : true ) ? "green" : ( (currentTfInMinutes<1 and is1 ? t1<=0 : true ) and 
 (currentTfInMinutes<=5 and is5 ? t2<=0 : true ) and 
 (currentTfInMinutes<=15 and is15 ? t3<=0 : true ) and 
 (currentTfInMinutes<=30 and is30 ? t4<=0 : true ) and 
 (currentTfInMinutes<=60 and is1H ? t5<=0 : true ) and 
 (currentTfInMinutes<=240 and is4H ? t6<=0 : true ) and 
// (currentTfInMinutes<=720 and is12H ? t12H<=0 : true ) and 
 (currentTfInMinutes<=1440 and is1D ? t7<=0 : true ) and
 (currentTfInMinutes<=7200 and is5D ? t10<=0 : true ) and
 (currentTfInMinutes<=10080 and is1W ? t8<=0 : true ) and
 (currentTfInMinutes<=45000 and is1M ? t9<=0 : true ) ? "red" : na))


alltriangles = triangle_type == "All Slope Match" ? true : false
m_chk = triangle_type == "All above/belove middle line and slope match" or triangle_type == "All above/belove middle line" ? true : false
hl_chk = triangle_type == "All above/below oversold/overbougt" ? true : false
noslope_chk = triangle_type == "All above/belove middle line" or triangle_type == "All above/below oversold/overbougt" ? true : false

triangles = ""
if triangle_type != "Lower TF in order" and triangle_type != "Higher TF in order" and triangle_type != "4H-1D in order"
    triangles := (
     (currentTfInMinutes<=1 and is1 ? (noslope_chk or t1>=0) and (alltriangles or (m_chk and fixnan(outK1)>=m) or (hl_chk and fixnan(outK1) <= l)) : true ) and 
     (currentTfInMinutes<=5 and is5 ? (noslope_chk or t2>=0) and (alltriangles or (m_chk and fixnan(outK2)>=m) or (hl_chk and fixnan(outK2) <= l)) : true ) and 
     (currentTfInMinutes<=15 and is15 ? (noslope_chk or t3>=0) and (alltriangles or (m_chk and fixnan(outK3)>=m) or (hl_chk and fixnan(outK3) <= l)) : true ) and 
     (currentTfInMinutes<=30 and is30 ? (noslope_chk or t4>=0) and (alltriangles or (m_chk and fixnan(outK4)>=m) or (hl_chk and fixnan(outK4) <= l)) : true ) and 
     (currentTfInMinutes<=60 and is1H ? (noslope_chk or t5>=0) and (alltriangles or (m_chk and fixnan(outK5)>=m) or (hl_chk and fixnan(outK5) <= l)) : true ) and 
     (currentTfInMinutes<=240 and is4H ? (noslope_chk or t6>=0) and (alltriangles or (m_chk and fixnan(outK6)>=m) or (hl_chk and fixnan(outK6) <= l)) : true ) and 
//     (currentTfInMinutes<=720 and is12H ? (noslope_chk or t12H>=0) and (alltriangles or (m_chk and fixnan(outK12H)>=m) or (hl_chk and fixnan(outK12H) <= l)) : true ) and 
     (currentTfInMinutes<=1440 and is1D ? (noslope_chk or t7>=0) and (alltriangles or (m_chk and fixnan(outK7)>=m) or (hl_chk and fixnan(outK7) <= l)) : true ) and 
     (currentTfInMinutes<=7200 and is5D ? (noslope_chk or t10>=0) and (alltriangles or (m_chk and fixnan(outK10)>=m) or (hl_chk and fixnan(outK10) <= l)) : true ) and 
     (currentTfInMinutes<=10080 and is1W ? (noslope_chk or t8>=0) and (alltriangles or (m_chk and fixnan(outK8)>=m) or (hl_chk and fixnan(outK8) <= l)) : true )  and 
     (currentTfInMinutes<=45000 and is1M ? (noslope_chk or t9>=0) and (alltriangles or (m_chk and fixnan(outK9)>=m) or (hl_chk and fixnan(outK9) <= l)) : true ) ? "green" : ( 
     (currentTfInMinutes<=1 and is1 ? (noslope_chk or t1<=0) and (alltriangles or (m_chk and fixnan(outK1)<=m) or (hl_chk and fixnan(outK1) >= h)) : true ) and 
     (currentTfInMinutes<=5 and is5 ? (noslope_chk or t2<=0) and (alltriangles or (m_chk and fixnan(outK2)<=m) or (hl_chk and fixnan(outK2) >= h)) : true ) and 
     (currentTfInMinutes<=15 and is15 ? (noslope_chk or t3<=0) and (alltriangles or (m_chk and fixnan(outK3)<=m) or (hl_chk and fixnan(outK3) >= h)) : true ) and 
     (currentTfInMinutes<=30 and is30 ? (noslope_chk or t4<=0) and (alltriangles or (m_chk and fixnan(outK4)<=m) or (hl_chk and fixnan(outK4) >= h)) : true ) and 
     (currentTfInMinutes<=60 and is1H ? (noslope_chk or t5<=0) and (alltriangles or (m_chk and fixnan(outK5)<=m) or (hl_chk and fixnan(outK5) >= h)) : true ) and 
     (currentTfInMinutes<=240 and is4H ? (noslope_chk or t6<=0) and (alltriangles or (m_chk and fixnan(outK6)<=m) or (hl_chk and fixnan(outK6) >= h)) : true ) and 
//     (currentTfInMinutes<=720 and is12H ? (noslope_chk or t12H<=0) and (alltriangles or (m_chk and fixnan(outK12H)<=m) or (hl_chk and fixnan(outK12H) >= h)) : true ) and 
     (currentTfInMinutes<=1440 and is1D ? (noslope_chk or t7<=0) and (alltriangles or (m_chk and fixnan(outK7)<=m) or (hl_chk and fixnan(outK7) >= h)) : true ) and
     (currentTfInMinutes<=7200 and is5D ? (noslope_chk or t10<=0) and (alltriangles or (m_chk and fixnan(outK10)<=m) or (hl_chk and fixnan(outK10) >= h)) : true ) and
     (currentTfInMinutes<=10080 and is1W ? (noslope_chk or t8<=0) and (alltriangles or (m_chk and fixnan(outK8)<=m) or (hl_chk and fixnan(outK8) >= h)) : true ) and
     (currentTfInMinutes<=45000 and is1M ? (noslope_chk or t9<=0) and (alltriangles or (m_chk and fixnan(outK9)<=m) or (hl_chk and fixnan(outK9) >= h)) : true ) ? "red" : na))

if triangle_type == "Lower TF in order" or triangle_type == "Higher TF in order" or triangle_type == "4H-1D in order"
    all_up = false
    all_down = false

    if triangle_type == "4H-1D in order"
        all_up := fixnan(outK6)>fixnan(outK7)
        all_down := fixnan(outK6)<fixnan(outK7)
    if triangle_type == "Higher TF in order"
        all_up := fixnan(outK6)>fixnan(outK7) and fixnan(outK7)>fixnan(outK8) // and fixnan(outK5)>fixnan(outK6) 
        all_down := fixnan(outK6)<fixnan(outK7) and fixnan(outK7)<fixnan(outK8) //and fixnan(outK5)<fixnan(outK6) 
    if triangle_type == "Lower TF in order"
        all_up := fixnan(outK2)>fixnan(outK3) and fixnan(outK3)>fixnan(outK4) and fixnan(outK4)>fixnan(outK5)
        all_down := fixnan(outK2)<fixnan(outK3) and fixnan(outK3)<fixnan(outK4) and fixnan(outK4)<fixnan(outK5)
    var on_up = false
    var on_down = false
    var last_sign = ""
    if triangle_gaps == "Print all triangles"
        on_up := all_up
        on_down := all_down
    if triangle_gaps == "Print only first triangles"
        on_up := all_up and last_sign != "up" // or (on_up and not all_up)
        on_down := all_down and last_sign != "down"// or (on_down and not all_down)
    if triangle_gaps == "Print only last triangles"
        on_up := all_up and all_up[1] == false
        on_down := all_down and all_down[1] == false
    if all_up
        last_sign := "up"
    if all_down
        last_sign := "down"
    triangles := ( on_up ) ? "green" : ( on_down) ? "red" : na

buy = triangles == "green" ? true : false
sell = triangles == "red" ? true : false

if triangle_gaps == "Print only first triangles"
    prevbuy=0
    prevsell=0
    for i = 0 to 1000
        if triangles[i]  == "green"
            prevbuy:=5
            break
        if triangles[i]  == "red"
            prevsell:=5
            break
        buy := crossover(prevbuy, prevsell)
        sell := crossunder(prevbuy, prevsell)

if triangle_gaps == "Print only last triangles"
    buy := triangles[1] == "green" and triangles == na
    sell := triangles[1] == "red" and triangles == na
    
plotshape (dotl, color=(buy ? color.green : na), style=shape.triangleup, location=location.absolute, size=size.small)
plotshape (doth, color=(sell ? color.red : na), style=shape.triangledown, location=location.absolute, size=size.small)

//plotshape (50, color=(triangles=="green" and triangles[1]!="green" ? color.green : ( triangles=="red" and triangles[1]!="red" ? color.red : color.new(color.black, 100))), style=shape., location=location.absolute, size=size.tiny)
plotshape (dotl, color=(triangles=="green" and triangles[1]!="green" ? color.green : na), style=shape.triangleup, location=location.absolute, size=size.tiny)
plotshape (doth, color=(triangles=="red" and triangles[1]!="red" ? color.red : na), style=shape.triangledown, location=location.absolute, size=size.tiny)

alertcondition( (buy or sell), title="Triangle alert", message="Triangle Signal!\n{{time}}\n{{exchange}}:{{ticker}}, price = {{close}}")


Round( _val, _decimals) => 
    // Rounds _val to _decimals places.
    _p = pow(10,_decimals)
    round(abs(_val)*_p)/_p*sign(_val)


Dark_Red = color.new(#8B0000, 30)	
Red = color.new(#FF0000, 30)
Dark_Green = color.new(#006400, 30)	
Green = color.new(#008000, 30)

//blue_ob = color.new(#87CEFA, 30)
blue_bull = color.new(#00BFFF, 30)
blue_bear = color.new(#007FFF, 30)
//blue os = color.new(#0000FF, 30)
gray = color.new(color.gray, 0)


f_val_color(_val) =>
  //(_val < l ? color.new(color.green, 30) : _val > h ? color.new(color.red, 30) : color.black )
  (_val < l ? Dark_Green : _val >=l and _val < m ? blue_bear : _val >= m and _val < h ? blue_bull : _val >= h ? Dark_Red : gray)


f_slope_color(_s) =>
  (Round(_s, 0) > 0 ? Green : Round(_s, 0) < 0 ? Red : color.black )
  

VC_1m         = f_val_color(fixnan(outK1))
VC_5m         = f_val_color(fixnan(outK2))
VC_15m        = f_val_color(fixnan(outK3))
VC_30m        = f_val_color(fixnan(outK4))
VC_1H         = f_val_color(fixnan(outK5))
VC_4H         = f_val_color(fixnan(outK6))
//VC_12H        = f_val_color(fixnan(outK12H))
VC_D          = f_val_color(fixnan(outK7))
VC_5D         = f_val_color(fixnan(outK10))
VC_W          = f_val_color(fixnan(outK8))
VC_M          = f_val_color(fixnan(outK9))

SC_1m         = f_slope_color(fixnan(t1))
SC_5m         = f_slope_color(fixnan(t2))
SC_15m        = f_slope_color(fixnan(t3))
SC_30m        = f_slope_color(fixnan(t4))
SC_1H         = f_slope_color(fixnan(t5))
SC_4H         = f_slope_color(fixnan(t6))
//SC_12H        = f_slope_color(fixnan(t12H))
SC_D          = f_slope_color(fixnan(t7))
SC_5D         = f_slope_color(fixnan(t10))
SC_W          = f_slope_color(fixnan(t8))
SC_M          = f_slope_color(fixnan(t9))

tableYposInput  = input ("middle", "Panel position", options = ["top", "middle", "bottom"], group = "info panel")
tableXposInput  = input ("left", "", options = ["left", "center", "right"])


var table TTM   = table.new( tableYposInput + "_" + tableXposInput, 30, 12, border_width = 0, frame_width=0 )

THC             = input (color.new(#000000, 0), "Table Header Color")
TC              = input (color.new(color.white, 0), "Table Text Color")
TS              = size.tiny //, "Table Text Size", options = [size.tiny, size.small, size.normal, size.large])


table.cell(TTM, 0, 0, oscType, text_color = THC, bgcolor = gray, text_size = size.small)
table.cell(TTM, 1, 0, "Value", text_color = THC, bgcolor = gray, text_size = size.small)
table.cell(TTM, 2, 0, "Slope", text_color = THC, bgcolor = gray, text_size = size.small)


if (currentTfInMinutes<=1 and is1)
    table.cell(TTM, 0, 1, "1m", text_color = THC, bgcolor = c1m, text_size = TS)
    table.cell(TTM, 1, 1, tostring(Round(fixnan(outK1), 0)), text_color = TC, bgcolor = VC_1m, text_size = TS)
    table.cell(TTM, 2, 1, tostring(Round(fixnan(t1), 2)), text_color = TC, bgcolor = SC_1m, text_size = TS)

if (currentTfInMinutes<=5 and is5)
    table.cell(TTM, 0, 2, "5m", text_color = THC, bgcolor = c5m, text_size = TS)
    table.cell(TTM, 1, 2, tostring(Round(fixnan(outK2), 0)), text_color = TC, bgcolor = VC_5m, text_size = TS)
    table.cell(TTM, 2, 2, tostring(Round(fixnan(t2), 2)), text_color = TC, bgcolor = SC_5m, text_size = TS)

if (currentTfInMinutes<=15 and is15)
    table.cell(TTM, 0, 3, "15m", text_color = THC, bgcolor = c15m, text_size = TS)
    table.cell(TTM, 1, 3, tostring(Round(fixnan(outK3), 0)), text_color = TC, bgcolor = VC_15m, text_size = TS)
    table.cell(TTM, 2, 3, tostring(Round(fixnan(t3), 2)), text_color = TC, bgcolor = SC_15m, text_size = TS)

if (currentTfInMinutes<=30 and is30)
    table.cell(TTM, 0, 4, "30m", text_color = THC, bgcolor = c30m, text_size = TS)
    table.cell(TTM, 1, 4, tostring(Round(fixnan(outK4), 0)), text_color = TC, bgcolor = VC_30m, text_size = TS)
    table.cell(TTM, 2, 4, tostring(Round(fixnan(t4), 2)), text_color = TC, bgcolor = SC_30m, text_size = TS)

if (currentTfInMinutes<=60 and is1H)
    table.cell(TTM, 0, 5, "1H", text_color = THC, bgcolor = c60m, text_size = TS)
    table.cell(TTM, 1, 5, tostring(Round(fixnan(outK5), 0)), text_color = TC, bgcolor = VC_1H, text_size = TS)
    table.cell(TTM, 2, 5, tostring(Round(fixnan(t5), 2)), text_color = TC, bgcolor = SC_1H, text_size = TS)

if (currentTfInMinutes<=240 and is4H)
    table.cell(TTM, 0, 6, "4H", text_color = THC, bgcolor = c240m, text_size = TS)
    table.cell(TTM, 1, 6, tostring(Round(fixnan(outK6), 0)), text_color = TC, bgcolor = VC_4H, text_size = TS)
    table.cell(TTM, 2, 6, tostring(Round(fixnan(t6), 2)), text_color = TC, bgcolor = SC_4H, text_size = TS)

//if (currentTfInMinutes<=720 and is12H)
//    table.cell(TTM, 0, 7, "12H", text_color = THC, bgcolor = c720m, text_size = TS)
//    table.cell(TTM, 1, 7, tostring(Round(fixnan(outK12H), 0)), text_color = TC, bgcolor = VC_12H, text_size = TS)
//    table.cell(TTM, 2, 7, tostring(Round(fixnan(t12H), 2)), text_color = TC, bgcolor = SC_12H, text_size = TS)

if (currentTfInMinutes<=1440 and is1D)
    table.cell(TTM, 0, 8, "D", text_color = THC, bgcolor = c1440m, text_size = TS)
    table.cell(TTM, 1, 8, tostring(Round(fixnan(outK7), 0)), text_color = TC, bgcolor = VC_D, text_size = TS)
    table.cell(TTM, 2, 8, tostring(Round(fixnan(t7), 2)), text_color = TC, bgcolor = SC_D, text_size = TS)

if (currentTfInMinutes<=7200 and is5D)
    table.cell(TTM, 0, 9, "5D", text_color = THC, bgcolor = c7200m, text_size = TS)
    table.cell(TTM, 1, 9, tostring(Round(fixnan(outK10), 0)), text_color = TC, bgcolor = VC_5D, text_size = TS)
    table.cell(TTM, 2, 9, tostring(Round(fixnan(t10), 2)), text_color = TC, bgcolor = SC_5D, text_size = TS)

if (currentTfInMinutes<=10080 and is1W)
    table.cell(TTM, 0, 10, "W", text_color = THC, bgcolor = c1W, text_size = TS)
    table.cell(TTM, 1, 10, tostring(Round(fixnan(outK8), 0)), text_color = TC, bgcolor = VC_W, text_size = TS)
    table.cell(TTM, 2, 10, tostring(Round(fixnan(t8), 2)), text_color = TC, bgcolor = SC_W, text_size = TS)

if (currentTfInMinutes<=45000 and is1M)
    table.cell(TTM, 0, 11, "M", text_color = THC, bgcolor = c1M, text_size = TS)
    table.cell(TTM, 1, 11, tostring(Round(fixnan(outK9), 0)), text_color = TC, bgcolor = VC_M, text_size = TS)
    table.cell(TTM, 2, 11, tostring(Round(fixnan(t9), 2)), text_color = TC, bgcolor = SC_M, text_size = TS)


// time ticks
new_week = change(time("W"))
new_day = change(time("D"))
new_H = change(time("60"))

plotshape (new_week and currentTfInMinutes<10080 ? dotl-20*span : na, color=color.new(color.white,25) , style=shape.arrowup, location=location.absolute, size=size.normal)
plotshape (new_day  and currentTfInMinutes<1440  ? dotl-15*span : na, color=color.new(color.white,50) , style=shape.arrowup, location=location.absolute, size=size.small)
plotshape (new_H    and currentTfInMinutes<60    ? dotl-10*span : na, color=color.new(color.white,70) , style=shape.arrowup, location=location.absolute, size=size.tiny)
