[Nekonyam] Auto Select Currency Binance Open Interest
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Nekonyam

//@version=5
indicator(title='Binance Open Interest Auto Select Currency[Nekonyam]', shorttitle='Open Interest',format = format.volume)

//Input settings.
chart_type = input.string(title="Chart Type", defval="Candle", options=["Candle","Line","Histogram"])

coin = input.string(title="Tether/Coin Margin's OI", defval="Tether", options=["Tether","Coin"])
isOverride = input(defval=false, title='Enable override symbol')
overrideSymbol = input.string(title='Override symbol', defval="BTC")
notshow = input(defval=false, title='Do not display messages when something happens',tooltip="For example, a message that you had set up a Coin margin OI and couldn't find it, so changed it to a Tether margin OI. Or, the message when no OI is found will no longer be displayed.")

line_color = input.color(color.blue, "Line color")
Histogram_up_color = input.color(color.new(#26A69A,0), "Histogram up color")
Histogram_down_color = input.color(color.new(#EF5350,0), "Histogram down color")

candle_up_color = input.color(color.new(#26A69A,0), "Candle up color")
candle_down_color = input.color(color.new(#EF5350,0), "Candle down color")
candle_wickcolor = input.color(color.gray, "Candle wick color")
candle_bordercolor = input.color(color.gray, "Candle border color")

show_title=input.bool(defval=true,title="Show title")
title_position = input.string(title='Title position', defval=position.bottom_left, options=[position.bottom_center, position.bottom_left, position.bottom_right, position.middle_center, position.middle_left, position.middle_right, position.top_center, position.top_left, position.top_right])
title_size = input.string(title='Title Size', defval=size.normal, options=[size.auto, size.tiny, size.small, size.normal, size.large, size.huge])
title_bakcol = input.color(color.white, "Title Background Color")
title_txtcol = input.color(color.black, "Title Text Color")


//Input open intarest.
o_c_open = request.security("BINANCE:"+overrideSymbol+"PERP_OI", timeframe.period, open,ignore_invalid_symbol=true)
o_c_high = request.security("BINANCE:"+overrideSymbol+"PERP_OI", timeframe.period, high,ignore_invalid_symbol=true)
o_c_low = request.security("BINANCE:"+overrideSymbol+"PERP_OI", timeframe.period, low,ignore_invalid_symbol=true)
o_c_close = request.security("BINANCE:"+overrideSymbol+"PERP_OI", timeframe.period, close,ignore_invalid_symbol=true)

o_t_open = request.security("BINANCE:"+overrideSymbol+"USDTPERP_OI", timeframe.period, open,ignore_invalid_symbol=true)
o_t_high = request.security("BINANCE:"+overrideSymbol+"USDTPERP_OI", timeframe.period, high,ignore_invalid_symbol=true)
o_t_low = request.security("BINANCE:"+overrideSymbol+"USDTPERP_OI", timeframe.period, low,ignore_invalid_symbol=true)
o_t_close = request.security("BINANCE:"+overrideSymbol+"USDTPERP_OI", timeframe.period, close,ignore_invalid_symbol=true)

c_open = request.security("BINANCE:"+syminfo.basecurrency+"PERP_OI", timeframe.period, open,ignore_invalid_symbol=true)
c_high = request.security("BINANCE:"+syminfo.basecurrency+"PERP_OI", timeframe.period, high,ignore_invalid_symbol=true)
c_low = request.security("BINANCE:"+syminfo.basecurrency+"PERP_OI", timeframe.period, low,ignore_invalid_symbol=true)
c_close = request.security("BINANCE:"+syminfo.basecurrency+"PERP_OI", timeframe.period, close,ignore_invalid_symbol=true)

t_open =request.security("BINANCE:"+syminfo.basecurrency+"USDTPERP_OI", timeframe.period, open,ignore_invalid_symbol=true)
t_high =request.security("BINANCE:"+syminfo.basecurrency+"USDTPERP_OI", timeframe.period, high,ignore_invalid_symbol=true)
t_low =request.security("BINANCE:"+syminfo.basecurrency+"USDTPERP_OI", timeframe.period, low,ignore_invalid_symbol=true)
t_close =request.security("BINANCE:"+syminfo.basecurrency+"USDTPERP_OI", timeframe.period, close,ignore_invalid_symbol=true)

s_open=0.0
s_high=0.0
s_low=0.0
s_close=0.0
isError=0

//Assign to variable for chart display.
if isOverride==true
    if coin=="Coin"
        s_open:=o_c_open
        s_high:=o_c_high
        s_low:=o_c_low
        s_close:=o_c_close
        //Error detection.
        if na(s_open)
            isError:=1
    else
        s_open:=o_t_open
        s_high:=o_t_high
        s_low:=o_t_low
        s_close:=o_t_close
        if na(s_open)
            isError:=2
else
    if coin=="Coin"
        s_open:=c_open
        s_high:=c_high
        s_low:=c_low
        s_close:=c_close
        if na(s_open)
            isError:=3
    else
        s_open:=t_open
        s_high:=t_high
        s_low:=t_low
        s_close:=t_close
        if na(s_open)
            isError:=4
            
if syminfo.basecurrency=="" and na(s_open)
    if not(isError==1 or isError==2)
        isError:=5

//Identify the type of error and change the message.
errorMessageOne=""
overrideSymbolShowText=overrideSymbol

oiName=isOverride==true?(str.upper(overrideSymbolShowText)+" / "):(syminfo.basecurrency+" / ")
//When both OI's of the overridden symbol are not provided.
if (isError==1 or isError==2) and na(o_c_open)==true and na(o_t_open)==true
    if str.length(overrideSymbol)>7
        overrideSymbolShowText:=str.substring(overrideSymbol,0,8)+"..."
    if overrideSymbolShowText==""
        errorMessageOne:="The override symbol is blank."
        oiName:=""
    else
        errorMessageOne:=" / This overridden\nsymbol's OI not found."

//When a coin margin override is used and only tether margin is provided.
if isError==1 and na(o_t_open)==false
    errorMessageOne:="/ Coin OI not found.\nChanged to Tether OI."
    coin:="Tether"
    s_open:=o_t_open
    s_high:=o_t_high
    s_low:=o_t_low
    s_close:=o_t_close
    
//When a tether margin override is used and only coin margin is provided.
if isError==2 and na(o_c_open)==false
    errorMessageOne:="/ Tether OI not found.\nChanged to Coin OI."
    coin:="Coin"
    s_open:=o_c_open
    s_high:=o_c_high
    s_low:=o_c_low
    s_close:=o_c_close

//When no override is used and OI for both margins is not provided.
if (isError==3 or isError==4) and na(c_open)==true and na(t_open)==true
    errorMessageOne:=" / This symbol's\nOI not found."

//When no override is used and only tether margin is provided.
if isError==3 and na(t_open)==false
    errorMessageOne:="/ Coin OI not found.\nChanged to Tether OI."
    coin:="Tether"
    s_open:=t_open
    s_high:=t_high
    s_low:=t_low
    s_close:=t_close

//When no override is used and only coin margin is provided
if isError==4 and na(c_open)==false
    errorMessageOne:="/ Tether OI not found.\nChanged to Coin OI."
    coin:="Coin"
    s_open:=c_open
    s_high:=c_high
    s_low:=c_low
    s_close:=c_close

    //When a symbol that is not supported at all is displayed on the main chart.
if isError==5
    oiName:=""
    errorMessageOne:="This symbol's OI\nis not available."



//Symbol and margin type display, error indication.
var table1 = table.new(position=title_position, columns=1, rows=4, bgcolor=title_bakcol, frame_color=color.black, frame_width=1, border_color=color.black, border_width=1)
if (barstate.islast and show_title) or (barstate.islast and isError>0)

    if isError==0
        table.cell(table_id=table1, column=0, row=0, text=oiName+coin, text_color=title_txtcol, text_size=title_size)
    if isError>0
        table.cell(table_id=table1, column=0, row=0, text=notshow==true?oiName+coin:oiName+coin+errorMessageOne, text_color=color.new(color.black,0), text_size=title_size)

//Adjusting Coin Margin Volume
if coin == "Coin"
    Mult = 0
    if syminfo.basecurrency == "BTC" or syminfo.basecurrency == "XBT"
        Mult := 100
    else
        Mult := 10
    s_open *=Mult
    s_high *=Mult
    s_low *=Mult
    s_close *=Mult

//Plots
plotcandle(chart_type=="Candle" ? s_open : na, chart_type=="Candle"? s_high : na, chart_type=="Candle"? s_low : na, chart_type=="Candle"? s_close : na, color=s_open > s_close ? candle_down_color : candle_up_color, wickcolor=candle_wickcolor, bordercolor=candle_bordercolor)
plot(chart_type=="Line" ? s_close : na, title='Line Chart',color=line_color)
plot(chart_type=="Histogram" and s_close-s_close[1]>0? s_close-s_close[1] : na, title='Line Chart',color=Histogram_up_color,style = plot.style_columns)
plot(chart_type=="Histogram" and s_close-s_close[1]<0? s_close-s_close[1] : na, title='Line Chart',color=Histogram_down_color,style = plot.style_columns)
