Leveraged Share Conversion Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Steversteves

// Update Details: Last update: March 21st, 2023
//                      Next Update: June 1st, 2023 

//@version=5
indicator("Leveraged Share Conversion Indicator")


// Select Leveraged Share 

    //UPRO
uprotospy = input.bool(false, title="UPRO to SPY", group="SPY") 
spytoupro = input.bool(false, title="SPY to UPRO", group="SPY") 
    //SPXS 
spxstospy = input.bool(false, title="SPXS to SPY", group="SPY") 
spytospxs = input.bool(false, title="SPY to SPXS", group="SPY") 

    //SPXU 
spxutospy = input.bool(false, title="SPXU to SPY", group="SPY") 
spytospxu = input.bool(false, title="SPY to SPXU", group="SPY")

    // QQQ
qqqtotqqq = input.bool(false, title="QQQ to TQQQ", group="QQQ")
tqqqtoqqq = input.bool(false, title="TQQQ to QQQ", group="QQQ")

    //BTC 
btctobito = input.bool(false, title="BTC to BITO", group="BTC")
bitotobtc = input.bool(false, title="BITO to BTC", group="BTC")
btctobiti = input.bool(false, title="BTC to BITI", group="BTC")

    // IWM 
iwmtotza = input.bool(false, title="IWM to TZA", group="IWM")
tzatoiwm = input.bool(false, title="TZA to IWM", group="IWM")
iwmtotna = input.bool(false, title="IWM to TNA", group="IWM")
tnatoiwm = input.bool(false, title="TNA to IWM", group="IWM")


    // Alternative Ticker 
usealtticker = input.bool(false, title="Use Alternative Ticker", group="Manual Conversion")
altnerativeticker = input.symbol("", title="Alternative Ticker", group="Manual Conversion")

    // Manual Conversion 
manualconversion = input.bool(false, title="Enable Manual Converison?", group="Manual Conversion")
manualclose = input.float(1, title="Manual Close Price", group="Manual Conversion")
manuallookback = input.int(10, title="Manual Conversion Lookback", group="Manual Conversion", tooltip="This is used for the correlation function") 


// Ticker Inputs 

btcclose = request.security("COINBASE:BTCUSD", "60", close, lookahead=barmerge.lookahead_on) 
uproclose = request.security("BATS:UPRO", "60", close, lookahead=barmerge.lookahead_on)
spxuclose = request.security("BATS:SPXU", "60", close, lookahead=barmerge.lookahead_on)
spxsclose = request.security("BATS:SPXS", "60", close, lookahead=barmerge.lookahead_on)   
spyclose = request.security("BATS:SPY", "60", close, lookahead=barmerge.lookahead_on) 
bitoclose = request.security("BATS:BITO", "60", close, lookahead=barmerge.lookahead_on) 
biticlose = request.security("BATS:BITI", "60", close, lookahead=barmerge.lookahead_on) 
qqqclose = request.security("BATS:QQQ", "60", close, lookahead=barmerge.lookahead_on) 
tqqclose = request.security("BATS:TQQQ", "60", close, lookahead=barmerge.lookahead_on) 
tnaclose = request.security("BATS:TNA", "60", close, lookahead=barmerge.lookahead_on) 
iwmclose = request.security("BATS:IWM", "60", close, lookahead=barmerge.lookahead_on) 
tzaclose = request.security("BATS:TZA", "60", close, lookahead=barmerge.lookahead_on) 
tickerclose = request.security(altnerativeticker, "60", close, lookahead=barmerge.lookahead_on)
curclose  = request.security(syminfo.ticker, "60", close, lookahead=barmerge.lookahead_on)
// Colours 

color red = color.new(color.red, 0) 
color green = color.new(color.green,0) 
color white = color.new(color.white, 0)
color blue = color.new(color.blue, 0) 
color black = color.new(color.black, 0)

// Variables 
var tickerucl = 0.0 
var ticker = 0.0 
var tickerlcl = 0.0 
string tickerid = na 
var se = 0.0
var conversionresult = ticker 
var currentclose = 0.0 
var currentcorrelation = 0.0 
// UPRO calculcations 

uprotospyconv = (uproclose * 2.144) + 312.098 // 5.65 
spytouproconv = (spyclose * 0.451) - 139.301 // 2.59 

if uprotospy 
    ticker := uprotospyconv 
    tickerucl := uprotospyconv + 5.65
    tickerlcl := uprotospyconv - 5.65 
    tickerid := "Upro to SPY"
    se := 5.65 
    currentclose := spyclose 
    currentcorrelation := ta.correlation(uproclose, spyclose, 10) 

if uprotospy and manualconversion 

    ticker := (manualclose * 2.144) + 312.098 
    tickerucl := ticker + 5.65 
    tickerlcl := ticker - 5.65 
    

if spytoupro 
    ticker := spytouproconv 
    tickerucl := spytouproconv + 2.59
    tickerlcl := spytouproconv - 2.59
    tickerid := "SPY to UPRO" 
    se := 2.59 
    currentclose := uproclose 
    currentcorrelation := ta.correlation(uproclose, spyclose, 10) 

if spytoupro and manualconversion 
    ticker := (manualclose * 0.451) - 139.301 
    tickerucl := ticker + 2.59 
    tickerlcl := ticker - 2.59 

// SPXS 

spytospxscon = (spyclose * -0.198) + 99.287 // SE 0.81 
spxstospycon = (spxsclose * -4.633) + 491.451 // 3.94 

if spytospxs 
    ticker := spytospxscon 
    tickerucl := spytospxscon + 0.81 
    tickerlcl := spytospxscon - 0.81
    tickerid := "SPY to SPXS"
    se := 0.81 
    currentclose := spxsclose 
    currentcorrelation := ta.correlation(spyclose, spxsclose, 10)

if spytospxs and manualconversion 
    ticker := (manualclose * -0.198) + 99.287 
    tickerucl := ticker + 0.81 
    tickerlcl := ticker - 0.81 

if spxstospy 
    ticker := spxstospycon 
    tickerucl := spxstospycon + 3.94 
    tickerlcl := spxstospycon - 3.94 
    tickerid := "SPXS to SPY" 
    se := 3.94 
    currentclose := spyclose 
    currentcorrelation := ta.correlation(spxsclose, spyclose, 10) 

if spxstospy and manualconversion  
    ticker := (manualclose * -4.633) + 491.451 
    tickerucl := ticker + 3.94
    tickerlcl := ticker - 3.94 


// SPXU

spytospxucon = (spyclose * -0.134) + 69.466 // 1
spxutospycon = (spxuclose * -6.054) + 493.793 // 6.70

if spytospxu 
    ticker := spytospxucon 
    tickerucl := spytospxucon + 1 
    tickerlcl := spytospxucon - 1 
    tickerid := "SPY to SPXU" 
    se := 1 
    currentclose := spxuclose 
    currentcorrelation := ta.correlation(spyclose, spxuclose, 10)

if spytospxu and manualconversion
    ticker := (manualclose * -0.134) + 69.466
    tickerucl := ticker + 1 
    tickerlcl := ticker - 1 

if spxutospy 
    ticker := spxutospycon 
    tickerucl := spxutospycon + 6.70 
    tickerlcl := spxutospycon  - 6.70 
    tickerid := "SPXU to SPY" 
    se := 6.70 
    currentclose := spyclose 
    currentcorrelation := ta.correlation(spyclose, spxsclose, 10)

if spxutospy and manualconversion 
    ticker := (manualclose * -6.054) + 493.793 
    tickerucl := ticker + 6.70
    tickerlcl := ticker - 6.70 

// BTC to BITO 

btctobitocon = (btcclose * -0.002) + 69.983 // se 2.82 
bitotobtccon = (bitoclose * -420.074) + 35408.272 // 1407.78

if btctobito 
    ticker := btctobitocon 
    tickerucl := btctobitocon + 2.82 
    tickerlcl := btctobitocon - 2.82 
    tickerid := "BTC to BITO" 
    se := 2.82 
    currentclose := bitoclose 
    currentcorrelation := ta.correlation(btcclose, bitoclose, 10)

if btctobito and manualconversion 
    ticker := (manualclose * -0.002) + 69.983 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 

if bitotobtc 
    ticker := bitotobtccon 
    tickerucl := bitotobtccon + 1407.78 
    tickerlcl := bitotobtccon - 1407.78 
    tickerid := "BITO to BTC" 
    //conversionresult := ticker
    se := 1407.78
    currentclose := btcclose 
    currentcorrelation := ta.correlation(btcclose, bitoclose, 10)
// BTC to BITI 

if btctobiti 
    se := 2.76
    ticker := (btcclose * -0.00166236589174558) + 69.6339750232392
    tickerucl := ticker + se 
    tickerlcl := ticker - se 
    tickerid := "BTC to BITI" 
    currentclose := biticlose 
    currentcorrelation := ta.correlation(btcclose, bitoclose, 10)

if btctobiti and manualconversion 
    ticker := (manualclose * -0.00166236589174558) + 69.6339750232392
    tickerucl := ticker + se 
    tickerlcl := ticker - se 


// QQQ to TQQQ 

var qqqtotqqqbase = 0.527562726082778 
var qqqtotqqqint = -128.128467285399 //se 3.51 

var tqqqtoqqqbase = 1.84883620557836 
var tqqqtoqqqint = 244.976170444663 

if qqqtotqqq 
    se := 3.51 
    ticker := (qqqclose * qqqtotqqqbase) + qqqtotqqqint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 
    currentclose := tqqclose
    tickerid := "QQQ to TQQQ" 
    currentcorrelation := ta.correlation(qqqclose, tqqclose, 10)

if qqqtotqqq and manualconversion 
    ticker := (manualclose * qqqtotqqqbase) + qqqtotqqqint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 

if tqqqtoqqq 
    se := 6.58 
    ticker := (tqqclose * tqqqtoqqqbase) + tqqqtoqqqint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 
    tickerid := "TQQQ to QQQ"
    currentclose := qqqclose
    currentcorrelation := ta.correlation(qqqclose, tqqclose, 10)

if tqqqtoqqq and manualconversion 
    ticker := (manualclose * tqqqtoqqqbase) + tqqqtoqqqint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 

// IWM 

tnatoiwmbase = 1.0173534935394 
tnatoiwmint = 142.159147225345 // se 3.89 

iwmtotnabase = 0.860707288878205
iwmtotnaint = -117.118398640605 //se 3.58

if tnatoiwm 

    se := 3.89 
    ticker := (tnaclose * tnatoiwmbase) + tnatoiwmint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 
    tickerid := "TNA to IWM" 
    currentclose := iwmclose 
    currentcorrelation := ta.correlation(tnaclose, iwmclose, 10)

if tnatoiwm and manualconversion 
    ticker := (manualclose * tnatoiwmbase) + tnatoiwmint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 

if iwmtotna 
    se := 3.58 
    ticker := (iwmclose * iwmtotnabase) + iwmtotnaint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 
    tickerid := "IWM to TNA" 
    currentclose := tnaclose 
    currentcorrelation := ta.correlation(iwmclose, tnaclose, 10) 

if iwmtotna and manualconversion 
    ticker := (manualclose * iwmtotnabase) + iwmtotnaint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 

// TZA 

tzatoiwmbase = -2.61817049480552
tzatoiwmint = 286.538707884321 // se 14.79 

iwmtotzabase = -0.190938510381163 
iwmtotzaint = 71.2387218412078 // se 3.99 

if tzatoiwm 
    se := 14.79 
    ticker := (tzaclose * tzatoiwmbase) + tzatoiwmint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 
    tickerid := "TZA to IWM"
    currentclose := iwmclose 
    currentcorrelation := ta.correlation(tzaclose, iwmclose, 10) 

if tzatoiwm and manualconversion 
    ticker := (manualclose * tzatoiwmbase) + tzatoiwmint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 

if iwmtotza 
    se := 3.99 
    ticker := (iwmclose * iwmtotzabase) + iwmtotzaint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 
    tickerid := "IWM to TZA" 
    currentclose := tzaclose 
    currentcorrelation := ta.correlation(iwmclose, tzaclose, 10)

if iwmtotza and manualconversion 
    ticker := (manualclose * iwmtotzabase) + iwmtotzaint 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 

// Manual Conversion with undefined ticker, manual calculcation of linreg to avoid linreg function limitations 

//y Variable
a = tickerclose[1]
b = tickerclose[2]
c = tickerclose[3]
d = tickerclose[4]
e = tickerclose[5]
f = tickerclose[6]
g = tickerclose[7] 
h = tickerclose[8]
i = tickerclose[9]
j = tickerclose[10] 

a2 = math.pow(a, 2)
b2 = math.pow(b, 2) 
c2 = math.pow(c, 2) 
d2 = math.pow(d, 2) 
e2 = math.pow(e, 2) 
f2 = math.pow(f, 2) 
g2 = math.pow(g, 2) 
h2 = math.pow(h, 2) 
i2 = math.pow(i, 2) 
j2 = math.pow(j, 2) 

y = (a + b + c + d + e + f + g + h + i + j) 
y2 = (a2 + b2 + c2 + d2 + e2 + f2 + g2 + h2 + i2 + j2) 

// X Variable

aa = curclose[1]
bb = curclose[2]
cc = curclose[3]
dd = curclose[4]
ee = curclose[5]
ff = curclose[6]
gg = curclose[7] 
hh = curclose[8]
ii = curclose[9]
jj = curclose[10] 


aa2 = math.pow(aa, 2)
bb2 = math.pow(bb, 2) 
cc2 = math.pow(cc, 2) 
dd2 = math.pow(dd, 2) 
ee2 = math.pow(ee, 2) 
ff2 = math.pow(ff, 2) 
gg2 = math.pow(gg, 2) 
hh2 = math.pow(hh, 2) 
ii2 = math.pow(ii, 2) 
jj2 = math.pow(jj, 2) 

x = (aa + bb + cc + dd + ee + ff + gg + hh + ii + jj) 
x2 = (aa2 + bb2 + cc2 + dd2 + ee2 + ff2 + gg2 + hh2 + ii2 + jj2) 

//xy 

xy = (a * aa) + (b * bb) + (c * cc) + (d * dd) + (e * ee) + (f * ff) + (g * gg) + (h * hh) + (i * ii) + (j * jj)

b1 = xy - (x * y) / 10
bbb2 = x2 - (math.pow(x, 2) / 10)

slope = (b1 / bbb2)


abc = y - (slope * x) 
abc1 = abc / 10 



// Manual Correlation 

mancor = ta.correlation(tickerclose, curclose, manuallookback)

manualconversionresult = (curclose * slope) + abc1 

// Manual Standard Error 
se10 = manualconversionresult[10] - tickerclose[10]
se9 = manualconversionresult[9] - tickerclose[9]
se8 = manualconversionresult[8] - tickerclose[8]
se7 = manualconversionresult[7] - tickerclose[7]
se6 = manualconversionresult[6] - tickerclose[6]
se5 = manualconversionresult[5] - tickerclose[5]
se4 = manualconversionresult[4] - tickerclose[4]
se3 = manualconversionresult[3] - tickerclose[3]
se2 = manualconversionresult[2] - tickerclose[2]
se1 = manualconversionresult[1] - tickerclose[1]

se10rt = math.pow(se10, 2)
se9rt = math.pow(se9, 2) 
se8rt = math.pow(se8, 2) 
se7rt = math.pow(se7, 2) 
se6rt = math.pow(se6, 2) 
se5rt = math.pow(se5, 2) 
se4rt = math.pow(se4, 2) 
se3rt = math.pow(se3, 2) 
se2rt = math.pow(se2, 2) 
se1rt = math.pow(se1, 2) 

rtadd = (se10rt + se9rt + se7rt + se6rt + se5rt + se4rt + se3rt + se2rt + se1rt) 

r1 = rtadd / (10 - 2) 
r2 = math.sqrt(r1) 


if usealtticker 
    ticker := manualconversionresult 
    currentcorrelation := mancor 
    currentclose := tickerclose 
    se := r2 
    tickerucl := ticker + r2 
    tickerlcl := ticker - r2 
    tickerid := str.tostring(altnerativeticker)

if usealtticker and manualconversion 
    ticker := (manualclose * slope) + abc1 
    tickerucl := ticker + se 
    tickerlcl := ticker - se 
    



// Plot functions 
   

plot(tickerucl, color=green)
plot(ticker, color=white)
plot(tickerlcl, color=red)



showTable     = input.bool(true, "Show Table")
tablePosInput = input.string(title="Position", defval="Top Right", options=["Bottom Left", "Bottom Right", "Top Left", "Top Right"], tooltip="Select where you want the table to draw.")
var tablePos  = tablePosInput == "Bottom Left" ? position.bottom_left : tablePosInput == "Bottom Right" ? position.bottom_right : tablePosInput == "Top Left" ? position.top_left : tablePosInput == "Top Right" ? position.top_right : na

var dataTable = table.new(tablePos, columns = 9, rows = 8, border_color = color.black, border_width = 2)
if showTable
    table.cell(dataTable, 1, 1, text = "Ticker:", bgcolor = black, text_color = white)
    table.cell(dataTable, 2, 1, text =  str.tostring(tickerid), bgcolor = blue, text_color = white)
    table.cell(dataTable, 1, 2, text = "Conversion Result:", bgcolor = black, text_color = white) 
    table.cell(dataTable, 2, 2, text = str.tostring(math.round(ticker, 2)), bgcolor = blue, text_color=white)
    table.cell(dataTable, 1, 3, text = "Standard Error:", bgcolor = black, text_color=white)
    table.cell(dataTable, 2, 3, text = str.tostring(math.round(se,2)), bgcolor=blue, text_color=white)
    table.cell(dataTable, 1, 4, text = "+ Standard Error", bgcolor = black, text_color=white)
    table.cell(dataTable, 2, 4, text = str.tostring(math.round(tickerucl, 2)), bgcolor = blue, text_color=white)
    table.cell(dataTable, 1, 5, text = "- Standard Error", bgcolor = black, text_color=white)
    table.cell(dataTable, 2, 5, text = str.tostring(math.round(tickerlcl, 2)), bgcolor = blue, text_color=white)
    table.cell(dataTable, 1, 6, text = "Current Close", bgcolor = black, text_color = white) 
    table.cell(dataTable, 2, 6, str.tostring(math.round(currentclose)), bgcolor = blue, text_color = white)
    table.cell(dataTable, 1, 7, text = "Current Correlation", bgcolor = black, text_color = white)
    table.cell(dataTable, 2, 7, text = str.tostring(math.round(currentcorrelation,2)), bgcolor = blue, text_color = white)