Short Term 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("Short Term IndeX", "[Ʌ] -‌ STIX", 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

f_stix(bar a, bar d, simple int len) =>
    bar x = bar.new(
         ta.ema(a.o / (a.o + d.o) * 100, len),
         ta.ema(a.h / (a.h + d.h) * 100, len),
         ta.ema(a.l / (a.l + d.l) * 100, len),
         ta.ema(a.c / (a.c + d.c) * 100, len))

    x
    

var string gs = 'STIX', var string ge = 'EMAs'
idx  = input.string('NYSE', "Index ", ['NYSE', 'NASDAQ', 'AMEX'], group = gs)
len  = input.int   (21    , "Length",                             group = gs)
ma1  = input.bool  (true  , "EMA  |",               inline = '1', group = ge)
len1 = input.int   (20    , "Length",               inline = '1', group = ge)
ma2  = input.bool  (false , "EMA  |",               inline = '2', group = ge)
len2 = input.int   (50    , "Length",               inline = '2', group = ge)


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

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

bar stix = f_stix(adv.null(), dec.null(), len).ha()


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

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

plotcandle(stix.o, stix.h, stix.l, stix.c, 
     "Stix", haColor, haColor, bordercolor = haColor)

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

hline(50, "MidLine", #787b86ab, hline.style_dotted)

max = hline(80, display = display.none)
hh  = hline(70, display = display.none)
lh  = hline(60, display = display.none)

min = hline(20, display = display.none)
ll  = hline(30, display = display.none)
hl  = hline(40, display = display.none)

fill(lh, hh , color = #9a9adf2a)
fill(hh, max, color = #9a9adf4d)

fill(ll, hl , color = #ffd6e83b)
fill(ll, min, color = #ffd6e85e)

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