Bolton-Tremblay Index
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © QuantiLuxe

//@version=5
indicator("Bolton-Tremblay Index", "[Ʌ] -‌ BOLTR", false)

type bar
    float o = open
    float h = high
    float l = low
    float c = close

method src(bar b, simple string src) =>
    float x = switch src
        'oc2'   => math.avg(b.o, b.c          )
        'hl2'   => math.avg(b.h, b.l          )
        'hlc3'  => math.avg(b.h, b.l, b.c     )
        'ohlc4' => math.avg(b.o, b.h, b.l, b.c)
        'hlcc4' => math.avg(b.h, b.l, b.c, b.c)

    x

method null(bar b) =>
    
    na(b) ? bar.new(0, 0, 0, 0) : b

method ha(bar b, simple bool p = true) =>
    var bar x = bar.new(       )
    x.c      := b  .src('ohlc4')
    x        := bar.new(
         na(x.o[1]) ? 
             b.src('oc2') : nz(x.src('oc2')[1]),
         math.max(b.h, math.max(x.o, x.c))     ,
         math.min(b.l, math.min(x.o, x.c))     ,
         x.c                                   )

    p ? x : b


var string gb = 'Bolton-Tremblay', var string ge = 'EMAs'
idx  = input.string('NYSE', "Index ", ['NYSE', 'NASDAQ', 'AMEX'], group = gb)
ma1  = input.bool  (true  , "EMA  |",               inline = '1', group = ge)
len1 = input.int   (20    , "Length",               inline = '1', group = ge)
ma2  = input.bool  (true  , "EMA  |",               inline = '2', group = ge)
len2 = input.int   (50    , "Length",               inline = '2', group = ge)


[tick_adv, tick_dec, tick_unch] = switch idx
    'NYSE'   => ['ADV' , 'DECL' , 'UCHG' ]
    'NASDAQ' => ['ADVQ', 'DECLQ', 'UCHGQ']
    'AMEX'   => ['ADVA', 'DECLA', 'UCHGA']

bar adv = request.security('USI:' + tick_adv , '', bar.new()).null()
bar dec = request.security('USI:' + tick_dec , '', bar.new()).null()
bar unc = request.security('USI:' + tick_unch, '', bar.new()).null()

float r  = (adv.c - dec.c) / unc.c
float a  = math.sqrt(math.abs(r)   )
float b  = ta  .cum (r > 0 ? a : -a)
bar   bt = bar .new (
                  b[1]             ,
                  math.max(b, b[1]),
                  math.min(b, b[1]),
                  b                ).ha()


var color colup   = #fff2cc
var color coldn   = #6fa8dc
var color colema1 = #FFD6E8
var color colema2 = #9a9adf

color haColor = switch
    bt.c > bt.o => colup
    bt.c < bt.o => coldn

plotcandle(bt.o, bt.h, bt.l, bt.c, 
     "BOLTR", haColor, haColor, bordercolor = haColor)

plot(ma1 ? ta.ema(bt.c, len1) : na, " 𝘌𝘔𝘈 1", colema1)
plot(ma2 ? ta.ema(bt.c, len2) : na, " 𝘌𝘔𝘈 2", colema2)

//Source Construction For Indicator\Strategy Exports
plot(bt.o           , "open" , editable = false, display = display.none)
plot(bt.h           , "high" , editable = false, display = display.none)
plot(bt.l           , "low"  , editable = false, display = display.none)
plot(bt.c           , "close", editable = false, display = display.none)
plot(bt.src('hl2'  ), "hl2"  , editable = false, display = display.none)
plot(bt.src('hlc3' ), "hlc3" , editable = false, display = display.none)
plot(bt.src('ohlc4'), "ohlc4", editable = false, display = display.none)
plot(bt.src('hlcc4'), "hlcc4", editable = false, display = display.none)