Daily Short Volume Ratio
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Snufkin420TC

//@version=5

indicator('Daily Short Volume Ratio', shorttitle="SVR", timeframe="D", timeframe_gaps=true)
smooth = input.bool(false, title = "Smooth")
leng = input.int(10, title = "Smoothing length")
showindex1 = input.bool(true, title = "Show index 1", group="Index tickers")
index1_sym = input.string(defval="SPY",title="Index 1", group="Index tickers")
showindex2 = input.bool(false, title = "Show index 2", group="Index tickers")
index2_sym = input.string(defval="DIA",title="Index 2", group="Index tickers")
showindex3 = input.bool(false, title = "Show index 3", group="Index tickers")
index3_sym = input.string(defval="IWM",title="Index 3", group="Index tickers")

//initialize values
sv_ratio_ticker = 0.0
sv_ratio_ticker1 = 0.0
sv_ratio_ticker2 = 0.0
sv_ratio_ticker3 = 0.0
sv_ratio_ticker4 = 0.0
sv_ratio_index1 = 0.0
sv_ratio_index2 = 0.0
sv_ratio_index3 = 0.0

// Grab quandle data for ticker from NYSE and NASDAQ exchange and combine; use chart symbol ..  1 day
sv_ticker_NASDAQ = request.quandl("FINRA/FNSQ_" + syminfo.ticker, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)
sv_ticker_NYSE = request.quandl("FINRA/FNYX_" + syminfo.ticker, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)
tv_ticker_NASDAQ = request.quandl("FINRA/FNSQ_" + syminfo.ticker, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)
tv_ticker_NYSE = request.quandl("FINRA/FNYX_" + syminfo.ticker, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)
sv_ticker = sv_ticker_NASDAQ + sv_ticker_NYSE
tv_ticker = tv_ticker_NASDAQ + tv_ticker_NYSE

// Grab quandle data for ticker from NYSE and NASDAQ exchange and combine; use chart symbol .. 2 days
sv_ticker_NASDAQ1 = request.quandl("FINRA/FNSQ_" + syminfo.ticker, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)[1]
sv_ticker_NYSE1 = request.quandl("FINRA/FNYX_" + syminfo.ticker, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)[1]
tv_ticker_NASDAQ1 = request.quandl("FINRA/FNSQ_" + syminfo.ticker, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)[1]
tv_ticker_NYSE1 = request.quandl("FINRA/FNYX_" + syminfo.ticker, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)[1]
sv_ticker1 = sv_ticker_NASDAQ + sv_ticker_NYSE + sv_ticker_NASDAQ1 + sv_ticker_NYSE1
tv_ticker1 = tv_ticker_NASDAQ + tv_ticker_NYSE + tv_ticker_NASDAQ1 + tv_ticker_NYSE1

// Grab quandle data for ticker from NYSE and NASDAQ exchange and combine; use chart symbol .. 3 days
sv_ticker_NASDAQ2 = request.quandl("FINRA/FNSQ_" + syminfo.ticker, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)[2]
sv_ticker_NYSE2 = request.quandl("FINRA/FNYX_" + syminfo.ticker, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)[2]
tv_ticker_NASDAQ2 = request.quandl("FINRA/FNSQ_" + syminfo.ticker, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)[2]
tv_ticker_NYSE2 = request.quandl("FINRA/FNYX_" + syminfo.ticker, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)[2]
sv_ticker2 = sv_ticker_NASDAQ + sv_ticker_NYSE + sv_ticker_NASDAQ1 + sv_ticker_NYSE1 + sv_ticker_NASDAQ2 + sv_ticker_NYSE2
tv_ticker2 = tv_ticker_NASDAQ + tv_ticker_NYSE + tv_ticker_NASDAQ1 + tv_ticker_NYSE1 + tv_ticker_NASDAQ2 + tv_ticker_NYSE2

// Grab quandle data for ticker from NYSE and NASDAQ exchange and combine; use chart symbol .. 4 days
sv_ticker_NASDAQ3 = request.quandl("FINRA/FNSQ_" + syminfo.ticker, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)[3]
sv_ticker_NYSE3 = request.quandl("FINRA/FNYX_" + syminfo.ticker, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)[3]
tv_ticker_NASDAQ3 = request.quandl("FINRA/FNSQ_" + syminfo.ticker, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)[3]
tv_ticker_NYSE3 = request.quandl("FINRA/FNYX_" + syminfo.ticker, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)[3]
sv_ticker3 = sv_ticker_NASDAQ + sv_ticker_NYSE + sv_ticker_NASDAQ1 + sv_ticker_NYSE1 + sv_ticker_NASDAQ2 + sv_ticker_NYSE2 + sv_ticker_NASDAQ3 + sv_ticker_NYSE3
tv_ticker3 = tv_ticker_NASDAQ + tv_ticker_NYSE + tv_ticker_NASDAQ1 + tv_ticker_NYSE1 + tv_ticker_NASDAQ2 + tv_ticker_NYSE2 + tv_ticker_NASDAQ3 + tv_ticker_NYSE3

// Grab quandle data for ticker from NYSE and NASDAQ exchange and combine; use chart symbol .. 5 days
sv_ticker_NASDAQ4 = request.quandl("FINRA/FNSQ_" + syminfo.ticker, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)[4]
sv_ticker_NYSE4 = request.quandl("FINRA/FNYX_" + syminfo.ticker, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)[4]
tv_ticker_NASDAQ4 = request.quandl("FINRA/FNSQ_" + syminfo.ticker, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)[4]
tv_ticker_NYSE4 = request.quandl("FINRA/FNYX_" + syminfo.ticker, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)[4]
sv_ticker4 = sv_ticker_NASDAQ + sv_ticker_NYSE + sv_ticker_NASDAQ1 + sv_ticker_NYSE1 + sv_ticker_NASDAQ2 + sv_ticker_NYSE2 + sv_ticker_NASDAQ3 + sv_ticker_NYSE3 + sv_ticker_NASDAQ4 + sv_ticker_NYSE4
tv_ticker4 = tv_ticker_NASDAQ + tv_ticker_NYSE + tv_ticker_NASDAQ1 + tv_ticker_NYSE1 + tv_ticker_NASDAQ2 + tv_ticker_NYSE2 + tv_ticker_NASDAQ3 + tv_ticker_NYSE3 + tv_ticker_NASDAQ4 + tv_ticker_NYSE4

// values to plot
sv_ratio_ticker := (smooth == false) ? (sv_ticker/tv_ticker) : ta.linreg(sv_ticker/tv_ticker, leng, 0)
sv_ratio_ticker1 := (smooth == false) ? (sv_ticker1/tv_ticker1) : ta.linreg(sv_ticker1/tv_ticker1, leng, 0)
sv_ratio_ticker2 := (smooth == false) ? (sv_ticker2/tv_ticker2) : ta.linreg(sv_ticker2/tv_ticker2, leng, 0)
sv_ratio_ticker3 := (smooth == false) ? (sv_ticker3/tv_ticker3) : ta.linreg(sv_ticker3/tv_ticker3, leng, 0)
sv_ratio_ticker4 := (smooth == false) ? (sv_ticker4/tv_ticker4) : ta.linreg(sv_ticker4/tv_ticker4, leng, 0)

// Convert index reference symbol into symbol for quandle
index1_NASDAQ = "FINRA/FNSQ_" + index1_sym  // convert to Nasdaq exchange for quandle
index1_NYSE = "FINRA/FNYX_" + index1_sym  // convert to NYSE exchange for quandle

// Grab quandle data for index reference from NYSE and NASDAQ exchange and combine
sv_index1_NASDAQ = request.quandl(index1_NASDAQ, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)
sv_index1_NYSE = request.quandl(index1_NYSE, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)
tv_index1_NASDAQ = request.quandl(index1_NASDAQ, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)
tv_index1_NYSE = request.quandl(index1_NYSE, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)
sv_index1 = sv_index1_NASDAQ + sv_index1_NYSE
tv_index1 = tv_index1_NASDAQ + tv_index1_NYSE

// Convert index reference symbol into symbol for quandle
index2_NASDAQ = "FINRA/FNSQ_" + index2_sym  // convert to Nasdaq exchange for quandle
index2_NYSE = "FINRA/FNYX_" + index2_sym  // convert to NYSE exchange for quandle

// Grab quandle data for index reference from NYSE and NASDAQ exchange and combine
sv_index2_NASDAQ = request.quandl(index2_NASDAQ, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)
sv_index2_NYSE = request.quandl(index2_NYSE, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)
tv_index2_NASDAQ = request.quandl(index2_NASDAQ, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)
tv_index2_NYSE = request.quandl(index2_NYSE, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)
sv_index2 = sv_index2_NASDAQ + sv_index2_NYSE
tv_index2 = tv_index2_NASDAQ + tv_index2_NYSE

// Convert index reference symbol into symbol for quandle
index3_NASDAQ = "FINRA/FNSQ_" + index3_sym  // convert to Nasdaq exchange for quandle
index3_NYSE = "FINRA/FNYX_" + index3_sym  // convert to NYSE exchange for quandle

// Grab quandle data for index reference from NYSE and NASDAQ exchange and combine  
sv_index3_NASDAQ = request.quandl(index3_NASDAQ, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)
sv_index3_NYSE = request.quandl(index3_NYSE, barmerge.gaps_off, index = 0, ignore_invalid_symbol = true)
tv_index3_NASDAQ = request.quandl(index3_NASDAQ, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)
tv_index3_NYSE = request.quandl(index3_NYSE, barmerge.gaps_off, index = 2, ignore_invalid_symbol = true)
sv_index3 = sv_index3_NASDAQ + sv_index3_NYSE
tv_index3 = tv_index3_NASDAQ + tv_index3_NYSE

// conditionals for plotting
if (showindex1 == true)
    sv_ratio_index1 := (smooth == false) ? (sv_index1/tv_index1) : ta.linreg(sv_index1/tv_index1, leng, 0)

if (showindex2 == true)
    sv_ratio_index2 := (smooth == false) ? (sv_index2/tv_index2) : ta.linreg(sv_index2/tv_index2, leng, 0)
    
if (showindex3 == true)
    sv_ratio_index3 := (smooth == false) ? (sv_index3/tv_index3) : ta.linreg(sv_index3/tv_index3, leng, 0)

//plot boundary bars
hbear5 = hline(0.0, linestyle=hline.style_solid, color=color.new(color.green,45), linewidth=1, editable=false)
hbear4 = hline(0.1, linestyle=hline.style_solid, color=color.new(color.green,55), linewidth=1, editable=false)
hbear3 = hline(0.2, linestyle=hline.style_solid, color=color.new(color.green,65), linewidth=1, editable=false)
hbear2 = hline(0.3, linestyle=hline.style_solid, color=color.new(color.green,75), linewidth=1, editable=false)
hbear1 = hline(0.4, linestyle=hline.style_solid, color=color.new(color.green,85), linewidth=1, editable=false)
hcenter = hline(0.5, linestyle=hline.style_dashed, color=color.new(color.gray,0), linewidth=1, editable=false)
hbull1 = hline(0.6, linestyle=hline.style_solid, color=color.new(color.red,85), linewidth=1, editable=false)
hbull2 = hline(0.7, linestyle=hline.style_solid, color=color.new(color.red, 75), linewidth=1, editable=false)
hbull3 = hline(0.8, linestyle=hline.style_solid, color=color.new(color.red,65), linewidth=1, editable=false)
hbull4 = hline(0.9, linestyle=hline.style_solid, color=color.new(color.red,55), linewidth=1, editable=false)
hbull5 = hline(1.0, linestyle=hline.style_solid, color=color.new(color.red,45), linewidth=1, editable=false)

//colors
col_index1 = color.new(color.gray, 30)
col_index2 = color.new(color.maroon, 30)
col_index3 = color.new(color.olive, 30)
col1 = color.new(color.lime, 0)
col2 = color.new(color.yellow, 0)
col3 = color.new(color.orange, 0)
col4 = color.new(color.red, 0)
col5 = color.new(color.fuchsia, 0)

//plots
plot(sv_ratio_ticker, title="SVR 1 day", color=col1)
plot(sv_ratio_ticker1, title="SVR 2 days", color=col2)
plot(sv_ratio_ticker2, title="SVR 3 days", color=col3)
plot(sv_ratio_ticker3, title="SVR 4 days", color=col4)
plot(sv_ratio_ticker4, title="SVR 5 days", color=col5)
plot(sv_ratio_index1, title="Index 1 SVR 1 day", color=col_index1)
plot(sv_ratio_index2, title="Index 2 SVR 1 day", color=col_index2)
plot(sv_ratio_index3, title="Index 3 SVR 1 day", color=col_index3)

// END