Market Internals
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © syntaxgeek

//@version=5
indicator("Market Internals", shorttitle="MI", overlay=true, max_labels_count=500)

// <consts>
v_y1 = low - (ta.atr(30) * 2)
v_y1B = low - ta.atr(30)
v_y2 = high + (ta.atr(30) * 2)
v_y2B = high + ta.atr(30)
// </consts>

// <funcs>
f_newLabel(_text, _bias) =>
    v_labelColor = color.green
    v_labelTextColor = color.white
    v_labelPosition = v_y1
    v_labelStyle = label.style_label_up

    if _bias == 'bullish'
        v_labelColor := color.green
        v_labelPosition := v_y1
        v_labelTextColor := color.white
        v_labelStyle := label.style_label_up
    else if _bias == 'bearish'
        v_labelColor := color.red
        v_labelPosition := v_y2
        v_labelTextColor := color.white
        v_labelStyle := label.style_label_down
    else if _bias == 'neutral'
        v_labelColor := color.white
        v_labelPosition := close[1] < close ? v_y2 : v_y1
        v_labelTextColor := color.black
        v_labelStyle := close[1] < close ? label.style_label_down : label.style_label_up

    label.new(bar_index, v_labelPosition, _text, xloc.bar_index, yloc.price, v_labelColor, v_labelStyle, v_labelTextColor)
// </funcs>

// <vars>
usTicks = request.security("USI:UPTKS.US-USI:DNTKS.US", timeframe.period, close)
spxOptions = request.security("USI:CVSPX-USI:PVSPX", timeframe.period, close)
spxGamma = request.security("CBOE:CDSPG", timeframe.period, ta.change(close, 1))
bidAskVol = request.security("USI:VOL.ASK.US-USI:VOL.BID.US", timeframe.period, close)
issues = request.security("USI:ADVN.US-USI:DECL.US", timeframe.period, close)

bullishTickCross = ta.crossover(usTicks, 0)
bearishTickCross = ta.crossunder(usTicks, 0)
bullishOptionCross = ta.crossover(spxOptions, 0)
bearishOptionCross = ta.crossunder(spxOptions, 0)
bullishBidAskVolCross = ta.crossover(bidAskVol, 0)
bearishBidAskVolCross = ta.crossunder(bidAskVol, 0)
bullishIssuesCross = ta.crossover(issues, 0)
bearishIssuesCross = ta.crossunder(issues, 0)
spxGammaSpike = spxGamma > 0.08
// </vars>

// <plots>
if spxGammaSpike
    f_newLabel("G", "neutral")

if bullishTickCross
    f_newLabel("T", "bullish")

if bearishTickCross
    f_newLabel("T", "bearish")

if bullishOptionCross
    f_newLabel("O", "bullish")

if bearishOptionCross
    f_newLabel("O", "bearish")

if bullishBidAskVolCross
    f_newLabel("V", "bullish")

if bearishBidAskVolCross
    f_newLabel("V", "bearish")

if bullishIssuesCross
    f_newLabel("I", "bullish")

if bearishIssuesCross
    f_newLabel("I", "bearish")
// </plots>