RODO Fair Value Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ©rodotoken

//@version=5
indicator('RODO Fair Value', overlay=true, shorttitle='RODOAS')

inv = input(50, 'My RODO tokens')
avp = input(6.70, 'My Average Price in USD')

trade = input(500, 'Trade value')
usdwu = input(68.63 , 'USD Weight (Ethereum) in %' )
usdwp = input(15.91 , 'USD Weight(Polygon) in %' )
ethw = input(5.66 , 'ETH Weight in %' )
bnbw = input(5.10 , 'BNB Weight in %' )
maticw = input(1.15 , 'MATIC Weight in %' )
hexw = input(1.33 , 'HEX Weight in %' )
uniw = input(1.09, 'UNI Weight in %' )
linkw = input(1.14, 'LINK Weight in %' )

rodo = nz(request.security("UNISWAP3ETH:RODOUSDC", timeframe.period, close))
rodo1 = nz(request.security("UNISWAP3POLYGON:RODOUSDC", timeframe.period, close))
rodo_eth = nz(request.security('UNISWAP3ETH:RODOWETH', timeframe.period, close))
eth = nz(request.security('UNISWAP3ETH:WETHUSDC', timeframe.period, close))
bnb = nz(request.security('BINANCE:BNBUSD', timeframe.period, close))
rodo_matic = nz(request.security('UNISWAP3POLYGON:RODOWMATIC', timeframe.period, close))
matic = nz(request.security('UNISWAP3POLYGON:WMATICUSDC', timeframe.period, close))
hex = nz(request.security('UNISWAP3POLYGON:HEXUSDC', timeframe.period, close))
uni = nz(request.security('UNISWAP3POLYGON:UNIUSDC', timeframe.period, close))
rodo_link = nz(request.security('UNISWAP3ETH:RODOLINK', timeframe.period, close))
link = nz(request.security('UNISWAP3ETH:LINKUSDC', timeframe.period, close))

// Calculating real time values 

rodoeth = rodo_eth * eth
rodobnb = (rodo/bnb[1]) * bnb
rodomatic1 = rodo_matic * matic 
rodomatic = (rodo/matic[1]) * matic 
rodohex = (rodo/hex[1]) * hex 
rodouni = (rodo/uni[1]) * uni 
rodolink = rodo_link* link 

//// Calculating USD Value
usdw = (usdwu + usdwp)/100
usdcp = (rodo*(usdwu/100)) + (rodo1*(usdwp/100))
actual = (rodo1 + rodo)/2

// Calculating fair value * weights 

fvw = usdcp + (rodoeth*(ethw/100)) + (rodobnb*(bnbw/100)) + (rodomatic*(maticw/100)) + (rodohex*(hexw/100)) + (rodouni*(uniw/100)) + (rodolink*(linkw/100))
mid = (actual + rodoeth  + rodomatic1 + rodohex + rodouni + rodolink + rodobnb)/7

// Calculating investment values 
invest = inv*fvw
entryv = inv*avp
profit_loss = invest - entryv

//Calculating returns 

roi = (fvw - actual)/actual*100
roieth = (fvw - rodoeth)/rodoeth*100
roimat= (fvw - rodomatic1)/rodomatic1*100
roibnb = (fvw - rodobnb)/rodobnb*100
roilink = (fvw - rodolink)/rodolink*100
roiuni = (fvw - rodouni)/rodouni*100
roihex = (fvw - rodohex)/rodohex*100

// calculating dolar value returns 
usdv = (roi * trade) /100
ethv = (roieth * trade) /100
matv = (roimat * trade) /100
bnbv = (roibnb * trade) /100
univ = (roiuni * trade) /100
linkv = (roilink * trade) /100
hexv = (roihex * trade) /100




// Decimal Truncation 
truncate(number, decimals) =>
    truncateFactor = math.pow(10, decimals)
    a = int(number * truncateFactor) / truncateFactor
    str.tostring(a)

usd_roi = truncate(roi, 2)
eth_roi = truncate(roieth, 2)
mat_roi = truncate(roimat, 2)
bnb_roi = truncate(roibnb, 2)
uni_roi = truncate(roiuni, 2)
hex_roi = truncate(roihex, 2)
link_roi = truncate(roilink, 2)


usd_ret = truncate(usdv, 2)
eth_ret = truncate(ethv, 2)
mat_ret = truncate(matv, 2)
bnb_ret = truncate(bnbv, 2)
uni_ret = truncate(univ, 2)
hex_ret = truncate(hexv, 2)
link_ret = truncate(linkv, 2)

investment = truncate(invest,0)
invret = truncate(profit_loss,0)


bgcol = roi >= 0 ? #01796F : #8A3324
ethcol = roieth >= 0 ? #01796F : #8A3324
maticcol = roimat >= 0 ? #01796F : #8A3324
bnbcol = roibnb >= 0 ? #01796F : #8A3324
unicol = roiuni >= 0 ? #01796F : #8A3324
hexcol = roihex >= 0 ? #01796F : #8A3324
linkcol = roilink >= 0 ? #01796F : #8A3324


fvcol = actual > fvw ? #8A3324 : #01796F
inv_status = invest > entryv ? ' $ Profit' : ' $ Loss'
inv_col = invest > entryv ? #01796F : #8A3324


    // Table customisation
var table chart = table.new(position.bottom_left, 8, 31, border_width=1)

if barstate.islast
    table.cell(chart, 0, 0, text='My Investment', bgcolor=inv_col, text_color=color.white, text_size = size.normal)
    table.cell(chart, 1, 0, text='USD', bgcolor=bgcol, text_color=color.white, text_size = size.normal)
    table.cell(chart, 2, 0, text='ETH', bgcolor=ethcol, text_color=color.white, text_size = size.normal)
    table.cell(chart, 3, 0, text='MATIC', bgcolor=maticcol, text_color=color.white, text_size = size.normal)
    table.cell(chart, 4, 0, text='BNB', bgcolor=bnbcol, text_color=color.white, text_size = size.normal)
    table.cell(chart, 5, 0, text='LINK', bgcolor=unicol, text_color=color.white, text_size = size.normal)
    table.cell(chart, 6, 0, text='UNI', bgcolor=hexcol, text_color=color.white, text_size = size.normal)
    table.cell(chart, 7, 0, text='HEX', bgcolor=linkcol, text_color=color.white, text_size = size.normal)
    
    table.cell(chart, 0, 1, text=str.tostring(investment)+ ' $', text_size = size.normal , bgcolor=inv_col, text_color=color.white)
    table.cell(chart, 1, 1, text=str.tostring(usd_roi)+'%', text_size = size.normal , bgcolor=color.black, text_color=color.white)
    table.cell(chart, 2, 1, text=str.tostring(eth_roi)+'%', text_size = size.normal, bgcolor=color.black, text_color=color.white)
    table.cell(chart, 3, 1, text=str.tostring(mat_roi)+'%', text_size = size.normal , bgcolor=color.black, text_color=color.white)
    table.cell(chart, 4, 1, text=str.tostring(bnb_roi)+'%', text_size = size.normal, bgcolor=color.black, text_color=color.white)
    table.cell(chart, 5, 1, text=str.tostring(uni_roi)+'%', text_size = size.normal, bgcolor=color.black, text_color=color.white)
    table.cell(chart, 6, 1, text=str.tostring(hex_roi)+'%', text_size = size.normal, bgcolor=color.black, text_color=color.white)
    table.cell(chart, 7, 1, text=str.tostring(link_roi)+'%', text_size = size.normal, bgcolor=color.black, text_color=color.white)
        
    table.cell(chart, 0, 2, text=str.tostring(invret)+ inv_status, text_size = size.normal , bgcolor=inv_col, text_color=color.white)
    table.cell(chart, 1, 2, text=str.tostring(usd_ret), text_size = size.normal , bgcolor=color.black, text_color=color.white)
    table.cell(chart, 2, 2, text=str.tostring(eth_ret), text_size = size.normal, bgcolor=color.black, text_color=color.white)
    table.cell(chart, 3, 2, text=str.tostring(mat_ret), text_size = size.normal , bgcolor=color.black, text_color=color.white)
    table.cell(chart, 4, 2, text=str.tostring(bnb_ret), text_size = size.normal, bgcolor=color.black, text_color=color.white)
    table.cell(chart, 5, 2, text=str.tostring(uni_ret), text_size = size.normal, bgcolor=color.black, text_color=color.white)
    table.cell(chart, 6, 2, text=str.tostring(hex_ret), text_size = size.normal, bgcolor=color.black, text_color=color.white)
    table.cell(chart, 7, 2, text=str.tostring(link_ret), text_size = size.normal, bgcolor=color.black, text_color=color.white)
    

    



plot(rodoeth, title="RODO/ETH", color = color.red, trackprice=true, display = display.none) 
plot(rodobnb, title = "RODO/BNB", color = color.gray, trackprice = false , display = display.none)
plot(rodomatic1, title = 'RODO/MATIC', color = color.yellow , display = display.none)
plot(rodohex, title = 'RODO/HEX', color = color.gray, display = display.none)
plot(rodouni, title = 'RODO/UNI', color = color.maroon, display = display.none)
plot(rodolink, title = 'RODO/LINK', color = color.gray, display = display.none)


plot(fvw, 'FAIR VALUE', color = fvcol, linewidth = 2, trackprice=true,display = display.all)
plot(actual, 'USDC VALUE', color = color.blue, linewidth = 2)
plot(mid, 'Arbitrage Value', color = color.red, linewidth = 1, trackprice=true , display = display.none)
