Fib TSI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Options360
// 9/16/23 Fib TSI update to 1.1
// 10/30/23 update: 1) add Macro lengths 55, 89, 144, 233 & 377. 2) add All lower TSI histogram option. 3) add All Macro TSI histogram option.
//@version=5
indicator("Fib TSI new")
src = input(defval=ohlc4, title="Source")
ma = input.int(1, 'stoch D smoothing', minval=1)
tsia = input.int(3, title='TSI signal', tooltip='TSI uses 2 moving averages compared with each other.')
tsi = input.int(5, title='TSI fastest', tooltip='TSI uses 2 moving averages compared with each other. Default value is 3/5')
fastma = input.int(8, 'TSI fast', minval=1, tooltip='TSI uses 2 moving averages compared with each other. Default value is 5/8')
midma = input.int(13, 'TSI mid', minval=1, tooltip='TSI uses 2 moving averages compared with each other. Default value is 8/13')
slowma = input.int(21, 'TSI slow', minval=1, tooltip='TSI uses 2 moving averages compared with each other. Default value is 13/21')
slowma2 = input.int(34, 'TSI slowest', minval=1, tooltip='TSI uses 2 moving averages compared with each other. Default value is 21/34')
stoch = input.int(55, 'stoch K length & Macro length 1', minval=1, tooltip='TSI uses 2 moving averages compared with each other. Default value is 34/55')
slowma3 = input.int(89, 'Macro length 2', minval=1, tooltip='TSI uses 2 moving averages compared with each other. Default value is 55/89')
slowma4 = input.int(144, 'Macro length 3', minval=1, tooltip='TSI uses 2 moving averages compared with each other. Default value is 89/144')
slowma5 = input.int(233, 'Macro length 4', minval=1, tooltip='TSI uses 2 moving averages compared with each other. Default value is 144/233')
slowma6 = input.int(377, 'Macro length 5', minval=1, tooltip='TSI uses 2 moving averages compared with each other. Default value is 233/377')
all = input.int(1, 'All rising / falling', minval=1, tooltip='Total average TSI of all rising or falling length bars ago')
//
ma1a = ta.tsi(src, tsia, tsi)
ma1 = ta.tsi(src, tsi, fastma)
ma2 = ta.tsi(src, fastma, midma)
ma3 = ta.tsi(src, midma, slowma)
ma4 = ta.tsi(src, slowma, slowma2)
//
ma5 = ta.tsi(src, slowma2, stoch)
ma6 = ta.tsi(src, stoch, slowma3)
ma7 = ta.tsi(src, slowma3, slowma4)
ma8 = ta.tsi(src, slowma4, slowma5)
ma9 = ta.tsi(src, slowma5, slowma6)
//
maa = ta.sma(ta.stoch(ma1a, ma1a, ma1a, stoch), ma)
ma1b = ta.sma(ta.stoch(ma1, ma1, ma1, stoch), ma)
ma2b = ta.sma(ta.stoch(ma2, ma2, ma2, stoch), ma)
ma3b = ta.sma(ta.stoch(ma3, ma3, ma3, stoch), ma)
ma4b = ta.sma(ta.stoch(ma4, ma4, ma4, stoch), ma)
//
ma5b = ta.sma(ta.stoch(ma5, ma5, ma5, stoch), ma)
ma6b = ta.sma(ta.stoch(ma6, ma6, ma6, stoch), ma)
ma7b = ta.sma(ta.stoch(ma7, ma7, ma7, stoch), ma)
ma8b = ta.sma(ta.stoch(ma8, ma8, ma8, stoch), ma)
ma9b = ta.sma(ta.stoch(ma9, ma9, ma9, stoch), ma)
//
total = (maa + ma1b + ma2b + ma3b + ma4b)/5
total2 = (ma5b + ma6b + ma7b + ma8b + ma9b)/5
tot = (total + total2)/2
//
up = #3cfe12
up2 = #4e8642
down2 = #8c2121
down = #ff0202
dir = (total[all] < total ? up : down)
dir2 = (total>=50 ? (total[1] < total ? up : up2) : (total[1] < total ? down2 : down))
//
plot(maa, 'TSI Fastest', color=color.new(#ffffff, 0), linewidth=2)
plot(ma1b, 'TSI Fast', color=color.new(#eaff00, 0))//), display=display.none)
plot(ma2b, 'TSI Mid', color=color.new(#ff7700, 0))//, display=display.none)
plot(ma3b, 'TSI Slow', color=color.new(#8d0eee, 0))//, display=display.none)
plot(ma4b, 'TSI Slowest', color=color.new(#008eff, 0), linewidth=2)
plot(total, 'All lower TSI', color=color.new((total>=50 ? (total[1] < total ? #3cfe12 : #4e8642) : (total[1] < total ? #8c2121 : #ff0202)), 70), linewidth=4, style=plot.style_area, histbase=50)
//
plot(ma5b, 'TSI Macro1', color=color.new(#ffffff, 0), linewidth=2, display=display.none)
plot(ma6b, 'TSI Macro2', color=color.new(#eaff00, 0), display=display.none)
plot(ma7b, 'TSI Macro3', color=color.new(#008eff, 0), display=display.none)
plot(ma8b, 'TSI Macro4', color=color.new(#8d0eee, 0), display=display.none)
plot(ma9b, 'TSI Macro5', color=color.new(#ff7700, 0), linewidth=2, display=display.none)
plot(total2, 'All macro TSI', color=color.new((total>=50 ? (total[1] < total ? #3cfe12 : #4e8642) : (total[1] < total ? #8c2121 : #ff0202)), 70), linewidth=4, style=plot.style_area, histbase=50, display=display.none)
plot(tot, 'All TSI', color=color.new(#ffffff, 0), histbase=50)//color=color.new(dir2, 70), linewidth=4, style=plot.style_area, histbase=50, display=display.none)
//
ArrowUp = ta.crossover(total, 1) or ta.crossover(total, 2) or ta.crossover(total, 3) or ta.crossover(total, 4) or ta.crossover(total, 5) or ta.crossover(total, 6) or ta.crossover(total, 7) or ta.crossover(total, 8) or ta.crossover(total, 9)
plotshape(ArrowUp, title='long', style=shape.triangleup, location=location.bottom, color=(#3cfe12), size=size.small)
ArrowUp2 = ta.crossover(maa, 5) or ta.crossover(maa, 10) or ta.crossover(maa, 15) or ta.crossover(maa, 20) or ta.crossover(maa, 25)// or ta.crossover(total, 40) or ta.crossover(total, 50) or ta.crossover(total, 60) or ta.crossover(total, 70) or ta.crossover(total, 80) or ta.crossover(total, 90)
plotshape(ArrowUp2, title='crossover', style=shape.triangleup, location=location.bottom, color=(#3cfe12), size=size.tiny)//, display=display.none)
au = ta.rising(tot, 1)
plotchar(au, title='bullish', char=".", location=location.bottom, color=(#3cfe12), size=size.tiny)//, display=display.none)
ArrowDown = ta.crossunder(total, 99) or ta.crossunder(total, 98) or ta.crossunder(total, 97) or ta.crossunder(total, 96) or ta.crossunder(total, 95) or ta.crossunder(total, 94) or ta.crossunder(total, 93) or ta.crossunder(total, 92) or ta.crossunder(total, 91)
plotshape(ArrowDown, title='short', style=shape.triangledown, location=location.bottom, color=(#ff0202), size=size.small)
ArrowDown2 = ta.crossunder(maa, 95) or ta.crossunder(maa, 90) or ta.crossunder(maa, 85) or ta.crossunder(maa, 80) or ta.crossunder(maa, 75)// or ta.crossunder(total, 60) or ta.crossunder(total, 50) or ta.crossunder(total, 40) or ta.crossunder(total, 30) or ta.crossunder(total, 20) or ta.crossunder(total, 10)
plotshape(ArrowDown2, title='crossunder', style=shape.triangledown, location=location.bottom, color=(#ff0202), size=size.tiny)//, display=display.none)
ad = ta.falling(tot, 1)
plotchar(ad, title='bearish', char=".", location=location.bottom, color=(#ff0202), size=size.tiny)//, display=display.none)
//
ArrowUp3 = ta.cross(tot, 1) or ta.crossover(tot, 2) or ta.crossover(tot, 3) or ta.crossover(tot, 4) or ta.crossover(tot, 5) or ta.crossover(tot, 6) or ta.crossover(tot, 7) or ta.crossover(tot, 8) or ta.crossover(tot, 9)
plotshape(ArrowUp3, title='superlong', style=shape.triangleup, location=location.bottom, color=(#3cfe12), size=size.normal)
ArrowDown3 = ta.cross(tot, 99) or ta.crossunder(tot, 98) or ta.crossunder(tot, 97) or ta.crossunder(tot, 96) or ta.crossunder(tot, 95) or ta.crossunder(tot, 94) or ta.crossunder(tot, 93) or ta.crossunder(tot, 92) or ta.crossunder(tot, 91)
plotshape(ArrowDown3, title='supershort', style=shape.triangledown, location=location.bottom, color=(#ff0202), size=size.normal)
//
h0 = hline(100, 'top', color=#989595, linestyle=hline.style_dotted)
h1 = hline(75, '75', color=#989595, linestyle=hline.style_dotted)
h2 = hline(50, '50', color=#989595, linestyle=hline.style_dashed)
h3 = hline(25, '25', color=#989595, linestyle=hline.style_dotted)
h4 = hline(0, '0', color=#989595, linestyle=hline.style_dotted)