COT Report BTC Positions
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nino_trade

//@version=5
indicator("COT Report BTC Positions", overlay=false, explicit_plot_zorder=true)

// const
int P_L = 0
int P_S = 1

COLOR_D = #565656
COLOR_AM = #2FBCFE
COLOR_LM = #3BAE6B
COLOR_OR = #C565ED
COLOR_NR = #D31926


// input
string positionString = input.string("Long", "position", options=["Long", "Short"], group="ticker")
int position = switch positionString
    "Long" => P_L
    "Short" => P_S

bool showD = input.bool(true, "dealer", group="category")
bool showAM = input.bool(true, "asset manager", group="category")
bool showLM = input.bool(true, "leverage funds", group="category")
bool showOR = input.bool(true, "other reportable", group="category")
bool showNR = input.bool(true, "non reportable", group="category")


// source const
cftc = "133741"
string dpLongs = "COT3:" + cftc + "_F_DP_L"
string dpShorts = "COT3:" + cftc + "_F_DP_S"

string ampLongs = "COT3:" + cftc + "_F_AMP_L"
string ampShorts = "COT3:" + cftc + "_F_AMP_S"

string lmpLongs = "COT3:" + cftc + "_F_LMP_L"
string lmpShorts = "COT3:" + cftc + "_F_LMP_S"

string orpLongs = "COT3:" + cftc + "_F_ORP_L"
string orpShorts = "COT3:" + cftc + "_F_ORP_S"

string nrpLongs = "COT3:" + cftc + "_F_NRP_L"
string nrpShorts = "COT3:" + cftc + "_F_NRP_S"


// func
f_getLSSource(position) =>
    string dS = na
    string amS = na
    string lmS = na
    string orS = na
    string nrS = na

    if position == P_L
        dS := dpLongs
        amS := ampLongs
        lmS := lmpLongs
        orS := orpLongs
        nrS := nrpLongs
    else if position == P_S
        dS := dpShorts
        amS := ampShorts
        lmS := lmpShorts
        orS := orpShorts
        nrS := nrpShorts
    [dS, amS, lmS, orS, nrS]

f_security(_sym, _res, _src, _rep) => 
    request.security(_sym, _res, _src[not _rep and barstate.isrealtime ? 1 : 0])[_rep or barstate.isrealtime ? 0 : 1]


// source data
[dS, amS, lmS, orS, nrS] = f_getLSSource(position)
sD  = (na(dS) or not showD) ? na : f_security(dS, "D", close, false)
sAM = (na(amS) or not showAM) ? na : f_security(amS, "D", close, false)
sLM = (na(lmS) or not showLM) ? na : f_security(lmS, "D", close, false)
sOR = (na(orS) or not showOR) ? na : f_security(orS, "D", close, false)
sNR = (na(nrS) or not showNR) ? na : f_security(nrS, "D", close, false)

    
// plot, explicit_plot_zorder is ON
plot(showD ? sD : na, 'Dealer', color=color.new(COLOR_D, 0), style=plot.style_line, linewidth=2)
plot(showAM ? sAM : na, 'Asset Mgr', color=color.new(COLOR_AM, 0), style=plot.style_line, linewidth=2)
plot(showLM ? sLM : na, 'Lev Funds', color=color.new(COLOR_LM, 0), style=plot.style_line, linewidth=2)
plot(showOR ? sOR : na, 'Other Rept', color=color.new(COLOR_OR, 0), style=plot.style_line, linewidth=2)
plot(showNR ? sNR : na, 'Non Rept', color=color.new(COLOR_NR, 0), style=plot.style_line, linewidth=2)
