Volume and Price Z-Score [Multi-Asset] - By Leviathan
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeviathanCapital

//@version=5
indicator("Volume and Price Z-Score [Multi-Asset] - By Leviathan", max_labels_count = 500, max_lines_count = 500, max_boxes_count = 100)

g1  = 'Symbols'
g2  = 'General'
g3  = 'Columns/Circles'
g4  = 'Scatter Plot'
g10 = 'Heatmap'
g5  = 'Group 1'
g6  = 'Group 2'
g7  = 'Group 3'
g8  = 'Group 4'
g9  = 'Group 5'

tt1 = 'Select the group of symbols that should be displayed. The symbols can be adjusted in the last section of indicator settings'
tt2 = 'Select the color palette'
tt3 = 'Select how the data should be displayed'
tt4 = 'Mean is the moving average of price/volume (fundamental part of calculating the z-score). This input controls the length of the moving average'
tt5 = 'This input determines the length of the moving average for calculating the Average Z-Score. '
tt6 = 'Select the type of z-score you wish to display. Please read more about this in indicator description.'
tt7 = 'Select the timeframe used in calculations. Default is current chart timeframe.'
tt8 = 'Select the type of volume used for calculating volume z-score. Please read more about this in indicator description.'
tt9 = 'Select the type of moving average used for calculating the mean. SMA equally weights all prices in the period. Gives more weight to recent values, making it more sensitive to recent action.'

// General
ztype         = input.string('Current Z-Score', 'Z-Score Type', options = ['Current Z-Score', 'Average Z-Score', 'Relative Z-Score'], tooltip = tt6)
disp          = input.string('Scatter Plot', 'Display', ['Circles', 'Scatter Plot', 'Columns', 'Z-Price Heatmap', 'Z-Volume Heatmap', 'Z-Delta Heatmap', 'Delta Columns'], tooltip = tt3)
meantype      = input.string('SMA', 'Mean Type', ['SMA', 'EMA'], tooltip = tt9)
zlen          = input.int(20, 'Mean Length', tooltip = tt4)
azlen         = input.int(14, 'Average Z-Score Length', tooltip = tt5)
voltype       = input.string('VOLUME', 'Volume Type', ['VOLUME', 'OBV'], tooltip = tt8)
gn            = input.string('Group 1', 'Asset Group', options = ['Group 1', 'Group 2', 'Group 3', 'Group 4', 'Group 5'], tooltip = tt1)
pl            = input.string('Palette 1', 'Color Palette', ['Palette 1', 'Palette 2', 'Palette 3', 'Palette 4', 'Palette 5'], tooltip = tt2)
tf            = input.timeframe('', 'Bar Timeframe', tooltip = tt7) 
showtitle     = input.bool(true, 'Show Title')

// Scatter
priceshape    = input.string('Circle', 'Shape & Transparency', options = ['Circle', 'Square', 'Diamond'], group = g4, inline = 'sp')
trans         = input.int(50, '', group = g4, inline = 'sp')
showquadr     = input.bool(true, 'Square (σ)    ', inline = 'quadrant', group = g4)
quadr         = input.float(1, '', step = 0.1, inline = 'quadrant', group = g4)
quadrcol      = input.color(color.gray, '', inline = 'quadrant', group = g4)
quadrbgcol    = input.color(color.rgb(120, 123, 134, 100), '', inline = 'quadrant', group = g4)
showquadr1    = input.bool(true, 'Square (σ)    ', inline = 'quadrant1', group = g4)
quadr1        = input.float(2, '', step = 0.1, inline = 'quadrant1', group = g4)
quadrcol1     = input.color(color.gray, '', inline = 'quadrant1', group = g4)
quadrbgcol1   = input.color(color.rgb(120, 123, 134, 100), '', inline = 'quadrant1', group = g4)
showquadr2    = input.bool(true, 'Square (σ)    ', inline = 'quadrant2', group = g4)
quadr2        = input.float(3, '', step = 0.1, inline = 'quadrant2', group = g4)
quadrcol2     = input.color(color.gray, '', inline = 'quadrant2', group = g4)
quadrbgcol2   = input.color(color.rgb(120, 123, 134, 100), '', inline = 'quadrant2', group = g4)
gridcol       = #787b861f
gridcol2      = #787b868f
lblplotscond  = input.string('Outliers', 'Symbol names', options = ['Outliers', 'All', 'None'], group = g4, inline = 'lblcol')
lblplotscol   = input.color(color.gray, '', inline = 'lblcol', group = g4)
outlierstd    = input.float(1, ' SD(σ) >', group = g4, inline = 'lblcol')
helperlab     = input.bool(true, 'Helper Labels', group = g4)

// Circles
zprice        = input.bool(true, '', inline = 'p', group = g3)
zvol          = input.bool(true, '', inline = 'v', group = g3)
priceshape2   = input.string('Circle', 'Z-Price  ', options = ['Circle', 'Square', 'Diamond'], inline = 'p', group = g3)
volshape      = input.string('Circle', 'Z-Volume', options = ['Circle', 'Square', 'Diamond'], inline = 'v', group = g3)

// Heatmap
hmpos         = input.string('1', 'Heatmap Position', ['1', '2', '3', '4', '5'], group = g10)
hmcoltype     = input.string('Two-Color', 'Color coding', ['Two-Color', 'Multi-Color'], group = g10)
colh1         = input.color(#b22222, 'Multi-Color (High->Low)', inline = 'hm', group = g10)  
colh2         = input.color(#ffa07a, '', inline = 'hm', group = g10)  
colh3         = input.color(#ffebeb, '', inline = 'hm', group = g10)  
colc1         = input.color(#4682b4, '', inline = 'hm', group = g10)  
colc2         = input.color(#add8e6, '', inline = 'hm', group = g10)  
colc3         = input.color(#f0f8ff, '', inline = 'hm', group = g10)  
colhh         = input.color(#FF6161, 'Two-Color (High->Low)', inline = 'hm2', group = g10)
colcc         = input.color(#1A237E, '', inline = 'hm2', group = g10)


// Function for switching object shape
switchshape(x) =>
    switch x
        'Circle'  => label.style_circle
        'Square'  => label.style_square
        'Diamond' => label.style_diamond


// User inputs - Group 1
sym1   = input.symbol('BTCUSDT.P', ''       , group = g5), sym2   = input.symbol('ETHUSDT.P', ''       , group = g5), sym3   = input.symbol('SOLUSDT.P', ''       , group = g5), sym4   = input.symbol('OGNUSDT.P', ''       , group = g5)
sym5   = input.symbol('LEVERUSDT.P', ''     , group = g5), sym6   = input.symbol('LINKUSDT.P', ''      , group = g5), sym7   = input.symbol('XRPUSDT.P', ''       , group = g5), sym8   = input.symbol('TRBUSDT.P', ''       , group = g5)
sym9   = input.symbol('WLDUSDT.P', ''       , group = g5), sym10  = input.symbol('LPTUSDT.P', ''       , group = g5), sym11  = input.symbol('BCHUSDT.P', ''       , group = g5), sym12  = input.symbol('STORJUSDT.P', ''     , group = g5)
sym13  = input.symbol('MATICUSDT.P', ''     , group = g5), sym14  = input.symbol('DOGEUSDT.P', ''      , group = g5), sym15  = input.symbol('BNBUSDT.P', ''       , group = g5), sym16  = input.symbol('RUNEUSDT.P', ''      , group = g5)
sym17  = input.symbol('ARBUSDT.P', ''       , group = g5), sym18  = input.symbol('MKRUSDT.P', ''       , group = g5), sym19  = input.symbol('LTCUSDT.P', ''       , group = g5), sym20  = input.symbol('PEPEUSDT.P', ''      , group = g5)
sym21  = input.symbol('SUIUSDT.P', ''       , group = g5), sym22  = input.symbol('ADAUSDT.P', ''       , group = g5), sym23  = input.symbol('OPUSDT.P', ''        , group = g5), sym24  = input.symbol('APEUSDT.P', ''       , group = g5)
sym25  = input.symbol('YGGUSDT.P', ''       , group = g5), sym26  = input.symbol('IOTXUSDT.P', ''      , group = g5), sym27  = input.symbol('DOTUSDT.P', ''       , group = g5), sym28  = input.symbol('CRVUSDT.P', ''       , group = g5)
sym29  = input.symbol('ETCUSDT.P', ''       , group = g5), sym30  = input.symbol('GALAUSDT.P', ''      , group = g5), sym31  = input.symbol('AVAXUSDT.P', ''      , group = g5), sym32  = input.symbol('CFXUSDT.P', ''       , group = g5)
sym33  = input.symbol('APTUSDT.P', ''       , group = g5), sym34  = input.symbol('SHIBUSDT.P', ''      , group = g5), sym35  = input.symbol('AMBUSDT.P', ''       , group = g5), sym36  = input.symbol('FTMUSDT.P', ''       , group = g5)
sym37  = input.symbol('FILUSDT.P', ''       , group = g5), sym38  = input.symbol('ATOMUSDT.P', ''      , group = g5), sym39  = input.symbol('COMPUSDT.P', ''      , group = g5), sym40  = input.symbol('DYDXUSDT.P', ''      , group = g5)
// User inputs - Group 2
sym41  = input.symbol('LQTYUSDT.P', ''      , group = g6), sym42  = input.symbol('AXSUSDT.P', ''       , group = g6), sym43  = input.symbol('GTCUSDT.P', ''       , group = g6), sym44  = input.symbol('EOSUSDT.P', ''       , group = g6)
sym45  = input.symbol('STMXUSDT.P', ''      , group = g6), sym46  = input.symbol('STXUSDT.P', ''       , group = g6), sym47  = input.symbol('FRONTUSDT.P', ''     , group = g6), sym48  = input.symbol('LUNA2USDT.P', ''     , group = g6)
sym49  = input.symbol('MASKUSDT.P', ''      , group = g6), sym50  = input.symbol('BAKEUSDT.P', ''      , group = g6), sym51  = input.symbol('TOMOUSDT.P', ''      , group = g6), sym52  = input.symbol('BLZUSDT.P', ''       , group = g6)
sym53  = input.symbol('PERPUSDT.P', ''      , group = g6), sym54  = input.symbol('RNDRUSDT.P', ''      , group = g6), sym55  = input.symbol('LDOUSDT.P', ''       , group = g6), sym56  = input.symbol('TRXUSDT.P', ''       , group = g6)
sym57  = input.symbol('1000FLOKIUSDT.P', '' , group = g6), sym58  = input.symbol('AAVEUSDT.P', ''      , group = g6), sym59  = input.symbol('NEARUSDT.P', ''      , group = g6), sym60  = input.symbol('HIFIUSDT.P', ''      , group = g6)
sym61  = input.symbol('CYBERUSDT.P', ''     , group = g6), sym62  = input.symbol('INJUSDT.P', ''       , group = g6), sym63  = input.symbol('SANDUSDT.P', ''      , group = g6), sym64  = input.symbol('UNFIUSDT.P', ''      , group = g6)
sym65  = input.symbol('UNIUSDT.P', ''       , group = g6), sym66  = input.symbol('ALGOUSDT.P', ''      , group = g6), sym67  = input.symbol('XLMUSDT.P', ''       , group = g6), sym68  = input.symbol('LITUSDT.P', ''       , group = g6)
sym69  = input.symbol('MANAUSDT.P', ''      , group = g6), sym70  = input.symbol('KNCUSDT.P', ''       , group = g6), sym71  = input.symbol('FETUSDT.P', ''       , group = g6), sym72  = input.symbol('XEMUSDT.P', ''       , group = g6)
sym73  = input.symbol('SEIUSDT.P', ''       , group = g6), sym74  = input.symbol('GMTUSDT.P', ''       , group = g6), sym75  = input.symbol('CELOUSDT.P', ''      , group = g6), sym76  = input.symbol('HBARUSDT.P', ''      , group = g6)
sym77  = input.symbol('CHZUSDT.P', ''       , group = g6), sym78  = input.symbol('CHRUSDT.P', ''       , group = g6), sym79  = input.symbol('BLURUSDT.P', ''      , group = g6), sym80  = input.symbol('RDNTUSDT.P', ''      , group = g6)
// User inputs - Group 3
sym81  = input.symbol('QTUMUSDT.P', ''      , group = g7), sym82  = input.symbol('WAVESUSDT.P', ''     , group = g7), sym83  = input.symbol('1000XECUSDT.P', ''   , group = g7), sym84  = input.symbol('MAVUSDT.P', ''       , group = g7)
sym85  = input.symbol('FLMUSDT.P', ''       , group = g7), sym86  = input.symbol('MAGICUSDT.P', ''     , group = g7), sym87  = input.symbol('HIGHUSDT.P', ''      , group = g7), sym88  = input.symbol('AGIXUSDT.P', ''      , group = g7)
sym89  = input.symbol('NMRUSDT.P', ''       , group = g7), sym90  = input.symbol('SNXUSDT.P', ''       , group = g7), sym91  = input.symbol('IMXUSDT.P', ''       , group = g7), sym92  = input.symbol('SPELLUSDT.P', ''     , group = g7)
sym93  = input.symbol('BELUSDT.P', ''       , group = g7), sym94  = input.symbol('JOEUSDT.P', ''       , group = g7), sym95  = input.symbol('COMBOUSDT.P', ''     , group = g7), sym96  = input.symbol('OMGUSDT.P', ''       , group = g7)
sym97  = input.symbol('1000LUNCUSDT.P', ''  , group = g7), sym98  = input.symbol('VETUSDT.P', ''       , group = g7), sym99  = input.symbol('MTLUSDT.P', ''       , group = g7), sym100 = input.symbol('LINAUSDT.P', ''      , group = g7)
sym101 = input.symbol('TRUUSDT.P', ''       , group = g7), sym102 = input.symbol('SXPUSDT.P', ''       , group = g7), sym103 = input.symbol('C98USDT.P', ''       , group = g7), sym104 = input.symbol('GMXUSDT.P', ''       , group = g7)
sym105 = input.symbol('IDUSDT.P', ''        , group = g7), sym106 = input.symbol('ENJUSDT.P', ''       , group = g7), sym107 = input.symbol('ZILUSDT.P', ''       , group = g7), sym108 = input.symbol('SUSHIUSDT.P', ''     , group = g7)
sym109 = input.symbol('XMRUSDT.P', ''       , group = g7), sym110 = input.symbol('KAVAUSDT.P', ''      , group = g7), sym111 = input.symbol('FLOWUSDT.P', ''      , group = g7), sym112 = input.symbol('ARKMUSDT.P', ''      , group = g7)
sym113 = input.symbol('PENDLEUSDT.P', ''    , group = g7), sym114 = input.symbol('GRTUSDT.P', ''       , group = g7), sym115 = input.symbol('DARUSDT.P', ''       , group = g7), sym116 = input.symbol('1INCHUSDT.P', ''     , group = g7)
sym117 = input.symbol('EGLDUSDT.P', ''      , group = g7), sym118 = input.symbol('AGLDUSDT.P', ''      , group = g7), sym119 = input.symbol('HOOKUSDT.P', ''      , group = g7), sym120 = input.symbol('WOOUSDT.P', ''       , group = g7)
// User inputs - Group 4
sym121 = input.symbol('OCEANUSDT.P', ''     , group = g8), sym122 = input.symbol('THETAUSDT.P', ''     , group = g8), sym123 = input.symbol('IOSTUSDT.P', ''      , group = g8), sym124 = input.symbol('NKNUSDT.P', ''       , group = g8)
sym125 = input.symbol('YFIUSDT.P', ''       , group = g8), sym126 = input.symbol('ENSUSDT.P', ''       , group = g8), sym127 = input.symbol('ICPUSDT.P', ''       , group = g8), sym128 = input.symbol('API3USDT.P', ''      , group = g8)
sym129 = input.symbol('REEFUSDT.P', ''      , group = g8), sym130 = input.symbol('EDUUSDT.P', ''       , group = g8), sym131 = input.symbol('PHBUSDT.P', ''       , group = g8), sym132 = input.symbol('PEOPLEUSDT.P', ''    , group = g8)
sym133 = input.symbol('BANDUSDT.P', ''      , group = g8), sym134 = input.symbol('STGUSDT.P', ''       , group = g8), sym135 = input.symbol('MDTUSDT.P', ''       , group = g8), sym136 = input.symbol('FXSUSDT.P', ''       , group = g8)
sym137 = input.symbol('ROSEUSDT.P', ''      , group = g8), sym138 = input.symbol('NEOUSDT.P', ''       , group = g8), sym139 = input.symbol('OXTUSDT.P', ''       , group = g8), sym140 = input.symbol('ARKUSDT.P', ''       , group = g8)
sym141 = input.symbol('XTZUSDT.P', ''       , group = g8), sym142 = input.symbol('SFPUSDT.P', ''       , group = g8), sym143 = input.symbol('MINAUSDT.P', ''      , group = g8), sym144 = input.symbol('CTSIUSDT.P', ''      , group = g8)
sym145 = input.symbol('ALPHAUSDT.P', ''     , group = g8), sym146 = input.symbol('JASMYUSDT.P', ''     , group = g8), sym147 = input.symbol('ACHUSDT.P', ''       , group = g8), sym148 = input.symbol('CELRUSDT.P', ''      , group = g8)
sym149 = input.symbol('ANKRUSDT.P', ''      , group = g8), sym150 = input.symbol('ONEUSDT.P', ''       , group = g8), sym151 = input.symbol('GLMRUSDT.P', ''      , group = g8), sym152 = input.symbol('BICOUSDT.P', ''      , group = g8)
sym153 = input.symbol('GALUSDT.P', ''       , group = g8), sym154 = input.symbol('ARPAUSDT.P', ''      , group = g8), sym155 = input.symbol('XVSUSDT.P', ''       , group = g8), sym156 = input.symbol('COTIUSDT.P', ''      , group = g8)
sym157 = input.symbol('DASHUSDT.P', ''      , group = g8), sym158 = input.symbol('HOTUSDT.P', ''       , group = g8), sym159 = input.symbol('TUSDT.P', ''         , group = g8), sym160 = input.symbol('ANTUSDT.P', ''       , group = g8)
// User inputs - Group 5        
sym161 = input.symbol('ATAUSDT.P', ''       , group = g9), sym162 = input.symbol('ALICEUSDT.P', ''     , group = g9), sym163 = input.symbol('ONTUSDT.P', ''       , group = g9), sym164 = input.symbol('ZENUSDT.P', ''       , group = g9)
sym165 = input.symbol('SSVUSDT.P', ''       , group = g9), sym166 = input.symbol('DODOXUSDT.P', ''     , group = g9), sym167 = input.symbol('KLAYUSDT.P', ''      , group = g9), sym168 = input.symbol('DENTUSDT.P', ''      , group = g9)
sym169 = input.symbol('ASTRUSDT.P', ''      , group = g9), sym170 = input.symbol('DUSKUSDT.P', ''      , group = g9), sym171 = input.symbol('RSRUSDT.P', ''       , group = g9), sym172 = input.symbol('ZECUSDT.P', ''       , group = g9)
sym173 = input.symbol('IOTAUSDT.P', ''      , group = g9), sym174 = input.symbol('ARUSDT.P', ''        , group = g9), sym175 = input.symbol('XVGUSDT.P', ''       , group = g9), sym176 = input.symbol('RLCUSDT.P', ''       , group = g9)
sym177 = input.symbol('LRCUSDT.P', ''       , group = g9), sym178 = input.symbol('AUDIOUSDT.P', ''     , group = g9), sym179 = input.symbol('KSMUSDT.P', ''       , group = g9), sym180 = input.symbol('QNTUSDT.P', ''       , group = g9)
sym181 = input.symbol('RENUSDT.P', ''       , group = g9), sym182 = input.symbol('CVXUSDT.P', ''       , group = g9), sym183 = input.symbol('UMAUSDT.P', ''       , group = g9), sym184 = input.symbol('RADUSDT.P', ''       , group = g9)
sym185 = input.symbol('ZRXUSDT.P', ''       , group = g9), sym186 = input.symbol('HFTUSDT.P', ''       , group = g9), sym187 = input.symbol('BATUSDT.P', ''       , group = g9), sym188 = input.symbol('BALUSDT.P', ''       , group = g9)
sym189 = input.symbol('KEYUSDT.P', ''       , group = g9), sym190 = input.symbol('SKLUSDT.P', ''       , group = g9), sym191 = input.symbol('RVNUSDT.P', ''       , group = g9), sym192 = input.symbol('IDEXUSDT.P', ''      , group = g9)
sym193 = input.symbol('BNXUSDT.P', ''       , group = g9), sym194 = input.symbol('TLMUSDT.P', ''       , group = g9), sym195 = input.symbol('DGBUSDT.P', ''       , group = g9), sym196 = input.symbol('CTKUSDT.P', ''       , group = g9)
sym197 = input.symbol('ICXUSDT.P', ''       , group = g9), sym198 = input.symbol('BNTUSDT.P', ''       , group = g9), sym199 = input.symbol('CKBUSDT.P', ''       , group = g9), sym200 = input.symbol('DEFIUSDT.P', ''      , group = g9)    

// Assigning symbols to groups
S1  = gn=='Group 1' ? sym1  : gn=='Group 2' ? sym41  : gn=='Group 3' ? sym81   : gn=='Group 4' ? sym121 : sym161   
S2  = gn=='Group 1' ? sym2  : gn=='Group 2' ? sym42  : gn=='Group 3' ? sym82   : gn=='Group 4' ? sym122 : sym162  
S3  = gn=='Group 1' ? sym3  : gn=='Group 2' ? sym43  : gn=='Group 3' ? sym83   : gn=='Group 4' ? sym123 : sym163  
S4  = gn=='Group 1' ? sym4  : gn=='Group 2' ? sym44  : gn=='Group 3' ? sym84   : gn=='Group 4' ? sym124 : sym164  
S5  = gn=='Group 1' ? sym5  : gn=='Group 2' ? sym45  : gn=='Group 3' ? sym85   : gn=='Group 4' ? sym125 : sym165  
S6  = gn=='Group 1' ? sym6  : gn=='Group 2' ? sym46  : gn=='Group 3' ? sym86   : gn=='Group 4' ? sym126 : sym166  
S7  = gn=='Group 1' ? sym7  : gn=='Group 2' ? sym47  : gn=='Group 3' ? sym87   : gn=='Group 4' ? sym127 : sym167  
S8  = gn=='Group 1' ? sym8  : gn=='Group 2' ? sym48  : gn=='Group 3' ? sym88   : gn=='Group 4' ? sym128 : sym168  
S9  = gn=='Group 1' ? sym9  : gn=='Group 2' ? sym49  : gn=='Group 3' ? sym89   : gn=='Group 4' ? sym129 : sym169  
S10 = gn=='Group 1' ? sym10 : gn=='Group 2' ? sym50  : gn=='Group 3' ? sym90   : gn=='Group 4' ? sym130 : sym170  
S11 = gn=='Group 1' ? sym11 : gn=='Group 2' ? sym51  : gn=='Group 3' ? sym91   : gn=='Group 4' ? sym131 : sym171  
S12 = gn=='Group 1' ? sym12 : gn=='Group 2' ? sym52  : gn=='Group 3' ? sym92   : gn=='Group 4' ? sym132 : sym172  
S13 = gn=='Group 1' ? sym13 : gn=='Group 2' ? sym53  : gn=='Group 3' ? sym93   : gn=='Group 4' ? sym133 : sym173  
S14 = gn=='Group 1' ? sym14 : gn=='Group 2' ? sym54  : gn=='Group 3' ? sym94   : gn=='Group 4' ? sym134 : sym174  
S15 = gn=='Group 1' ? sym15 : gn=='Group 2' ? sym55  : gn=='Group 3' ? sym95   : gn=='Group 4' ? sym135 : sym175  
S16 = gn=='Group 1' ? sym16 : gn=='Group 2' ? sym56  : gn=='Group 3' ? sym96   : gn=='Group 4' ? sym136 : sym176  
S17 = gn=='Group 1' ? sym17 : gn=='Group 2' ? sym57  : gn=='Group 3' ? sym97   : gn=='Group 4' ? sym137 : sym177  
S18 = gn=='Group 1' ? sym18 : gn=='Group 2' ? sym58  : gn=='Group 3' ? sym98   : gn=='Group 4' ? sym138 : sym178  
S19 = gn=='Group 1' ? sym19 : gn=='Group 2' ? sym59  : gn=='Group 3' ? sym99   : gn=='Group 4' ? sym139 : sym179  
S20 = gn=='Group 1' ? sym20 : gn=='Group 2' ? sym60  : gn=='Group 3' ? sym100  : gn=='Group 4' ? sym140 : sym180  
S21 = gn=='Group 1' ? sym21 : gn=='Group 2' ? sym61  : gn=='Group 3' ? sym101  : gn=='Group 4' ? sym141 : sym181  
S22 = gn=='Group 1' ? sym22 : gn=='Group 2' ? sym62  : gn=='Group 3' ? sym102  : gn=='Group 4' ? sym142 : sym182  
S23 = gn=='Group 1' ? sym23 : gn=='Group 2' ? sym63  : gn=='Group 3' ? sym103  : gn=='Group 4' ? sym143 : sym183  
S24 = gn=='Group 1' ? sym24 : gn=='Group 2' ? sym64  : gn=='Group 3' ? sym104  : gn=='Group 4' ? sym144 : sym184  
S25 = gn=='Group 1' ? sym25 : gn=='Group 2' ? sym65  : gn=='Group 3' ? sym105  : gn=='Group 4' ? sym145 : sym185  
S26 = gn=='Group 1' ? sym26 : gn=='Group 2' ? sym66  : gn=='Group 3' ? sym106  : gn=='Group 4' ? sym146 : sym186  
S27 = gn=='Group 1' ? sym27 : gn=='Group 2' ? sym67  : gn=='Group 3' ? sym107  : gn=='Group 4' ? sym147 : sym187  
S28 = gn=='Group 1' ? sym28 : gn=='Group 2' ? sym68  : gn=='Group 3' ? sym108  : gn=='Group 4' ? sym148 : sym188  
S29 = gn=='Group 1' ? sym29 : gn=='Group 2' ? sym69  : gn=='Group 3' ? sym109  : gn=='Group 4' ? sym149 : sym189  
S30 = gn=='Group 1' ? sym30 : gn=='Group 2' ? sym70  : gn=='Group 3' ? sym110  : gn=='Group 4' ? sym150 : sym190  
S31 = gn=='Group 1' ? sym31 : gn=='Group 2' ? sym71  : gn=='Group 3' ? sym111  : gn=='Group 4' ? sym151 : sym191  
S32 = gn=='Group 1' ? sym32 : gn=='Group 2' ? sym72  : gn=='Group 3' ? sym112  : gn=='Group 4' ? sym152 : sym192  
S33 = gn=='Group 1' ? sym33 : gn=='Group 2' ? sym73  : gn=='Group 3' ? sym113  : gn=='Group 4' ? sym153 : sym193  
S34 = gn=='Group 1' ? sym34 : gn=='Group 2' ? sym74  : gn=='Group 3' ? sym114  : gn=='Group 4' ? sym154 : sym194  
S35 = gn=='Group 1' ? sym35 : gn=='Group 2' ? sym75  : gn=='Group 3' ? sym115  : gn=='Group 4' ? sym155 : sym195  
S36 = gn=='Group 1' ? sym36 : gn=='Group 2' ? sym76  : gn=='Group 3' ? sym116  : gn=='Group 4' ? sym156 : sym196  
S37 = gn=='Group 1' ? sym37 : gn=='Group 2' ? sym77  : gn=='Group 3' ? sym117  : gn=='Group 4' ? sym157 : sym197  
S38 = gn=='Group 1' ? sym38 : gn=='Group 2' ? sym78  : gn=='Group 3' ? sym118  : gn=='Group 4' ? sym158 : sym198  
S39 = gn=='Group 1' ? sym39 : gn=='Group 2' ? sym79  : gn=='Group 3' ? sym119  : gn=='Group 4' ? sym159 : sym199  
S40 = gn=='Group 1' ? sym40 : gn=='Group 2' ? sym80  : gn=='Group 3' ? sym120  : gn=='Group 4' ? sym160 : sym200 

// Assigning colors to palettes
C1  = pl=='Palette 1' ? #a0e4c0 : pl=='Palette 2' ? #8DD3C7 : pl=='Palette 3' ? #1F77B4 : pl=='Palette 4' ? #D32F2F : #007BFF
C2  = pl=='Palette 1' ? #7d8bae : pl=='Palette 2' ? #FFFFB3 : pl=='Palette 3' ? #FF7F0E : pl=='Palette 4' ? #1976D2 : #1C56AC
C3  = pl=='Palette 1' ? #e5857b : pl=='Palette 2' ? #BEBADA : pl=='Palette 3' ? #2CA02C : pl=='Palette 4' ? #388E3C : #0082CC
C4  = pl=='Palette 1' ? #f1b2b2 : pl=='Palette 2' ? #FB8072 : pl=='Palette 3' ? #D62728 : pl=='Palette 4' ? #FBC02D : #3D8DD9
C5  = pl=='Palette 1' ? #e8ccc7 : pl=='Palette 2' ? #80B1D3 : pl=='Palette 3' ? #9467BD : pl=='Palette 4' ? #8E24AA : #0066B2
C6  = pl=='Palette 1' ? #506488 : pl=='Palette 2' ? #FDB462 : pl=='Palette 3' ? #8C564B : pl=='Palette 4' ? #E64A19 : #3498DB
C7  = pl=='Palette 1' ? #849bb1 : pl=='Palette 2' ? #B3DE69 : pl=='Palette 3' ? #E377C2 : pl=='Palette 4' ? #0288D1 : #0099E5
C8  = pl=='Palette 1' ? #e89c8f : pl=='Palette 2' ? #FCCDD3 : pl=='Palette 3' ? #7F7F7F : pl=='Palette 4' ? #7B1FA2 : #57A8FF
C9  = pl=='Palette 1' ? #f3c3c3 : pl=='Palette 2' ? #D9D9D9 : pl=='Palette 3' ? #BCBD22 : pl=='Palette 4' ? #C2185B : #6BBBFF
C10 = pl=='Palette 1' ? #eddbd2 : pl=='Palette 2' ? #BC80BD : pl=='Palette 3' ? #17BECF : pl=='Palette 4' ? #7C4DFF : #4C8DBE
C11 = pl=='Palette 1' ? #5b7085 : pl=='Palette 2' ? #CCEDC5 : pl=='Palette 3' ? #9EDAE5 : pl=='Palette 4' ? #00796B : #7EBFFF
C12 = pl=='Palette 1' ? #8badb5 : pl=='Palette 2' ? #DEDAC4 : pl=='Palette 3' ? #FFBB78 : pl=='Palette 4' ? #D35400 : #53A0D1
C13 = pl=='Palette 1' ? #d17584 : pl=='Palette 2' ? #FED9A6 : pl=='Palette 3' ? #98DF8A : pl=='Palette 4' ? #CDDC39 : #80CAFF
C14 = pl=='Palette 1' ? #f4c8c5 : pl=='Palette 2' ? #FFED6F : pl=='Palette 3' ? #D6616B : pl=='Palette 4' ? #9E9E9E : #4694BF
C15 = pl=='Palette 1' ? #ddd8da : pl=='Palette 2' ? #98DF8A : pl=='Palette 3' ? #C5B0D5 : pl=='Palette 4' ? #607D8B : #8ED5FF
C16 = pl=='Palette 1' ? #667c91 : pl=='Palette 2' ? #9EDAE5 : pl=='Palette 3' ? #C49C94 : pl=='Palette 4' ? #8BC34A : #3E86AD
C17 = pl=='Palette 1' ? #92c0b8 : pl=='Palette 2' ? #D53E4F : pl=='Palette 3' ? #F7B6D2 : pl=='Palette 4' ? #795548 : #9CE0FF
C18 = pl=='Palette 1' ? #be6e8d : pl=='Palette 2' ? #F46D43 : pl=='Palette 3' ? #C7C7C7 : pl=='Palette 4' ? #5D4037 : #3A79A1
C19 = pl=='Palette 1' ? #f7dde0 : pl=='Palette 2' ? #FDA861 : pl=='Palette 3' ? #DBDB8D : pl=='Palette 4' ? #9C27B0 : #B2E9FF
C20 = pl=='Palette 1' ? #d4d9dd : pl=='Palette 2' ? #ABDDA4 : pl=='Palette 3' ? #9EDAE5 : pl=='Palette 4' ? #FFEB3B : #316595
C21 = pl=='Palette 1' ? #70878e : pl=='Palette 2' ? #80B1D3 : pl=='Palette 3' ? #AEC7E8 : pl=='Palette 4' ? #00BCD4 : #C4F1FF
C22 = pl=='Palette 1' ? #99d2bc : pl=='Palette 2' ? #FDB462 : pl=='Palette 3' ? #FF9896 : pl=='Palette 4' ? #03A9F4 : #295089
C23 = pl=='Palette 1' ? #aa688b : pl=='Palette 2' ? #B3DE69 : pl=='Palette 3' ? #C5B0D5 : pl=='Palette 4' ? #E91E63 : #D6FAFF
C24 = pl=='Palette 1' ? #fad2e3 : pl=='Palette 2' ? #FCCDD3 : pl=='Palette 3' ? #C49C94 : pl=='Palette 4' ? #FF5722 : #20477D
C25 = pl=='Palette 1' ? #ccd3d9 : pl=='Palette 2' ? #D9D9D9 : pl=='Palette 3' ? #F7B6D2 : pl=='Palette 4' ? #4CAF50 : #E6FFFF
C26 = pl=='Palette 1' ? #7c938a : pl=='Palette 2' ? #BC80BD : pl=='Palette 3' ? #7FCDBB : pl=='Palette 4' ? #3F51B5 : #1E66A8
C27 = pl=='Palette 1' ? #a0a2e4 : pl=='Palette 2' ? #CCEDC5 : pl=='Palette 3' ? #D9D9D9 : pl=='Palette 4' ? #FFC107 : #A1D7FF
C28 = pl=='Palette 1' ? #966398 : pl=='Palette 2' ? #DEDAC4 : pl=='Palette 3' ? #DB8D41 : pl=='Palette 4' ? #FF9800 : #2773A3
C29 = pl=='Palette 1' ? #fbe7ec : pl=='Palette 2' ? #FED9A6 : pl=='Palette 3' ? #1CAE78 : pl=='Palette 4' ? #673AB7 : #ACE3FF
C30 = pl=='Palette 1' ? #e3ecf0 : pl=='Palette 2' ? #FFED6F : pl=='Palette 3' ? #AD494A : pl=='Palette 4' ? #212121 : #2E7FAD
C31 = pl=='Palette 1' ? #b4e1d1 : pl=='Palette 2' ? #8CAFCB : pl=='Palette 3' ? #8C6D31 : pl=='Palette 4' ? #BDBDBD : #B6EEFF
C32 = pl=='Palette 1' ? #6a82a3 : pl=='Palette 2' ? #C994C7 : pl=='Palette 3' ? #E7969C : pl=='Palette 4' ? #76FF03 : #257C99
C33 = pl=='Palette 1' ? #f09f9b : pl=='Palette 2' ? #E5F5E0 : pl=='Palette 3' ? #7B4173 : pl=='Palette 4' ? #00E5FF : #BEDFFF
C34 = pl=='Palette 1' ? #f9c9ca : pl=='Palette 2' ? #67A9CF : pl=='Palette 3' ? #A55194 : pl=='Palette 4' ? #FF1744 : #21708E
C35 = pl=='Palette 1' ? #e5d6d2 : pl=='Palette 2' ? #E31A1C : pl=='Palette 3' ? #8C6D31 : pl=='Palette 4' ? #651FFF : #C6E9FF
C36 = pl=='Palette 1' ? #455a6f : pl=='Palette 2' ? #74C476 : pl=='Palette 3' ? #B5CF6B : pl=='Palette 4' ? #FF9100 : #1D6380
C37 = pl=='Palette 1' ? #7694a1 : pl=='Palette 2' ? #F768A1 : pl=='Palette 3' ? #CE6DBD : pl=='Palette 4' ? #8D6E63 : #D1F4FF
C38 = pl=='Palette 1' ? #df8980 : pl=='Palette 2' ? #7FCDBB : pl=='Palette 3' ? #BD9E39 : pl=='Palette 4' ? #4E342E : #186573
C39 = pl=='Palette 1' ? #f5bcbc : pl=='Palette 2' ? #9E9AC8 : pl=='Palette 3' ? #6B6ECF : pl=='Palette 4' ? #1B5E20 : #DBFEFF
C40 = pl=='Palette 1' ? #c9cfcd : pl=='Palette 2' ? #FEB24C : pl=='Palette 3' ? #D6616B : pl=='Palette 4' ? #01579B : #155C6B

debugtitle = input.bool(false, 'Fix Ticker Titling')

// Creating symbol and color arrays
syms = array.new_string()
cols = array.new_color()

// Pushing symbols into an array
array.push(syms, S1 ), array.push(syms, S2 ), array.push(syms, S3 ), array.push(syms, S4 ), array.push(syms, S5 )
array.push(syms, S6 ), array.push(syms, S7 ), array.push(syms, S8 ), array.push(syms, S9 ), array.push(syms, S10)
array.push(syms, S11), array.push(syms, S12), array.push(syms, S13), array.push(syms, S14), array.push(syms, S15)
array.push(syms, S16), array.push(syms, S17), array.push(syms, S18), array.push(syms, S19), array.push(syms, S20)
array.push(syms, S21), array.push(syms, S22), array.push(syms, S23), array.push(syms, S24), array.push(syms, S25)
array.push(syms, S26), array.push(syms, S27), array.push(syms, S28), array.push(syms, S29), array.push(syms, S30)
array.push(syms, S31), array.push(syms, S32), array.push(syms, S33), array.push(syms, S34), array.push(syms, S35)
array.push(syms, S36), array.push(syms, S37), array.push(syms, S38), array.push(syms, S39), array.push(syms, S40)

// Pushing colors into an array
array.push(cols, C1 ), array.push(cols, C2 ), array.push(cols, C3 ), array.push(cols, C4)
array.push(cols, C5 ), array.push(cols, C6 ), array.push(cols, C7 ), array.push(cols, C8)
array.push(cols, C9 ), array.push(cols, C10), array.push(cols, C11), array.push(cols, C12)
array.push(cols, C13), array.push(cols, C14), array.push(cols, C15), array.push(cols, C16)
array.push(cols, C17), array.push(cols, C18), array.push(cols, C19), array.push(cols, C20)
array.push(cols, C21), array.push(cols, C22), array.push(cols, C23), array.push(cols, C24)
array.push(cols, C25), array.push(cols, C26), array.push(cols, C27), array.push(cols, C28)
array.push(cols, C29), array.push(cols, C30), array.push(cols, C31), array.push(cols, C32)
array.push(cols, C33), array.push(cols, C34), array.push(cols, C35), array.push(cols, C36)
array.push(cols, C37), array.push(cols, C38), array.push(cols, C39), array.push(cols, C40)

// Function for calculating the Z-Score
f_zscore(src) =>
    mean    = meantype=='SMA' ? ta.sma(src, zlen) : ta.ema(src, zlen)
    std_dev = ta.stdev(src, zlen)
    z_score = (src - mean) / std_dev

// Function for encoding price + volume data (a trick to overcome max 40 security call limit)
encode_data(closeVal, volumeVal) =>
    closeStr    = na(closeVal)  ? "na" : str.tostring(closeVal)
    volumeStr   = na(volumeVal) ? "na" : str.tostring(volumeVal)
    encodedData = closeStr + "|" + volumeStr
    encodedData

// Function for decoding price data
decode_close(encodedData) =>
    data = str.split(encodedData, "|")
    float closeVal = na
    if array.size(data) > 0
        for i = 0 to array.size(data)-1
            if na(closeVal)
                closeVal := str.tonumber(array.get(data, i))
            else
                closeVal := closeVal, str.tonumber(array.get(data, i))
    else
        closeVal := na
    closeVal

// Function for decoding volume data
decode_volume(encodedData) =>
    data = str.split(encodedData, "|")
    float volumeVal = na
    if array.size(data) > 1
        val = str.tonumber(array.get(data, 1))
        if not na(val)
            volumeVal := val
    volumeVal

// Function for fetching data
f_data(sym, t) =>
    vol          = voltype=='VOLUME' ? ta.sma(volume, 2) : ta.obv
    combinedData = request.security(sym, timeframe.period, encode_data(close, vol))
    dta          = t == 0 ? decode_close(combinedData) : decode_volume(combinedData)
    out          = ztype=='Current Z-Score' or ztype=='Relative Z-Score' ? f_zscore(dta) : ta.sma(f_zscore(dta), azlen)
    out > 4 ? 4 : out < -4 ? -4 : out

// Creating arrays for z-score values
Ps = array.new_float(40)
Vs = array.new_float(40)

// Fetching price and volume z-score data and pushing it into arrays
f_pushdata(index, p, v) =>
    array.set(Ps, index, p)
    array.set(Vs, index, v)

P1  = f_data(S1 , 0), V1  = f_data(S1 , 1), f_pushdata(0, P1 , V1  ), P2  = f_data(S2 , 0), V2  = f_data(S2 , 1), f_pushdata(1, P2 , V2 )
P3  = f_data(S3 , 0), V3  = f_data(S3 , 1), f_pushdata(2, P3 , V3  ), P4  = f_data(S4 , 0), V4  = f_data(S4 , 1), f_pushdata(3, P4 , V4 )
P5  = f_data(S5 , 0), V5  = f_data(S5 , 1), f_pushdata(4, P5 , V5  ), P6  = f_data(S6 , 0), V6  = f_data(S6 , 1), f_pushdata(5, P6 , V6 )
P7  = f_data(S7 , 0), V7  = f_data(S7 , 1), f_pushdata(6, P7 , V7  ), P8  = f_data(S8 , 0), V8  = f_data(S8 , 1), f_pushdata(7, P8 , V8 )
P9  = f_data(S9 , 0), V9  = f_data(S9 , 1), f_pushdata(8, P9 , V9  ), P10 = f_data(S10, 0), V10 = f_data(S10, 1), f_pushdata(9, P10, V10)
P11 = f_data(S11, 0), V11 = f_data(S11, 1), f_pushdata(10, P11, V11), P12 = f_data(S12, 0), V12 = f_data(S12, 1), f_pushdata(11, P12, V12)
P13 = f_data(S13, 0), V13 = f_data(S13, 1), f_pushdata(12, P13, V13), P14 = f_data(S14, 0), V14 = f_data(S14, 1), f_pushdata(13, P14, V14)
P15 = f_data(S15, 0), V15 = f_data(S15, 1), f_pushdata(14, P15, V15), P16 = f_data(S16, 0), V16 = f_data(S16, 1), f_pushdata(15, P16, V16)
P17 = f_data(S17, 0), V17 = f_data(S17, 1), f_pushdata(16, P17, V17), P18 = f_data(S18, 0), V18 = f_data(S18, 1), f_pushdata(17, P18, V18)
P19 = f_data(S19, 0), V19 = f_data(S19, 1), f_pushdata(18, P19, V19), P20 = f_data(S20, 0), V20 = f_data(S20, 1), f_pushdata(19, P20, V20)
P21 = f_data(S21, 0), V21 = f_data(S21, 1), f_pushdata(20, P21, V21), P22 = f_data(S22, 0), V22 = f_data(S22, 1), f_pushdata(21, P22, V22)
P23 = f_data(S23, 0), V23 = f_data(S23, 1), f_pushdata(22, P23, V23), P24 = f_data(S24, 0), V24 = f_data(S24, 1), f_pushdata(23, P24, V24)
P25 = f_data(S25, 0), V25 = f_data(S25, 1), f_pushdata(24, P25, V25), P26 = f_data(S26, 0), V26 = f_data(S26, 1), f_pushdata(25, P26, V26)
P27 = f_data(S27, 0), V27 = f_data(S27, 1), f_pushdata(26, P27, V27), P28 = f_data(S28, 0), V28 = f_data(S28, 1), f_pushdata(27, P28, V28)
P29 = f_data(S29, 0), V29 = f_data(S29, 1), f_pushdata(28, P29, V29), P30 = f_data(S30, 0), V30 = f_data(S30, 1), f_pushdata(29, P30, V30)
P31 = f_data(S31, 0), V31 = f_data(S31, 1), f_pushdata(30, P31, V31), P32 = f_data(S32, 0), V32 = f_data(S32, 1), f_pushdata(31, P32, V32)
P33 = f_data(S33, 0), V33 = f_data(S33, 1), f_pushdata(32, P33, V33), P34 = f_data(S34, 0), V34 = f_data(S34, 1), f_pushdata(33, P34, V34)
P35 = f_data(S35, 0), V35 = f_data(S35, 1), f_pushdata(34, P35, V35), P36 = f_data(S36, 0), V36 = f_data(S36, 1), f_pushdata(35, P36, V36)
P37 = f_data(S37, 0), V37 = f_data(S37, 1), f_pushdata(36, P37, V37), P38 = f_data(S38, 0), V38 = f_data(S38, 1), f_pushdata(37, P38, V38)
P39 = f_data(S39, 0), V39 = f_data(S39, 1), f_pushdata(38, P39, V39), P40 = f_data(S40, 0), V40 = f_data(S40, 1), f_pushdata(39, P40, V40)

// Relative/Second-Level Z-score
avgZscorePrice  = array.avg(Ps)
avgZscoreVolume = array.avg(Vs)
stdDevPrice     = array.stdev(Ps, false) 
stdDevVolume    = array.stdev(Vs, false)

relativeZscorePrice(P) => 
    out = (P - avgZscorePrice) / stdDevPrice
    out > 4 ? 4 : out < -4 ? -4 : out
relativeZscoreVolume(V) => 
    out = (V - avgZscoreVolume) / stdDevVolume
    out > 4 ? 4 : out < -4 ? -4 : out

// Modifying symbol names
ticker_names = array.new_string(40, na)

for i = 0 to 39
    full_ticker = array.get(syms, i)
    ticker_split = str.split(full_ticker, ":")
    short_ticker = array.get(ticker_split, 1)
    array.set(ticker_names, i, short_ticker)

// Creating an array with bar_index points
xs = array.new_int(0)
for i = 0 to 80
    array.push(xs, bar_index[i])

// Function for transforming Z-Score values into bar_index points
f_magic(value) =>
    index = 80 - int((value + 4) * 10)
    array.get(xs, index)

// Initializing labels
var label[] pLabels = array.new_label(40, na)
var label[] vLabels = array.new_label(40, na)
var label[] nLabels = array.new_label(40, na)
var label[] sLabels = array.new_label(40, na)
var box[]   pBoxes  = array.new_box(40, na)
var box[]   vBoxes  = array.new_box(40, na)

// Functions to create or update labels and boxes
createOrUpdateLabel(arr, idx, x_val, y_val, lbl_style, lbl_color, lbl_text) =>
    lbl = array.get(arr, idx)
    if (na(lbl))
        lbl := label.new(x_val, y_val, style = lbl_style, color = lbl_color, text = str.tostring(lbl_text), size = size.tiny, textcolor = lblplotscol)
        array.set(arr, idx, lbl)
    else
        label.set_x(lbl, x = x_val)

createOrUpdateBox(arr, idx, x1_val, y1_val, x2_val, y2_val, bg_color) =>
    bx = array.get(arr, idx)
    if (na(bx))
        bx := box.new(x1_val, y1_val, x2_val, y2_val, bgcolor = bg_color, border_color = chart.bg_color)
        array.set(arr, idx, bx)
    else
        box.set_left(bx, x1_val)
        box.set_right(bx, x2_val)

// Creating and updating labels and boxes
for i = 0 to 39
    yValueP   = ztype=='Relative Z-Score' ? relativeZscorePrice(array.get(Ps, i)) : array.get(Ps, i)
    yValueV   = ztype=='Relative Z-Score' ? relativeZscoreVolume(array.get(Vs, i)) : array.get(Vs, i)
    xValue    = 195 - i * 5
    PlblColor = disp == 'Columns' ? color.new(array.get(cols, i), 30) : array.get(cols, i)
    VlblColor = color.new(array.get(cols, i), trans)
    txt       = str.tostring(array.get(ticker_names, i))
    txt1      = array.get(str.split(txt, 'USD'), 0)

    if barstate.islast
        if disp == 'Circles'
            createOrUpdateLabel(pLabels, i, bar_index[xValue], yValueP, switchshape(priceshape), PlblColor, '')
            createOrUpdateLabel(nLabels, i, bar_index[xValue], -4.3, label.style_label_up, color.rgb(120, 123, 134, 100), debugtitle ? txt : txt1)
            createOrUpdateLabel(vLabels, i, bar_index[xValue], yValueV, switchshape(priceshape), VlblColor, '')
        
        if disp == 'Columns'
            createOrUpdateBox(pBoxes,    i, bar_index[xValue], yValueP, bar_index[xValue] -2, 0, PlblColor)
            createOrUpdateLabel(nLabels, i, bar_index[xValue], -4.3, label.style_label_up, color.rgb(54, 58, 69, 100), debugtitle ? txt : txt1)
            createOrUpdateBox(vBoxes,    i, bar_index[xValue] + 2, yValueV, bar_index[xValue], 0, VlblColor)

        if disp == 'Delta Columns'
            createOrUpdateBox(pBoxes,    i, bar_index[xValue] - 2, yValueP - yValueV, bar_index[xValue] + 2, 0, PlblColor)
            createOrUpdateLabel(nLabels, i, bar_index[xValue], -4.3, label.style_label_up, color.rgb(54, 58, 69, 100), debugtitle ? txt : txt1)

        if disp == 'Scatter Plot'
            cond = lblplotscond == 'Outliers' ? (yValueV > outlierstd or yValueV < -outlierstd) or (yValueP > outlierstd or yValueP < -outlierstd) : lblplotscond == 'All' ? true : false
            createOrUpdateLabel(sLabels, i, f_magic(yValueP), yValueV, switchshape(priceshape), VlblColor, cond ? (debugtitle ? txt : txt1) : '')

mainsty = line.style_solid
quadsty = line.style_dotted

// Creating chart grid and lines
hlines = array.new_line()
vlines = array.new_line()

var s = disp == 'Scatter Plot' ? 80 : 200
var b = disp == 'Scatter Plot' ? 0 : 2
var line[] horzLines = na

if (na(horzLines)) and disp!='Z-Price Heatmap' and disp!='Z-Volume Heatmap' and disp!='Z-Delta Heatmap'
    horzLines := array.new_line(9)
    array.set(horzLines, 0, line.new(bar_index[s],  0, bar_index + b,  0, color = disp == 'Scatter Plot' ? gridcol : gridcol2))
    array.set(horzLines, 1, line.new(bar_index[s], -4, bar_index + b, -4, color = disp == 'Scatter Plot' ? gridcol2 : gridcol))
    array.set(horzLines, 2, line.new(bar_index[s],  1, bar_index + b,  1, color = gridcol))
    array.set(horzLines, 3, line.new(bar_index[s],  2, bar_index + b,  2, color = gridcol))
    array.set(horzLines, 4, line.new(bar_index[s],  3, bar_index + b,  3, color = gridcol))
    array.set(horzLines, 5, line.new(bar_index[s],  4, bar_index + b,  4, color = gridcol))
    array.set(horzLines, 6, line.new(bar_index[s], -1, bar_index + b, -1, color = gridcol))
    array.set(horzLines, 7, line.new(bar_index[s], -2, bar_index + b, -2, color = gridcol))
    array.set(horzLines, 8, line.new(bar_index[s], -3, bar_index + b, -3, color = gridcol))

if (barstate.islast) and disp!='Z-Price Heatmap' and disp!='Z-Volume Heatmap' and disp!='Z-Delta Heatmap'
    for i = 0 to array.size(horzLines) - 1
        line.set_x2(array.get(horzLines, i), bar_index + b)
        line.set_x1(array.get(horzLines, i), bar_index[s])

var line[] vertLines  = na
var label[] hlaLabels = na

if (na(vertLines)) and disp=='Scatter Plot'
    vertLines := array.new_line(9)
    hlaLabels := array.new_label(9)
    
    array.set(vertLines, 0, line.new(bar_index[s], 4, bar_index[s], -4, color = color.rgb(120, 123, 134, 44)))
    array.set(hlaLabels, 0, label.new(bar_index[s], -4.3, text = '-4', textcolor = color.gray, style = label.style_label_up, color = na, size = size.tiny))
    array.set(vertLines, 1, line.new(bar_index[s - 10], 4, bar_index[s - 10], -4, style = mainsty, color = gridcol))
    array.set(hlaLabels, 1, label.new(bar_index[s - 10], -4.3, text = '-3', textcolor = color.gray, style = label.style_label_up, color = na, size = size.tiny))
    array.set(vertLines, 2, line.new(bar_index[s - 20], 4, bar_index[s - 20], -4, style = mainsty, color = gridcol))
    array.set(hlaLabels, 2, label.new(bar_index[s - 20], -4.3, text = '-2', textcolor = color.gray, style = label.style_label_up, color = na, size = size.tiny))
    array.set(vertLines, 3, line.new(bar_index[s - 30], 4, bar_index[s - 30], -4, style = mainsty, color = gridcol))
    array.set(hlaLabels, 3, label.new(bar_index[s - 30], -4.3, text = '-1', textcolor = color.gray, style = label.style_label_up, color = na, size = size.tiny))
    array.set(vertLines, 4, line.new(bar_index[s - 40], 4, bar_index[s - 40], -4, style = mainsty, color = gridcol))
    array.set(hlaLabels, 4, label.new(bar_index[s - 40], -4.3, text = '0', textcolor = color.gray, style = label.style_label_up, color = na, size = size.tiny))
    array.set(vertLines, 5, line.new(bar_index[s - 50], 4, bar_index[s - 50], -4, style = mainsty, color = gridcol))
    array.set(hlaLabels, 5, label.new(bar_index[s - 50], -4.3, text = '1', textcolor = color.gray, style = label.style_label_up, color = na, size = size.tiny))
    array.set(vertLines, 6, line.new(bar_index[s - 60], 4, bar_index[s - 60], -4, style = mainsty, color = gridcol))
    array.set(hlaLabels, 6, label.new(bar_index[s - 60], -4.3, text = '2', textcolor = color.gray, style = label.style_label_up, color = na, size = size.tiny))
    array.set(vertLines, 7, line.new(bar_index[s - 70], 4, bar_index[s - 70], -4, style = mainsty, color = gridcol))
    array.set(hlaLabels, 7, label.new(bar_index[s - 70], -4.3, text = '3', textcolor = color.gray, style = label.style_label_up, color = na, size = size.tiny))
    array.set(vertLines, 8, line.new(bar_index[s - 80], 4, bar_index[s - 80], -4, style = mainsty, color = gridcol))
    array.set(hlaLabels, 8, label.new(bar_index[s - 80], -4.3, text = '4', textcolor = color.gray, style = label.style_label_up, color = na, size = size.tiny))

if (barstate.islast) and disp=='Scatter Plot'
    for i = 0 to 8
        line.set_x1(array.get(vertLines, i), bar_index[s - i * 10])
        line.set_x2(array.get(vertLines, i), bar_index[s - i * 10])
        label.set_x(array.get(hlaLabels, i), bar_index[s - i * 10])

if showquadr and disp == 'Scatter Plot'
    q1 = box.new(f_magic(-quadr), quadr, f_magic(quadr), -quadr, quadrcol, border_style = quadsty, bgcolor = quadrbgcol, text = str.tostring(quadr) + 'σ', text_valign = text.align_top, text_size = size.normal, text_color = color.rgb(120, 123, 134, 40))
    box.delete(q1[1])
if showquadr1 and disp == 'Scatter Plot'
    q1 = box.new(f_magic(-quadr1), quadr1, f_magic(quadr1), -quadr1, quadrcol1, border_style = quadsty, bgcolor = quadrbgcol1, text = str.tostring(quadr1) + 'σ', text_valign = text.align_top, text_size = size.normal, text_color = color.rgb(120, 123, 134, 40))
    box.delete(q1[1])
if showquadr2 and disp == 'Scatter Plot'
    q1 = box.new(f_magic(-quadr2), quadr2, f_magic(quadr2), -quadr2, quadrcol2, border_style = quadsty, bgcolor = quadrbgcol2, text = str.tostring(quadr2) + 'σ', text_valign = text.align_top, text_size = size.normal, text_color = color.rgb(120, 123, 134, 40))
    box.delete(q1[1])

// Grid labels
offset                  = 0.04
var label[] valueLabels = na
var label[] guideLabels  = na
var label[] guideLabels2 = na

if (na(valueLabels)) and disp!='Z-Price Heatmap' and disp!='Z-Volume Heatmap' and disp!='Z-Delta Heatmap'
    valueLabels := array.new_label(10)
    
    array.set(valueLabels, 0, label.new(bar_index[s],  0 - offset,  text =   '0             ',    textcolor = color.gray, style = label.style_label_right, color = na, size = size.tiny))
    array.set(valueLabels, 1, label.new(bar_index[s],  1 - offset,  text =   '1             ',    textcolor = color.gray, style = label.style_label_right, color = na, size = size.tiny))
    array.set(valueLabels, 2, label.new(bar_index[s],  2 - offset,  text =   '2             ',    textcolor = color.gray, style = label.style_label_right, color = na, size = size.tiny))
    array.set(valueLabels, 3, label.new(bar_index[s],  3 - offset,  text =   '3             ',    textcolor = color.gray, style = label.style_label_right, color = na, size = size.tiny))
    array.set(valueLabels, 4, label.new(bar_index[s],  4 - offset,  text =   '4             ',    textcolor = color.gray, style = label.style_label_right, color = na, size = size.tiny))
    array.set(valueLabels, 5, label.new(bar_index[s], -1 - offset,  text =  '-1             ',    textcolor = color.gray, style = label.style_label_right, color = na, size = size.tiny))
    array.set(valueLabels, 6, label.new(bar_index[s], -2 - offset,  text =  '-2             ',    textcolor = color.gray, style = label.style_label_right, color = na, size = size.tiny))
    array.set(valueLabels, 7, label.new(bar_index[s], -3 - offset,  text =  '-3             ',    textcolor = color.gray, style = label.style_label_right, color = na, size = size.tiny))
    array.set(valueLabels, 8, label.new(bar_index[s], -4 - offset,  text =  '-4             ',    textcolor = color.gray, style = label.style_label_right, color = na, size = size.tiny))

if (barstate.islast) and disp!='Z-Price Heatmap' and disp!='Z-Volume Heatmap' and disp!='Z-Delta Heatmap'
    for i = 0 to 9
        label.set_x(array.get(valueLabels, i), bar_index[s])

if helperlab and (na(guideLabels)) and (na(guideLabels)) and disp=='Scatter Plot'
    guideLabels := array.new_label(2)
    guideLabels2 := array.new_label(2)
    array.set(guideLabels,  0, label.new(bar_index[s +4], -4.5 - offset,  text =  'Low Price - Low Volume',    textcolor = color.gray, style = label.style_label_right, color = #787b8624, size = size.small))
    array.set(guideLabels,  1, label.new(bar_index[s +4],  4.5 - offset,  text =  'Low Price - High Volume',   textcolor = color.gray, style = label.style_label_right, color = #787b8624, size = size.small))
    array.set(guideLabels2, 0, label.new(bar_index + 4,   -4.5 - offset,  text =  'High Price - Low Volume',  textcolor = color.gray, style = label.style_label_left,  color = #787b8624, size = size.small))
    array.set(guideLabels2, 1, label.new(bar_index + 4,    4.5 - offset,  text =  'High Price - High Volume',   textcolor = color.gray, style = label.style_label_left,  color = #787b8624, size = size.small))

if helperlab and barstate.islast and disp=='Scatter Plot'
    for i = 0 to 1
        label.set_x(array.get(guideLabels, i), bar_index[s + 4])
    for i = 0 to 1
        label.set_x(array.get(guideLabels2, i), bar_index + 4)

// Creating the title
table1 = table.new(position.top_center, 5,5)
if showtitle
    table.cell(table1, 0, 0, 'Price x Volume Z-Score: ' + str.tostring(disp), text_size = size.normal, text_color = color.gray)
    table.cell(table1, 0, 1, '• Period: ' + str.tostring(zlen) + '    ' + '• Timeframe: ' + str.tostring(tf=='' ? (timeframe.period + (timeframe.isminutes ? 'm' : '')) : tf) + '    • Data: ' + str.tostring(ztype), text_size = size.tiny, text_color = color.gray)

// Creating heatmap grid
f_vertoffs(n) =>
    hmpos=='1' ? n : hmpos=='2' ? -n : hmpos=='3' ? (-1*n - n) : hmpos=='4' ? (-2*n - n) : (-3*n - n)

h0  = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(40) : na,  display = display.none, linewidth = 0, editable = false)
h1  = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(39) : na,  display = display.none, linewidth = 0, editable = false)
h2  = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(38) : na,  display = display.none, linewidth = 0, editable = false)
h3  = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(37) : na,  display = display.none, linewidth = 0, editable = false)
h4  = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(36) : na,  display = display.none, linewidth = 0, editable = false)
h5  = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(35) : na,  display = display.none, linewidth = 0, editable = false)
h6  = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(34) : na,  display = display.none, linewidth = 0, editable = false)
h7  = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(33) : na,  display = display.none, linewidth = 0, editable = false)
h8  = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(32) : na,  display = display.none, linewidth = 0, editable = false)
h9  = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(31) : na,  display = display.none, linewidth = 0, editable = false)
h10 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(30) : na,  display = display.none, linewidth = 0, editable = false)
h11 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(29) : na,  display = display.none, linewidth = 0, editable = false)
h12 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(28) : na,  display = display.none, linewidth = 0, editable = false)
h13 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(27) : na,  display = display.none, linewidth = 0, editable = false)
h14 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(26) : na,  display = display.none, linewidth = 0, editable = false)
h15 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(25) : na,  display = display.none, linewidth = 0, editable = false)
h16 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(24) : na,  display = display.none, linewidth = 0, editable = false)
h17 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(23) : na,  display = display.none, linewidth = 0, editable = false)
h18 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(22) : na,  display = display.none, linewidth = 0, editable = false)
h19 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(21) : na,  display = display.none, linewidth = 0, editable = false)
h20 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(20) : na,  display = display.none, linewidth = 0, editable = false)
h21 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(19) : na,  display = display.none, linewidth = 0, editable = false)
h22 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(18) : na,  display = display.none, linewidth = 0, editable = false)
h23 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(17) : na,  display = display.none, linewidth = 0, editable = false)
h24 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(16) : na,  display = display.none, linewidth = 0, editable = false)
h25 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(15) : na,  display = display.none, linewidth = 0, editable = false)
h26 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(14) : na,  display = display.none, linewidth = 0, editable = false)
h27 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(13) : na,  display = display.none, linewidth = 0, editable = false)
h28 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(12) : na,  display = display.none, linewidth = 0, editable = false)
h29 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(11) : na,  display = display.none, linewidth = 0, editable = false)
h30 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(10) : na,  display = display.none, linewidth = 0, editable = false)
h31 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(9 ) : na,  display = display.none, linewidth = 0, editable = false)
h32 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(8 ) : na,  display = display.none, linewidth = 0, editable = false)
h33 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(7 ) : na,  display = display.none, linewidth = 0, editable = false)
h34 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(6 ) : na,  display = display.none, linewidth = 0, editable = false)
h35 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(5 ) : na,  display = display.none, linewidth = 0, editable = false)
h36 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(4 ) : na,  display = display.none, linewidth = 0, editable = false)
h37 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(3 ) : na,  display = display.none, linewidth = 0, editable = false)
h38 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(2 ) : na,  display = display.none, linewidth = 0, editable = false)
h39 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(1 ) : na,  display = display.none, linewidth = 0, editable = false)
h40 = hline(disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? f_vertoffs(0 ) : na,  display = display.none, linewidth = 0, editable = false)

// Function for calculating heatmap color-coding
f_hmcol(P, V) =>
    Px     = disp=='Z-Price Heatmap' ? (ztype=='Relative Z-Score' ? relativeZscorePrice(P) : P) : disp=='Z-Volume Heatmap' ? (ztype=='Relative Z-Score' ? relativeZscoreVolume(V) : V) : (ztype=='Relative Z-Score' ? (relativeZscorePrice(P) - relativeZscoreVolume(V)) : (P - V))
    hot    = color.from_gradient(Px,  0,  2, colh3, colh2) 
    vhot   = color.from_gradient(Px,  2,  4, colh2, colh1) 
    cold   = color.from_gradient(Px, -2,  0, colc2, colc3) 
    vcold  = color.from_gradient(Px, -4, -2, colc1, colc2) 
    out1   = Px == 0 ? colc3 : Px>0 and Px<2 ? hot : Px>2 ? vhot : Px<0 and Px>-2 ? cold : Px<-2 ? vcold : chart.bg_color
    out2   = color.from_gradient(Px, -4, 4, colcc, colhh)
    out    = hmcoltype=='Two-Color' ? out2 : out1

// Filling the grid with color-coded colors
fill(h0,  h1,  f_hmcol(P1 , V1 ))
fill(h1,  h2,  f_hmcol(P2 , V2 ))
fill(h2,  h3,  f_hmcol(P3 , V3 ))
fill(h3,  h4,  f_hmcol(P4 , V4 ))
fill(h4,  h5,  f_hmcol(P5 , V5 ))
fill(h5,  h6,  f_hmcol(P6 , V6 ))
fill(h6,  h7,  f_hmcol(P7 , V7 ))
fill(h7,  h8,  f_hmcol(P8 , V8 ))
fill(h8,  h9,  f_hmcol(P9 , V9 ))
fill(h9,  h10, f_hmcol(P10, V10))
fill(h10, h11, f_hmcol(P11, V11))
fill(h11, h12, f_hmcol(P12, V12))
fill(h12, h13, f_hmcol(P13, V13))
fill(h13, h14, f_hmcol(P14, V14))
fill(h14, h15, f_hmcol(P15, V15))
fill(h15, h16, f_hmcol(P16, V16))
fill(h16, h17, f_hmcol(P17, V17))
fill(h17, h18, f_hmcol(P18, V18))
fill(h18, h19, f_hmcol(P19, V19))
fill(h19, h20, f_hmcol(P20, V20))
fill(h20, h21, f_hmcol(P21, V21))
fill(h21, h22, f_hmcol(P22, V22))
fill(h22, h23, f_hmcol(P23, V23))
fill(h23, h24, f_hmcol(P24, V24))
fill(h24, h25, f_hmcol(P25, V25))
fill(h25, h26, f_hmcol(P26, V26))
fill(h26, h27, f_hmcol(P27, V27))
fill(h27, h28, f_hmcol(P28, V28))
fill(h28, h29, f_hmcol(P29, V29))
fill(h29, h30, f_hmcol(P30, V30))
fill(h30, h31, f_hmcol(P31, V31))
fill(h31, h32, f_hmcol(P32, V32))
fill(h32, h33, f_hmcol(P33, V33))
fill(h33, h34, f_hmcol(P34, V34))
fill(h34, h35, f_hmcol(P35, V35))
fill(h35, h36, f_hmcol(P36, V36))
fill(h36, h37, f_hmcol(P37, V37))
fill(h37, h38, f_hmcol(P38, V38))
fill(h38, h39, f_hmcol(P39, V39))
fill(h39, h40, f_hmcol(P40, V40))

// Creating heatmap symbol labels
var label[] tickerLabels = na
if disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap'
    if (na(tickerLabels))
        tickerLabels := array.new_label(40)
        
        for i = 0 to 39
            y_position = 39.5 - i
            y_position := f_vertoffs(y_position)
            txt       = str.tostring(array.get(ticker_names, i))
            txt1      = array.get(str.split(txt, 'USD'), 0)
            array.set(tickerLabels, i, label.new(bar_index + 1, y_position - offset * 1, text = debugtitle ? txt : txt1, xloc = xloc.bar_index, style = label.style_label_left, color = color.rgb(255, 235, 59, 100), textcolor = color.gray, size = size.small, textalign = text.align_left))
    else
        for i = 0 to 39
            lbl = array.get(tickerLabels, i)
            label.set_x(lbl, bar_index + 1)

// Scaling anchor
scaleanchorup   = disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? na : 5.3
scaleanchordown = disp=='Z-Price Heatmap' or disp=='Z-Volume Heatmap' or disp=='Z-Delta Heatmap' ? na : -5.3

plot(scaleanchorup,   display = display.pane, color = na, editable = false)
plot(scaleanchordown, display = display.pane, color = na, editable = false)


    