Trend intensity 65 TI65
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TintinTrading

//@version=5
indicator(title="[TTI] TI65",shorttitle="TI65")
snes        = input(defval=5, title="Sensitivity", tooltip = "Decrease this value to increase the sensitivity of the indicator and get more signals. Default value is 5")

//INPUTS––––––––––––––––––––––––––––––––––––––––––
TIH_show    = input(defval=true, title='Show Higher TI65', group = 'Add Indicator', tooltip = "Check to add additional TI65 signal from higher timeframe")
TIH_res     = input.timeframe(defval='', title='Higher Timeframe', group ='Add Indicator', tooltip = "Choose higher timeframe for additional TI65signal")
trnsp       = input(defval=95, title="Transparency",group ='Add Indicator', tooltip = "Change the setting for the transparency of the higher timeframe")

mdtsh       = input.bool(true,   title = "Show MDT up", group = 'Add Indicator', inline = "3")
mdtshd      = input.bool(true,   title = "Show MDT down", group = 'Add Indicator', inline = "4")

movepar     = input(defval=7.0, title="Offset", group = 'Label')


//CALCULATIONS––––––––––––––––––––––––––––––––––––––––––
calc_ma(src, len) =>
    v1 = ta.sma(src, len) 

HMA0 = calc_ma(close, 7)
HMA1 = calc_ma(close, 65)
HMA_res_final = TIH_res == '' ? timeframe.period : TIH_res

HMA0_final = request.security(syminfo.tickerid, HMA_res_final, HMA0)
HMA1_final = request.security(syminfo.tickerid, HMA_res_final, HMA1)

TIH65 = HMA0_final/HMA1_final


MDTup       = mdtsh==1 and math.round(close/ta.sma(close,126),2)>(1+snes/100) ? 0.8 : 0
MDTdown     = mdtshd==1 and math.round(close/ta.sma(close,126),2)<(1-snes/100) ? 0.8 : 0



TI65  = ta.sma(close,7)/ta.sma(close,65)

Line0 = plot(0)


TIIH = if TIH65>(1+snes/100)
    1.1
else
    0

TIIDH = if TIH65<(1-snes/100)
    1.1
else
    0


TII = if TI65>(1+snes/100)
    1
else
    0

TIID = if TI65<(1-snes/100)
    1
else
    0
    
loctext=int(bar_index+movepar)
loctext2=int(bar_index+2*movepar)


//PLOTS––––––––––––––––––––––––––––––––––––––––––
TIIIH = plot(TIH_show ? TIIH : 0, color=color.orange, title = "Higher Timeframe UP Line Color")
fill(TIIIH,Line0,color=color.green, transp=trnsp, title = "Higher Timeframe UP color")

TIIIDH = plot(TIH_show ? TIIDH : 0, color=color.orange, title = "Higher Timeframe DOWN Line Color")
fill(TIIIDH,Line0,color=color.red, transp=trnsp, title = "Higher Timeframe DOWN color")


TIII = plot(TII, title = "UP Line Color")
fill(TIII,Line0,color=color.green, transp=80, title = "Up Color")

TIIID = plot(TIID, title = "DOWN Line Color")
fill(TIIID,Line0,color=color.red, transp=80, title = "Down Color")


MDTu = plot(MDTup, title = "MDT up", color=color.aqua)
fill(MDTu,Line0,color=color.aqua, transp=80, title = "MDT Up color")


MDTd = plot(MDTdown, title = "MDT down", color=color.purple)
fill(MDTu,Line0,color=color.purple, transp=80, title = "MDT Up color")



l1 = label.new(loctext,1.20, text = "H TI65", style = label.style_label_center, color=color.orange,textcolor=color.white, size = size.small, textalign=text.align_left)
label.delete(l1[1])

l2 = label.new(loctext,1.00, text = "TI65", style = label.style_label_center, color=color.blue,textcolor=color.white, size = size.small, textalign=text.align_left)
label.delete(l2[1])

l3 = label.new(loctext,0.80, text = "MDT", style = label.style_label_center, color=color.purple,textcolor=color.white, size = size.small, textalign=text.align_left)
label.delete(l3[1])

