LNL Smart TICK
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//
// @version=5
//
// LNL Smart $TICK
//
// This study is mostly beneficial for intraday traders. It is basically a user-friendly "colorful" representation of the $TICK chart with highlighted the $TICK extremes. 
// Smart TICK also includes simple EMA gauge as well as cumulative $TICK values & cumulative $TICK cloud which can serve as an additional confirmation for intraday bias.
//
// Credit for the main part of the cumulative tick code to PineCoders-LucF & Investorito
// 
// Created by L&L Capital
// 

indicator("LNL Smart TICK",shorttitle = "Smart TICK")

// Inputs

emalength = input(20,title = "EMA Length")
Hi1 = input(500,title = "HighLine 1")
Hi2 = input(1000,title = "HighLine 2")
Hi3 = input(1300,title = "HighLine 3")
Lo1 = input(-500,title = "LowLine 1")
Lo2 = input(-1000,title = "LowLine 2")
Lo3 = input(-1300,title = "LowLine 3")

// $TICK Calculations & Plot 

tickO = request.security("USI:TICK", timeframe.period, open)
tickH = request.security("USI:TICK", timeframe.period, high)
tickL = request.security("USI:TICK", timeframe.period, low)
tickC = request.security("USI:TICK", timeframe.period, close)
tickHL2 = (tickH + tickL)/2

plotcandle(tickO,tickH,tickL,tickC,  title='TICK', color = tickO < tickC ? #009900 : #cc0000, wickcolor = tickO < tickC ? #009900 : #cc0000)

// Cumulative $TICK Calculations & Plot

cumtickdata = (request.security("USI:TICK", timeframe.period, hl2))
timeset = not na(time(timeframe.period, "0930-1555"))
start = timeset and not timeset[1]
var cumtick = 0.
cumtick := start ? 0. : cumtick + cumtickdata
cloudcolor = cumtick > ta.ema(cumtick,5) ? color.rgb(76,175,80,70) : color.rgb(255,82,82,70)
devidor = timeframe.period == "1" ? 80 : timeframe.period == "5" ? 20 : 5

plot(cumtick/devidor, title = "Cumulative Tick Cloud", color = cloudcolor, linewidth = 1, style =plot.style_area)

// $TICK Extreme Lines Calculations & Plot

hline(0, title = "ZeroLine", linestyle = hline.style_solid, linewidth = 1, color = #787b86)
hline(Hi1, title = "HighLine 1", linestyle = hline.style_solid, linewidth = 1, color = #1b5e20)
hline(Hi2, title = "HighLine 2", linestyle = hline.style_solid, linewidth = 2, color = #27c22e)
hline(Hi3, title = "HighLine 3", linestyle = hline.style_dashed, linewidth = 1, color = #1b5e20)
hline(Lo1, title = "LowLine 1", linestyle = hline.style_solid, linewidth = 1, color = #801922)
hline(Lo2, title = "LowLine 2", linestyle = hline.style_solid, linewidth = 2, color = #ff0000)
hline(Lo3, title = "LowLine 3", linestyle = hline.style_dashed, linewidth = 1, color = #801922)

// EMA Trend Gauge Plot

ema = ta.sma(tickC,emalength)
plot(ema,title = "ema", color = ema > 0 ? #27c22e : #ff0000, linewidth = 3)

// Extreme Readings Calculations & Plot

extremeup = tickH > Hi2
plotshape(extremeup, title='Extreme Up',style=shape.triangleup,location=location.top,color=#27c22e,size=size.tiny) 
extremedn = tickL < Lo2
plotshape(extremedn, title='Extreme Down',style=shape.triangledown,location=location.bottom,color=#ff0000,size=size.tiny) 

// Breadth Ratios

uvol = request.security("USI:UVOL", timeframe.period, close)
dvol = request.security("USI:DVOL", timeframe.period, close)
uvolq = request.security("USI:UVOLQ", timeframe.period, close)
dvolq = request.security("USI:DVOLQ", timeframe.period, close)

nyseratio = (uvol >= dvol) ? (uvol / dvol) : -(dvol / uvol)
nasdratio = (uvolq >= dvolq) ? (uvolq / dvolq) : -(dvolq / uvolq)

// $TICK Tables Calculations & Plot

Text1 = string ("$TICK:")
Color1 = color.gray
if tickC > 0
    Color1 := #27c22e 
else
    Color1 := #ff0000
    
Text2 = string ("Cumulative $TICK:")
Color2 = color.gray
if cumtick > 0
    Color2 := #27c22e 
else
    Color2 := #ff0000

Text3 = string ("NYSE:")
Color3 = color.gray
if nyseratio >= 0
    Color3 := #27c22e 
else
    Color3 := #ff0000
     
Text4 = string ("NASD:")
Color4 = color.gray
if nasdratio >= 0
    Color4 := #27c22e 
else
    Color4 := #ff0000
   
Table = table.new(position = position.top_left,  columns=10, rows=1, bgcolor=color.gray)
if barstate.islast
    table.cell(table_id=Table, column=1, row=0, text=Text1, height=0, text_color=color.rgb(0, 0, 0), text_halign=text.align_center, text_size = size.auto, text_valign= text.align_center, bgcolor=Color1)  
    table.cell(table_id=Table, column=2, row=0, text=str.tostring(tickC), height=0, text_color=color.rgb(0, 0, 0), text_halign=text.align_center, text_size = size.auto, text_valign= text.align_center, bgcolor=Color1)  
    table.cell(table_id=Table, column=3, row=0, text=Text2, height=0,text_color=color.rgb(0, 0, 0), text_halign=text.align_center, text_valign=text.align_center, bgcolor=Color2)
    table.cell(table_id=Table, column=4, row=0, text=str.tostring(cumtick), height=0,text_color=color.rgb(0, 0, 0), text_halign=text.align_center, text_valign=text.align_center, bgcolor=Color2)
    table.cell(table_id=Table, column=5, row=0, text=Text3, height=0,text_color=color.rgb(0, 0, 0), text_halign=text.align_center, text_valign=text.align_center, bgcolor=Color3)
    table.cell(table_id=Table, column=6, row=0, text=str.tostring(math.round(nyseratio,2)), height=0,text_color=color.rgb(0, 0, 0), text_halign=text.align_center, text_valign=text.align_center, bgcolor=Color3)
    table.cell(table_id=Table, column=7, row=0, text=Text4, height=0,text_color=color.rgb(0, 0, 0), text_halign=text.align_center, text_valign=text.align_center, bgcolor=Color4)
    table.cell(table_id=Table, column=8, row=0, text=str.tostring(math.round(nasdratio,2)), height=0,text_color=color.rgb(0, 0, 0), text_halign=text.align_center, text_valign=text.align_center, bgcolor=Color4)