TICK Grid (TheMas7er)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TheMas7er and bmistiaen

//@version=5
indicator('TICK Grid (TheMas7er)', 'TICK Grid')


// Constants
var string  c_0_em_5    = ' '                                               //  0.5 em space
var string  c_1_em      = ' '                                               //  1   em space
var string  c_5_em      = c_1_em + c_1_em + c_1_em + c_1_em + c_1_em        //  5   em spaces
var string  c_15_em     = c_5_em + c_5_em + c_5_em                          // 15   em spaces
var string  c_18_em_5   = c_15_em + c_1_em + c_1_em + c_1_em + c_0_em_5     // 18.5 em spaces


// Defaults
var int     d_L1            = 600
var int     d_L2            = 800
var string  d_ticker        = 'USI:TICK'
var color   d_colorHi       = color.green
var color   d_colorLo       = color.red
var color   d_colorDown     = color.black
var color   d_colorUp       = color.green
var color   d_colorZero     = color.black
var color   d_colorWick     = color.black
var color   d_colorBorder   = color.black


// Inputs
var bool    i_showTickHi    = input.bool(true,           'Positive' + c_0_em_5,       group='levels',     inline='hi')
var int     i_tickHi        = input.int(d_L1,            '',                minval=0, group='levels',     inline='hi')
var int     i_tickHi1       = input.int(d_L2,            '',                minval=0, group='levels',     inline='hi')
var color   i_colorHi       = input.color(d_colorHi,     '',                          group='levels',     inline='hi')
var bool    i_showTickLo    = input.bool(true,           'Negative',                  group='levels',     inline='lo')
var int     i_tickLo        = input.int(-d_L1,           '',                maxval=0, group='levels',     inline='lo')
var int     i_tickLo1       = input.int(-d_L2,           '',                maxval=0, group='levels',     inline='lo')
var color   i_colorLo       = input.color(d_colorLo,     '',                          group='levels',     inline='lo')
var bool    i_showZero      = input.bool(true,           'Zero' + c_18_em_5,          group='levels',     inline='ze')
var color   i_colorZero     = input.color(d_colorZero,   '',                          group='levels',     inline='ze')

var color   i_colorUp       = input.color(d_colorUp,     'Up',                        group='bar colors', inline='ba')
var color   i_colorDown     = input.color(d_colorDown,   'Down',                      group='bar colors', inline='ba')
var color   i_colorWick     = input.color(d_colorWick,   'Wick',                      group='bar colors', inline='ba')
var color   i_colorBorder   = input.color(d_colorBorder, 'Border',                    group='bar colors', inline='ba')

var string  i_ticker        = input.symbol(d_ticker,     'Ticker',                    group='symbol',     inline='ti')


// Functions
f_showLevel(_show, _level) => _show ? _level ? _level : na : na // Hide level when value is zero.


// Levels
var int     tickhi  = f_showLevel(i_showTickHi, i_tickHi)
var int     tickhi1 = f_showLevel(i_showTickHi, i_tickHi1)
var int     zero    = i_showZero ? 0 : na
var int     ticklo  = f_showLevel(i_showTickLo, i_tickLo)
var int     ticklo1 = f_showLevel(i_showTickLo, i_tickLo1)


// Other variables
var bool canPlot = na


// Data Retrieval
[ticko, tickh, tickl, tickc, tickfb, ticklb] = request.security(i_ticker, timeframe.period, [open, high, low, close, session.isfirstbar, session.islastbar])


// Only plot candles when there is tick data available
canPlot := tickfb ? true : ticklb[1] ? false : canPlot
if not canPlot
    ticko := na
    tickh := na
    tickl := na
    tickc := na


// Plot Candles
candleColor = ticko < tickc ? i_colorUp : i_colorDown
plotcandle(ticko, tickh, tickl, tickc, 'Tick', candleColor, i_colorWick, bordercolor = i_colorBorder)


// Plot levels
hline(tickhi,  'High',  i_colorHi)
hline(tickhi1, 'High1', i_colorHi)
hline(zero,    'Zero',  i_colorZero)
hline(ticklo,  'Low',   i_colorLo)
hline(ticklo1, 'Low1',  i_colorLo)
