Everything Bitcoin [Kioseff Trading]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KioseffTrading

//@version=5
indicator("Everything Bitcoin [Kioseff Trading]", overlay = true, max_labels_count = 500,  max_boxes_count = 500,  max_lines_count = 500, max_bars_back = 500)

TFdata                    = input.string(defval = "Close", title = "Low TF Data", 

     options = 
     
     [
    
     "Close",
     "High", 
     "Low", 
     "Open", 
     "HL2", 
     "HLC3", 
     "OHLC4", 
     "HLCC4", 
     "Volume", 
     "RSI", 
     "EMA 20", 
     "%b", 
     "Stochastic", 
     "VWAP"
    
     ]
     )



TF                           = input.string(defval = "1 Minute", title = "TF Chart Period", 

     options = 

     [
     
     "1 Minute", 
     "3 Minute", 
     "5 Minute",
     "15 Minute", 
     "30 Minute", 
     "45 Minute", 
     "1 Hour", 
     "2 Hour", 
     "3 Hour", 
     "4 Hour",
     "Custom"
     
     ]
     )

custom                        = input.int(defval = 1, minval = 1, maxval = 1440, 
                                 title = "Custom Timeframe Interval (Minutes)", 
                                 tooltip = 'This setting activates when "TF Chart Period" is set to "Custom". Inactive otherwsie.')
show                          = input.string(defval = "Yes", title = "Show Floating BTC Table?", options = ["Yes", "No", "All Data In Stat Table"])
show1                         = input.string(defval = "Yes", title = "Show BTC Stat Table?", options = ["Yes", "No"])
show2                         = input.string(defval = "Yes", title = "Show Regression Channel?", options = ["Yes", "No"])

trueTF = switch TF
    
    "1 Minute"  => "1"
    "3 Minute"  => "3"
    "5 Minute"  => "5"
    "15 Minute" => "15"
    "30 Minute" => "30"
    "45 Minute" => "45"
    "1 Hour"    => "60"
    "2 Hour"    => "120"
    "3 Hour"    => "180"
    "4 Hour"    => "240"
    "Custom"    => str.tostring(custom)


tablePos                        = input.string(defval = "Bottom Right", title = "Stat Table Position", 
                                 
                                 options = 
                                 
                                     ["Top Left", "Top Center", "Top Right", 
                                     "Bottom Left", "Bottom Center", "Bottom Right", 
                                     "Middle Left", "Middle Center", "Midde Right"])

quandl(x) => 
    request.quandl("BCHAIN/" + x)
    
price                           = quandl("MKPRU") // True price
vol                             = quandl("TRVOU") // Volume
difficulty                      = quandl("DIFF")  // Difficulty
wallet                          = quandl("MWNUS") // My Wallet# Of Users
size                            = quandl("AVBLS") // Average Block Size
api                             = quandl("BLCHS") // api.blockchain size
con                             = quandl("ATRCT") // Median Transaction Confirmation Time
rev                             = quandl("MIREV") // Miners Revenue
tate                            = quandl("HRATE") // Hash rate
cost                            = quandl("CPTRA") // Cost per transaction
percentage                      = quandl("CPTRV") // Cost % of transaction volume
usd                             = quandl("ETRVU") // Transaction Volume USD
output                          = quandl("TOUTV") // Total output volume
tpb                             = quandl("NTRBL") // Number Of Transactions Per Block
unique                          = quandl("NADDU") // # of unique BTC addresses
pop                             = quandl("NTREP") // # of BTC transactions excluding popular addresses
tot                             = quandl("NTRAT") // Total number of transactions
day                             = quandl("NTRAN") // Daily # of transcationss
totfees                         = quandl("TRFUS") // Total transaction fees USD
mc                              = quandl("MKTCP") // Market cap
tbtc                            = quandl("TOTBC") // Tot btc


// _______________________________________________________
//
// Normalize Data
// _______________________________________________________  


 
var box   [] volArray           = array.new_box    ()
var line  [] volLineArray       = array.new_line   ()
var line  [] perLineArray       = array.new_line   ()
var label [] costLabArray       = array.new_label  ()
var float [] volAvg2            = array.new_float(30)
var box   [] contain            = array.new_box   (1)
var line  [] MC                 = array.new_line   ()
var line  [] MC2                = array.new_line   ()
var float [] MCdata             = array.new_float(31)
var box   [] blackBox           = array.new_box   (1)
var label [] blackBoxLab        = array.new_label  ()
var label [] stat               = array.new_label  ()
var box   [] blackBox1          = array.new_box   (1)
var box   [] fee                = array.new_box    ()
var box   [] FEE                = array.new_box    ()
var line  [] sep                = array.new_line  (2)    
var label [] arrows             = array.new_label  ()
var box      stat5              = na    
    


normalizedData() => 
    
    var float hiVol             = 0.0
    var float loVol             = 0.0 
    var float priceHi           = 0.0
    var float mcNorm            = 0.0
    var float hiMC              = 0.0
    var float loMC              = 0.0
    var float hiCost            = 0.0
    var float loCost            = 0.0    
    var float hiFees            = 0.0
    var float loFees            = 0.0
    var float hiPer             = 0.0
    var float loPer             = 0.0 
    
    
    volNormalized               = (vol - loVol)  / (hiVol - loVol)

    calc                        = (math.pow((volNormalized + 1) * 2.15, 10) + priceHi)
    fincalc                     = math.min(math.avg(priceHi * 2,  priceHi), calc)
    boxMax                      = math.max(priceHi * 2, fincalc)

    mcNormalized                =  (mc - loMC) / (hiMC - loMC)
    
    calcMC                      = math.avg(priceHi * 2,priceHi * 2,priceHi * 2,  priceHi * 2,priceHi) * .005
    calcHold                    = math.avg(priceHi * 2,priceHi * 2,priceHi * 2, priceHi * 2, priceHi * 2, priceHi)
    var float MCcalc            = calcHold
    
    costNormalized              =  (percentage - loCost) / (hiCost - loCost)

    calcHoldCost                = math.avg(boxMax * 1.07, boxMax )
    calcCost                    = math.avg(boxMax * 1.07, boxMax ) * .005
    var float Costcalc          = calcHoldCost

    FeesNormalized              =  (totfees - loFees) / (hiFees - loFees)

    calcHoldFees                = math.avg(boxMax *.95, boxMax )
    calcFees                    = math.avg(boxMax *.95, boxMax ) * .005
    var float Feescalc          = calcHoldFees

    perNormalized               = (cost - loPer)  / (hiPer - loPer)

    calcPer                     = (math.pow((perNormalized + 1), 20)) + math.avg(priceHi * 2, priceHi * 2, priceHi)
    fincalcPer                  =  math.min(boxMax * .915, calcPer)
    
    
    if year(time) > 2018
        
        hiVol := hiVol[1] > vol ? hiVol[1] : vol
        loVol := loVol[1] < vol ? loVol[1] : vol
        hiMC  := hiMC[1] > mc ? hiMC[1] : mc
        loMC  := loMC[1] < mc ? loMC[1] : mc
    
        if hiVol != hiVol[1]
            priceHi := high

        hiCost  := hiCost[1] > percentage ? hiCost[1] : percentage
        loCost  := loCost[1] < percentage ? loCost[1] : percentage

        hiFees  := hiFees[1] > totfees ? hiFees[1] : totfees
        loFees  := loFees[1] < totfees ? loFees[1] : totfees
    
        hiPer := hiPer[1] > cost ? hiPer[1] : cost
        loPer := loPer[1] < cost ? loPer[1] : cost

    if mc >= mc[1]
        MCcalc := MCcalc[1] + calcMC 
    else if mc < mc[1]
        MCcalc := MCcalc[1] - calcMC
    if ta.change(priceHi)
        MCcalc := calcHold
    if MCcalc > priceHi * 2
        MCcalc := calcHold
    if MCcalc < math.avg(priceHi * 2,priceHi * 2,priceHi * 2, priceHi * 2,priceHi)
        MCcalc := calcHold
    
    if percentage >= percentage[1]
        Costcalc := Costcalc[1] + calcCost 
    else if percentage < percentage[1]
        Costcalc := Costcalc[1] - calcCost
    if ta.change(priceHi)
        Costcalc := calcHoldCost
    if Costcalc > boxMax * 1.07
        Costcalc := calcHoldCost
    if Costcalc < boxMax * 1.005
        Costcalc := calcHoldCost
    
    if totfees >= totfees[1]
        Feescalc := Feescalc[1] + calcFees 
    else if totfees < totfees[1]
        Feescalc := Feescalc[1] - calcFees
    if ta.change(priceHi)
        Feescalc := calcHoldFees
    if Feescalc > boxMax * .99
        Feescalc := calcHoldFees
    if Feescalc < boxMax * .955
        Feescalc := calcHoldFees
    
    [volNormalized, priceHi, calc, fincalc, boxMax, mcNormalized, MCcalc, 
     calcHold, Costcalc, calcHoldCost, calcCost, calcHoldFees, Feescalc, 
     calcPer, fincalcPer, perNormalized]

[volNorm, priceChart, calc, fincalc, boxMax, mcNormalized, MCcalc, 
 calcHold, Costcalc, calcCostHold, calcCost, calcHoldPer, Percalc, 
 calcFees, fincalcFees, feesNormalized] 
  
  = normalizedData()

cond(x) => 
    
    switch show 
        "Yes"                    => x 
        "No"                     => na
        "All Data In Stat Table" => na
cond1(x) => 
    
    switch show1
        "Yes"                    => x
        "No"                     => na
        "All Data In Stat Table" => x

if last_bar_index - bar_index <= 50
    
// 
    array.push(blackBox, box.new(bar_index + 51, box.get_top(array.get(contain, array.size(contain) - 1)), bar_index + 81, 
                                 math.avg(priceChart * 2, priceChart * 2, priceChart), 
                                 bgcolor = cond(color.new(#000000, 50)), border_color = cond(color.blue)))
    
    array.push(blackBox1, box.new(bar_index + 81, box.get_top(array.get(contain, array.size(contain) - 1)), bar_index + 120, 
                                 math.avg(priceChart * 2, priceChart * 2, priceChart), 
                                 bgcolor = priceChart == priceChart[38] ? cond(color.new(#000000, 50)) : cond(color.new(color.blue, 90)), border_color = cond(color.blue)))
    for i = 0 to 29
    
        array.set(volAvg2, i, fincalc[i])
        array.set(MCdata, i, MCcalc[i])
        
        if show == "Yes"
            if priceChart == priceChart[30]
                array.push(volLineArray, line.new(bar_index + (80 - i), 
                 math.max(box.get_bottom(array.get(contain, 0)), 
                 array.avg(volAvg2)[i + 1]), bar_index + (81 - i), 
                 math.max(box.get_bottom(array.get(contain, 0)), 
                 array.avg(volAvg2)[i])))



    if barstate.islast  
        

        for i = 0 to 29
        
            array.push(volArray, box.new(bar_index + (80 - i), 
                                         array.get(volAvg2, (i)) > priceChart ? array.get(volAvg2, i) : 
                                         priceChart + array.get(volAvg2, i), bar_index + (81 - i), 
                                         priceChart, bgcolor = na, 
                                         border_color = close[i] - open[i] >= 0 ? cond(color.green) : cond(color.red)))
        for i = 0 to 29
            
            if priceChart == priceChart[29]
                array.push(MC, line.new(bar_index + (79 - i), 
                 array.get(MCdata, i + 1), bar_index + (80 - i), 
                 array.get(MCdata, i), color = cond(#03ff00)))
                if array.size(MC2) > 0
                    for j = 0 to array.size(MC2) - 1
                        line.delete(array.shift(MC2))
            
            else if priceChart != priceChart[29]
                if array.size(MC) > 0
                    for n = 0 to array.size(MC) - 1
                        line.delete(array.shift(MC))                
                break

        if priceChart[1] != priceChart[29]
            array.push(MC2, line.new(bar_index + (79), array.get(MCdata, 1), bar_index + (80), array.get(MCdata, 0), color = cond(#03ff00)))

        if array.size(MC2) > 29
            line.delete(array.shift(MC2))
        if array.size(MC) > 0
            if array.size(MC2) > 0 
                for i = 0 to array.size(MC2) - 1
                    line.delete(array.shift(MC2))
        
        if priceChart[38] == priceChart
            if array.size(perLineArray) > 38
                for n = 0 to 37
                    line.delete(array.shift(perLineArray))        
        
        if priceChart[38] != priceChart
            array.push(perLineArray, line.new(bar_index + (119), 
             Costcalc[1], bar_index + (120), 
             Costcalc, color = cond(#ffe500), style = line.style_dotted))  
            if array.size(perLineArray) > 38
                line.delete(array.shift(perLineArray))        
        
        
        if array.size(sep) >= 2
        
            if array.size(perLineArray) > 0 
                if line.get_y1(array.get(perLineArray, array.size(perLineArray) - 1)) > 
                 box.get_top(array.get(contain, array.size(contain) - 1))
                  or line.get_y1(array.get(perLineArray, array.size(perLineArray)- 1)) < 
                   line.get_y1(array.get(sep, array.size(sep) - 1))
                    line.delete(array.pop(perLineArray))
        
            for i = 0 to 37
                
                if priceChart[38] == priceChart
                    array.push(perLineArray, line.new(bar_index + (119 - i), 
                     Costcalc[i + 1], bar_index + (120 - i), 
                     Costcalc[i], color = cond(#ffe500), style = line.style_dotted))
            
                else if priceChart != priceChart[1]
                    if array.size(perLineArray) > 0
                        for x = 0 to array.size(perLineArray) - 1
                            line.delete(array.shift(perLineArray))
                
                    break
                    
            if array.size(sep) >= 2
                for i = 1 to 38
                    if priceChart == priceChart [38]
                        if array.size(fee) > 38
                            for n = 0 to 37
                                box.delete(array.shift(fee))    
                        if array.size(blackBox1) > 0
                            array.push(fee, box.new(bar_index + (119 - i), fincalcFees[i], 
                             bar_index + (120 - i), math.avg(priceChart * 2, priceChart * 2, priceChart) * 1.003, 
                             border_color = close[i] > open[i] ? cond(#00ffff) : cond(color.white), bgcolor = na))
                    else
                        break 

                        
                if priceChart != priceChart[1]
                    if priceChart[1] == priceChart[2]
                        if array.size(fee) > 0
                            for n = 0 to array.size(fee) - 1
                                box.delete(array.shift(fee))

                           
            for i = 0 to 36    
                array.push(costLabArray, label.new(bar_index + (119 - i), 
                 Percalc[i], textcolor = cond(#5d00ff), text = "𝅇", color = na))

        
        if array.size(fee) == 0 
            array.push(FEE, box.new(bar_index + (119), fincalcFees, 
             bar_index + (120), math.avg(priceChart * 2, priceChart * 2, priceChart) * 1.003, 
             border_color = close > open ?  cond(#00ffff) : cond(color.white), bgcolor = na))

        if array.size(FEE) > 38
            box.delete(array.shift(FEE))  

        if array.size(MC) > 30
            for i = 0 to 29
                line.delete(array.shift(MC))        

        if priceChart == priceChart[38]
            if array.size(FEE) > 0
                for i = 0 to array.size(FEE) - 1
                    box.delete(array.shift(FEE))
        
        if priceChart != priceChart[30]
            if array.size(volLineArray) > 0
                for i = 0 to array.size(volLineArray) - 1
                    line.delete(array.shift(volLineArray))

        
        if array.size(FEE) > 0
            for i = 0 to array.size(FEE) - 1
                if box.get_top(array.get(FEE, i)) > boxMax * .93
                    box.delete(array.get(FEE, i))

        
        if array.size(costLabArray) > 0
            if array.size(sep) > 1
                for i = 0 to array.size(costLabArray) - 1
                    if label.get_y(array.get(costLabArray, i)) > 
                     line.get_y1(array.get(sep, array.size(sep) - 2))
                      or label.get_y(array.get(costLabArray, i)) < 
                       line.get_y1(array.get(sep, array.size(sep) - 1))
                        label.delete(array.get(costLabArray, i))
            
        array.push(stat, label.new(bar_index + 67, math.avg(priceChart * 2, priceChart * 2, priceChart) * .935, text = "Exchange Volume \n" + str.tostring(vol, format.volume), 
                                         color = na, 
                                         size = size.normal, 
                                         textcolor = cond(#00ffff), 
                                         style = label.style_label_up))
        
        array.push(stat, label.new(bar_index + 85,  box.get_bottom(array.get(contain, array.size(contain) - 1)) * .965, 
                                         text = "Transaction Volume USD: $" + str.tostring(usd, format.volume), 
                                         color = na, size = size.small, textcolor = cond(color.white), 
                                         style = label.style_label_up))

        array.push(stat, label.new(bar_index + 135,  box.get_top(array.get(contain, array.size(contain) - 1)), 
                                         text = "Cost % of \nTransaction Volume: \n" + str.tostring(percentage, format.percent), 
                                         color = na, size = size.small, textcolor = cond(color.white), 
                                         style = label.style_label_up))
                                         
                                         
        if array.size(stat) > 2


            array.push(stat, label.new(bar_index + 135,  label.get_y(array.get(stat, array.size(stat) - 1)) * .90, 
                                         text = "Total Transaction\n Fees USD: \n$" + str.tostring(totfees, "###,###,###.00"), 
                                         color = na, 
                                         size = size.small, 
                                         textcolor = cond(color.white), 
                                         style = label.style_label_up))
            

        if array.size(stat) > 3
            array.push(stat, label.new(bar_index + 136,  label.get_y(array.get(stat, array.size(stat) - 1)) * .88, 
                                         text = "Cost Per Transaction: \n$" + str.tostring(cost, "###,###,###.00"),
                                         color = na, size = size.small, 
                                         textcolor = cond(color.white), 
                                         style = label.style_label_up))
        array.push(stat, label.new(bar_index + 100,  math.avg(box.get_bottom(array.get(contain, array.size(contain) - 1)), 
                                         box.get_bottom(array.get(contain, array.size(contain) - 1)), 
                                         box.get_top(array.get(contain, array.size(contain) - 1))), 
                                         text = "Median Transaction \n Confrimation Time" , 
                                         color = na, size = size.small, 
                                         textcolor = cond(color.white), style = label.style_label_up))
        
        stat5 := box.new(bar_index + 90, label.get_y(array.get(stat, array.size(stat)- 1)) * .97, bar_index + 110, label.get_y(array.get(stat, array.size(stat)- 1)) * .88, 
                                         text = str.tostring(con, format.mintick) + "ᵐ" , 
                                         bgcolor = cond(#000000), 
                                         text_size = size.large,
                                         border_color = cond(color.blue), 
                                         text_color = cond(color.red))
        
        array.push(arrows, label.new(
                                         bar_index + 126, 
                                         box.get_top(array.get(contain, array.size(contain) - 1)) * .957, 
                                         text = "⏎",
                                         textcolor = cond(#ffe500),
                                         color = na, size = size.huge
                                         ))
        array.push(arrows, label.new(
                                         bar_index + 126, 
                                         label.get_y(array.get(stat, array.size(stat) - 3)) * .957, 
                                         text = "⏎",
                                         textcolor = cond(#5d00ff),
                                         color = na, size = size.huge
                                         ))
        array.push(arrows, label.new(
                                         bar_index + 126, 
                                         math.avg(priceChart * 2, priceChart * 2, priceChart) * .99, 
                                         text = "⏎",
                                         color = na,
                                         textcolor = close > open ? cond(#00ffff) : cond(color.white), size = size.huge
                                         ))
        
        if array.size(stat) > 4
            array.push(sep, line.new(bar_index + 81, boxMax, 
                             bar_index + 120, 
                             boxMax,
                             color = cond(color.blue)))    
            array.push(sep, line.new(bar_index + 81, boxMax * .93 , 
                             bar_index + 120, 
                             boxMax * .93,
                             color = cond(color.blue)))   
            box.delete(stat5[1])

        if array.size(arrows) > 3
            for i = 0 to 2
                label.delete(array.shift(arrows))


    array.push(blackBoxLab, label.new(bar_index + 66,  box.get_top(array.get(contain, array.size(contain) - 1)) * .955, 
                             text = "Market Cap \n" + str.tostring(mc, format.volume), 
                             style = label.style_label_up, 
                             color = na, 
                             textcolor = cond(#03ff00)))    
    array.push(contain, box.new(bar_index + 51, math.max(boxMax * 1.07, priceChart * 1.75), 
                             bar_index + 120,
                             priceChart, 
                             bgcolor = na, 
                             border_color = cond(color.blue)))
    
    if array.size(contain) > 1
        box.delete(array.shift(contain))
    if array.size(blackBox) > 1
        box.delete(array.shift(blackBox))
    if array.size(blackBoxLab) > 1
        label.delete(array.shift(blackBoxLab))
    if array.size(blackBox1) > 1
        box.delete(array.shift(blackBox1))
    
    if array.size(sep) > 2
        for i = 0 to 1
            line.delete(array.shift(sep))
    
    if array.size(stat) > 6
        for i = 0 to 5 by 1
            label.delete(array.shift(stat))
        
    if array.size(volArray) > 30
        for i = 0 to 29 
            box.delete(array.shift(volArray))
    
    if array.size(volLineArray) > 30
        for i = 0 to 29
            line.delete(array.shift(volLineArray))
    
    if array.size(volArray) > 0 
        for i = 0 to array.size(volArray) - 1
            if box.get_top(array.get(volArray, i)) > 
             math.avg(priceChart * 2, priceChart * 2, priceChart)[0]
                box.delete(array.get(volArray, i))
    
    if array.size(costLabArray) > 37
        for i = 0 to 36
            label.delete(array.shift(costLabArray))
    

    position = switch tablePos

        "Top Left"       => position.top_left
        "Top Center"     => position.top_center
        "Top Right"      => position.top_right
        "Bottom Left"    => position.bottom_left  
        "Bottom Center"  => position.bottom_center    
        "Bottom Right"   => position.bottom_right    
        "Middle Left"    => position.middle_left  
        "Middle Center"  => position.middle_center    
        "Midde Right"    => position.middle_right  
    
    color textCond = cond1(color.white)
    var table tab = na
    tab := table.new(position, 100, 100 ,  bgcolor = cond1(color.new(color.blue, 90)),
                                     frame_width = 1, 
                                     frame_color = cond1(color.white), 
                                     border_width = 1, 
                                     border_color = cond1(color.white))
    table.cell(tab, 0, 0,     text = "Difficulty", text_color = textCond, text_size = size.small)
    table.cell(tab, 1, 0,     text = str.tostring(difficulty, "###,###,###"), text_color = textCond,text_size = size.small)
    table.cell(tab, 0, 1,     text = "My Wallet # of Users", text_color = textCond,text_size = size.small)
    table.cell(tab, 1, 1,     text = str.tostring(wallet, "###,###,###"), text_color = textCond,text_size = size.small)
    table.cell(tab, 0, 2,     text = "Average Block Size", text_color = textCond,text_size = size.small)
    table.cell(tab, 1, 2,     text = str.tostring(size, "###,###,###.0000"), text_color = textCond,text_size = size.small)
    table.cell(tab, 0, 3,     text = "Hash Rate", text_color = textCond,text_size = size.small)
    table.cell(tab, 1, 3,     text = str.tostring(tate, "###,###,###.0000"), text_color = textCond,text_size = size.small)
    table.cell(tab, 0, 4,     text = "Transactions Per Block", text_color = textCond,text_size = size.small)
    table.cell(tab, 1, 4,     text = str.tostring(tpb, "###,###,###.0000"), text_color = textCond,text_size = size.small)
    table.cell(tab, 0, 5,     text = "# of Unique BTC Adresses", text_color = textCond,text_size = size.small)
    table.cell(tab, 1, 5,     text = str.tostring(unique, "###,###,###"), text_color = textCond,text_size = size.small)
    table.cell(tab, 0, 6,     text = "Total Output Volume", text_color = textCond,text_size = size.small)
    table.cell(tab, 1, 6,     text = str.tostring(output, "###,###,###.0000"), text_color = textCond,text_size = size.small)
    table.cell(tab, 0, 7,     text = "# of Transactions \n(Excluding Popular Addresses)", text_color = textCond,text_size = size.small)
    table.cell(tab, 1, 7,     text = str.tostring(pop, "###,###,###"), text_color = textCond,text_size = size.small)
    
    if show == "All Data In Stat Table" 
        
        table.cell(tab, 2, 0, text = "Exchange Volume", text_color = textCond, text_size = size.small)
        table.cell(tab, 3, 0, text = str.tostring(vol, "###,###,###"), text_color = textCond,text_size = size.small)
        table.cell(tab, 2, 1, text = "Transaction Volume USD", text_color = textCond,text_size = size.small)
        table.cell(tab, 3, 1, text = str.tostring(usd, "###,###,###"), text_color = textCond,text_size = size.small)
        table.cell(tab, 2, 2, text = "Median Transaction \n Confirmation Time", text_color = textCond,text_size = size.small)
        table.cell(tab, 3, 2, text = str.tostring(con, "###,###,###.0000"), text_color = textCond,text_size = size.small)
        table.cell(tab, 2, 3, text = "Cost Per Transaction", text_color = textCond,text_size = size.small)
        table.cell(tab, 4, 3, text = str.tostring(cost, "###,###,###.0000"), text_color = textCond,text_size = size.small)
        table.cell(tab, 2, 4, text = "Total Transaction Fees USD", text_color = textCond,text_size = size.small)
        table.cell(tab, 4, 4, text = str.tostring(totfees, "###,###,###.0000"), text_color = textCond,text_size = size.small)
        table.cell(tab, 2, 5, text = "Cost % of Transaction Volume", text_color = textCond,text_size = size.small)
        table.cell(tab, 4, 5, text = str.tostring(percentage, "###,###,###"), text_color = textCond,text_size = size.small)
        table.cell(tab, 2, 6, text = "Market Cap", text_color = textCond,text_size = size.small)
        table.cell(tab, 4, 6, text = str.tostring(mc,format.volume), text_color = textCond,text_size = size.small)
        table.cell(tab, 2, 7, text = "Total BTC", text_color = textCond,text_size = size.small)
        table.cell(tab, 4, 7, text = str.tostring(tbtc, "###,###,###"), text_color = textCond,text_size = size.small)
        table.cell(tab, 4, 0, text = "Miners' Revenue", text_color = textCond, text_size = size.small)
        table.cell(tab, 5, 0, text = str.tostring(rev, "###,###,###"), text_color = textCond,text_size = size.small)
        table.cell(tab, 4, 1, text = "api.blockchain Size", text_color = textCond,text_size = size.small)
        table.cell(tab, 5, 1, text = str.tostring(api, "###,###,###"), text_color = textCond,text_size = size.small)
        table.cell(tab, 4, 2, text = "Total Transactions", text_color = textCond,text_size = size.small)
        table.cell(tab, 5, 2, text = str.tostring(tot, "###,###,###"), text_color = textCond,text_size = size.small)
        
        table.merge_cells(tab, 2, 3, 3, 3)
        table.merge_cells(tab, 2, 4, 3, 4)
        table.merge_cells(tab, 2, 5, 3, 5)
        table.merge_cells(tab, 2, 6, 3, 6)
        table.merge_cells(tab, 2, 7, 3, 7)
        
        table.merge_cells(tab, 4, 3, 5, 3)
        table.merge_cells(tab, 4, 4, 5, 4)
        table.merge_cells(tab, 4, 5, 5, 5)
        table.merge_cells(tab, 4, 6, 5, 6)
        table.merge_cells(tab, 4, 7, 5, 7)


// _______________________________________________________
//
// Defining Fiscal Quarter Periods
// _______________________________________________________



var startCalculations                = matrix.new<float>(3, 5)
var float [] Qcalculations           = array.new_float()
var float [] cumulCalc               = array.new_float()
var float [] x                       = array.new_float()


if barstate.isfirst
    for c = 0 to matrix.columns(startCalculations) - 1
        matrix.set(startCalculations, 0, c, 0)
        matrix.set(startCalculations, 1, c, 0)
    array.push(Qcalculations, 0)

if bar_index > 0 
    
    if year(time) != year(time[1]) 
        matrix.set(startCalculations, 0, 0, 1)
        
    array.push(cumulCalc, rev)
    matrix.set(startCalculations, 1, 0, array.sum(cumulCalc))

if matrix.get(startCalculations, 0, 0) == 1
    
    if time >= timestamp(year(time), 01,01,00,00,05) and time <= timestamp(year(time), 03, 31, 23, 59, 59) 
        matrix.set(startCalculations, 0, 1, 1)
        array.push(Qcalculations, rev)
        matrix.set(startCalculations, 1, 1, array.sum(Qcalculations))
    else
        matrix.set(startCalculations, 0, 1, 0)
        matrix.set(startCalculations, 1, 1, 0)
    if matrix.get(startCalculations, 0, 1)[1] == 1 
        if matrix.get(startCalculations, 0, 1) == 0
            array.clear(Qcalculations)
            array.clear(x)


    if time >= timestamp(year(time), 04, 1, 00, 00, 00) and time <= timestamp(year(time), 06, 30, 23, 59, 59) 
        matrix.set(startCalculations, 0, 2, 1)
        array.push(Qcalculations, rev)
        matrix.set(startCalculations, 1, 2, array.sum(Qcalculations))

    else
        matrix.set(startCalculations, 0, 2, 0)
        matrix.set(startCalculations, 1, 2, 0)
    if matrix.get(startCalculations, 0, 2)[1] == 1 
        if matrix.get(startCalculations, 0, 2) == 0
            array.clear(Qcalculations)
            array.clear(x)


    if time >= timestamp(year(time), 07, 1, 00, 00, 00) and time <= timestamp(year(time), 09, 30, 23, 59, 59) 
        matrix.set(startCalculations, 0, 3, 1)
        array.push(Qcalculations, rev)
        matrix.set(startCalculations, 1, 3, array.sum(Qcalculations))

    else
        matrix.set(startCalculations, 0, 3, 0)
        matrix.set(startCalculations, 1, 3, 0)
    if matrix.get(startCalculations, 0, 3)[1] == 1 
        if matrix.get(startCalculations, 0, 3) == 0
            array.clear(Qcalculations)
            array.clear(x)


    if time >= timestamp(year(time), 10, 1, 00, 00, 00) and time <= timestamp(year(time), 12, 31, 23, 59, 59)    
        matrix.set(startCalculations, 0, 4, 1)
        array.push(Qcalculations, rev)
        matrix.set(startCalculations, 1, 4, array.sum(Qcalculations))

    else
        matrix.set(startCalculations, 0, 4, 0)
        matrix.set(startCalculations, 1, 4, 0)
    if time == timestamp(year(time), 01, 01, 00, 00, 00)
        array.clear(Qcalculations)
        array.clear(x)



lin() => 


    array.push(x, close)        
    
    X = array.sum(x) / array.size(x) - 
         (ta.linreg(close, array.size(x), 0) - 
             ta.linreg(close, array.size(x), 1)) * 
                 math.floor(array.size(x) / 2) + 0.5 * 
                     (ta.linreg(close, array.size(x), 0) - 
                         ta.linreg(close, array.size(x), 1))        
    
    Y  = (array.sum(x) / array.size(x) - 
         (ta.linreg(close, array.size(x), 0) - 
             ta.linreg(close, array.size(x), 1)) * 
                 math.floor(array.size(x) / 2))  + 
                     (ta.linreg(close, array.size(x), 0) - 
                         ta.linreg(close, array.size(x), 1)) * 
                             (array.size(x) - 1)
    
    
    stDev = ta.stdev(X, array.size(x) > 0 ? array.size(x) : 1) * 4

    
    [X, Y, stDev]
    
[X, Y, stDev]              = lin()

cond2(x) => 
    
    switch show2
        "Yes" => x 
        "No"  => na

cond3(x) => 
    
    switch show
        
        "Yes" => x
        "No"  => x
        "All Data in Stat Table" => color.new(color.white, 100)
    
vars() =>
    n  = array.size(x)[1]
    n1 = ta.highest(math.ceil(high), n > 0 ? n : 1)
    [n, n1]

[n, n1] = vars()

var line    [] xy                       = array.new_line(2)
var linefill[] xyfill                   = array.new_linefill(2)
var line    [] xyperm                   = array.new_line()
var linefill[] permfill                 = array.new_linefill()
var label   [] minlab                   = array.new_label()
var label   [] minlabperm               = array.new_label()



    
array.push(xy, line.new(bar_index + 1 - array.size(x), X + stDev, bar_index+ 20, Y + stDev, color = cond2(color.blue)))
array.push(xy, line.new(bar_index + 1 - array.size(x), X - stDev, bar_index + 20, Y - stDev, color = cond2(color.blue)))
array.push(xyfill, linefill.new(array.get(xy, array.size(xy) - 1), array.get(xy, array.size(xy) - 2), color = cond2(color.new(color.blue, 90))))
array.push(minlab, label.new(bar_index, math.max(high, X + stDev, Y + stDev), style = label.style_label_down, text = "Cumulative Miners' Revenue\n$" + 
     str.tostring(matrix.get(startCalculations, 1 , 
     matrix.get(startCalculations, 0, 1) == 1 ? 1 :
     matrix.get(startCalculations, 0, 2) == 1 ? 2 :
     matrix.get(startCalculations, 0, 3) == 1 ? 3 : 4
     ),format.volume ) + " USD", textcolor = cond3(#000000),
     color = cond3(color.new(#ffffff, 40)), size = size.small
     ))

if array.size(xy) > 2
    for i = 0 to 1 
        line.delete(array.shift(xy))
if array.size(xyfill) > 2
    for i = 0 to 1
        linefill.delete(array.shift(xyfill))



if array.size(x) == 1
    array.push(xyperm, line.new(bar_index - n, X[1] + stDev[1], bar_index, Y[1] + stDev[1], color = cond2(color.new(#ffffff, 30)), style = line.style_dashed, width = 2))
    array.push(xyperm, line.new(bar_index - n, X[1] - stDev[1], bar_index, Y[1] - stDev[1], color = cond2(color.new(#ffffff, 30)), style = line.style_dashed, width = 2))
    array.push(permfill, linefill.new(array.get(xyperm, array.size(xyperm) - 1), array.get(xyperm, array.size(xyperm) - 2), color = cond2(color.new(#ffffff, 90))))
    array.push(minlabperm, label.copy(array.get(minlab, 0)[1]))
    label.set_x(array.get(minlabperm, array.size(minlabperm) - 1), math.ceil(bar_index - n / 2))
    label.set_y(array.get(minlabperm, array.size(minlabperm) - 1), math.max(Y[math.ceil(n / 2)] + stDev[math.ceil(n /2)], n1) * 1.12)
    label.set_style(array.get(minlabperm, array.size(minlabperm) - 1), label.style_label_center)
    label.set_color(array.get(minlabperm, array.size(minlabperm) - 1), cond3(color.new(#000000, 40)))
    label.set_textcolor(array.get(minlabperm, array.size(minlabperm) - 1), cond3(color.white))
    label.set_size(array.get(minlabperm, array.size(minlabperm) - 1), size.tiny)
    

if array.size(minlab) > 1
    label.delete(array.shift(minlab))
    

// _______________________________________________________
//
// Lower Timeframe
// _______________________________________________________
 

[middle, upper, lower] = ta.bb(close, 20, 2.0)
b = (close - lower)/(upper - lower)
k = ta.stoch(close, high, low, 14)

    
request = switch TFdata
    
    "Close"              => close            
    "High"               => high 
    "Low"                => low      
    "Open"               => open       
    "HL2"                => hl2      
    "HLC3"               => hlc3       
    "OHLC4"              => ohlc4        
    "HLCC4"              => hlcc4        
    "Volume"             => volume
    "RSI"                => ta.rsi(close, 14)
    "EMA 20"             => ta.ema(close, 20)      
    "%b"                 => b 
    "Stochastic"         => k         
    "VWAP"               => ta.vwap 

TIME = switch TF
    
    "1 Minute"           => " Minutes"
    "5 Minute"           => " Minutes"
    "15 Minute"          => " Minutes"
    "30 Minute"          => " Minutes"
    "45 Minute"          => " Minutes"
    "1 Hour"             => " Hours"
    "2 Hour"             => " Hours"
    "3 Hour"             => " Hours"
    "4 Hour"             => " Hours"
    "Custom"             => " Minutes"

MULT = switch TF
    
    "1 Minute"           => 1
    "5 Minute"           => 5
    "15 Minute"          => 15
    "30 Minute"          => 30
    "45 Minute"          => 45
    "1 Hour"             => 1
    "2 Hour"             => 2
    "3 Hour"             => 3
    "4 Hour"             => 4
    "Custom"             => custom


var table TAB             = na
var string [] tri         = array.new_string()
var string [] GL          = array.new_string()
lowTF                     = request.security_lower_tf(syminfo.tickerid, trueTF, request)
show4                     = input.string("Yes", title = "Show LTF Data?", options = ["Yes", "No"])
lowTFstring               = ""

SIZE() => 
    
    array.size(lowTF) > 30 ? 30 : array.size(lowTF) > 0 and array.size(lowTF) <= 30 ? array.size(lowTF) - 1 : na

array.reverse(lowTF)

if barstate.islast
    if show4 == "Yes"
        if array.size(lowTF) > 1
            for i = 1 to SIZE()
                if array.get(lowTF, i) < array.get(lowTF, i - 1)
                    array.push(tri, "△")
                    array.push(GL, "+")
                else if array.get(lowTF, i) >= array.get(lowTF, i - 1)
                    array.push(tri, "▼")
                    array.push(GL,  "-")
                   
            if array.size(tri) > 1
                if array.size(GL) > 1
                    if array.size(lowTF) > 1
                        for j = 1 to SIZE()
                            lowTFstring := lowTFstring + str.tostring(array.get(lowTF, j - 1), "###,###,###.00") + "  " + 
                             str.tostring((j - 1) * MULT) + TIME + "  " + array.get(tri, j- 1) +  " (" + 
                             array.get(GL, j - 1) + " " + str.tostring(math.abs(array.get(lowTF, j - 1) - 
                             array.get(lowTF, j)), "###,###,###.00") + ")\n" 
        TAB := table.new(position.top_right, 100, 100, bgcolor = color.new(color.green, 90), border_color = color.white, frame_color = color.white, frame_width = 1, border_width = 1)
        if array.size(tri) > 0
            for i = 1 to SIZE()
                table.cell(TAB, 0, 0, str.tostring(MULT) + TIME + " " + TFdata, text_color = color.white, text_halign = text.align_center, text_size = size.small)
                table.cell(TAB, 0, 1, lowTFstring, text_color = color.white, text_halign = text.align_left)
        

    

// _______________________________________________________
//
// Optional Plots
// _______________________________________________________


plot(price       ,display = display.none, title = "True Price ",                                        color = color.blue   )
plot(vol         ,display = display.none, title = "Exchange Volume ",                                   color = color.red    )
plot(difficulty  ,display = display.none, title = "Difficulty ",                                        color = color.green  )
plot(wallet      ,display = display.none, title = "My Wallet # of Users ",                              color = color.orange )
plot(size        ,display = display.none, title = "Average Block Size ",                                color = color.yellow )
plot(api         ,display = display.none, title = "api.blockchain Size ",                               color = color.purple )
plot(con         ,display = display.none, title = "Median Transaction Confirmation Time ",             color = color.gray   )
plot(rev         ,display = display.none, title = "Miners' Revenue",                                    color = color.white  )
plot(tate        ,display = display.none, title = "Hash Rate",                                          color = color.teal   )
plot(cost        ,display = display.none, title = "Cost Per Transaction",                               color = color.olive  )
plot(percentage  ,display = display.none, title = "Cost % of Transaction Volume",                       color = color.maroon )
plot(usd         ,display = display.none, title = "Transaction Volume USD",                             color = color.silver )
plot(output      ,display = display.none, title = "Total Output Volume",                                color = color.navy   )
plot(tpb         ,display = display.none, title = "Number of Transactions Per Block",                   color = color.lime   )
plot(unique      ,display = display.none, title = "# of Unique BTC Addresses",                          color = color.fuchsia)
plot(tot         ,display = display.none, title = "Total Number of Transactions",                       color = color.black  )
plot(day         ,display = display.none, title = "Daily Number of Transactions",                       color = color.aqua   )
plot(totfees     ,display = display.none, title = "Total Transaction Fees (USD) ",                      color = #000000      )
plot(mc          ,display = display.none, title = "Market Cap",                                         color = #148328      )
plot(pop         ,display = display.none, title = "# of BTC Transactions Excluding Popular Addresses ", color = #437891      )
plot(tbtc        ,display = display.none, title = "Total BTC ",                                         color = #538271      )


if timeframe.isintraday
    runtime.error("This script runs on DWM charts")
