Coin & market cap table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Streetcoin

//@version=5
indicator("BTC Dominance",shorttitle = "CMCT", precision = 12, overlay = true)
   //================================================\\
  //---->Custom function. truncate number means <-----\\ 
 // ---->to reduce or increase decimal numbers  <------\\
//======================================================\\

truncate(_number, _decimalPlaces) =>
    _factor = math.pow(10, _decimalPlaces)
    int(_number * _factor) / _factor

  //==================================\\
 // getting the daily percentage change\\
//======================================\\
var g_length = "This table gives you a qucik overview of how the coin on the chart is doing. It gives you the monthly, weekly and daily percentage change as well as what is happening in the overall market without having to go through numerous charts."
res         = input.timeframe(title="Time Frame", defval= "D",group = g_length,tooltip = "This cell is designed to only display the candle information from the time frame and market you choose")
res2        = input.timeframe(title="Time Frame Weekly", defval= "W", tooltip = "The week starts on a Monday. This information is last weeks data")
res3        = input.timeframe(title="Time Frame Monthly", defval= "M", tooltip = " the month starts on the first of the month, so this data is the last calander months data.")
market      = input.symbol(title="market", defval ="CRYPTOCAP:Total", tooltip = " This is specifically for the crypto markets.")
source      = input.source(title="Source", defval=close)
reference   = input.symbol(title="Reference Market", defval="CRYPTOCAP:TOTAL2", tooltip = "This is the cryptocap total without Bitcoin hence Total2, Total3 removes Bitcoin and Etherium.")
  //===================================\\
 //Get % change of reference data source\\
//=======================================\\

referenceData    = request.security(reference, res, source[barstate.isconfirmed ? 1 : 0])
marketData       = request.security(market, res, source[barstate.isconfirmed ? 1 : 0])
referenceChange  = ((marketData[1] - referenceData[1]) / marketData[1]) * 100
referenceChange2 = ((marketData[2] - referenceData[2]) / marketData[2]) * 100



  //==============================\\
 // Define custom security function\\
//==================================\\

f_sec(_market,_res,_exp)  => request.security(_market,res,_exp[barstate.isconfirmed ? 1 : 0])
f_sec1(_market,_res,_exp) => request.security(_market,res,_exp[barstate.isconfirmed ? 1 : 0])
f_sec2(_market,_res,_exp) => request.security(_market,res,_exp[barstate.isconfirmed ? 1 : 0])
f_sec3(_market,_res,_exp) => request.security(_market,res,_exp[barstate.isconfirmed ? 1 : 0])
f_sec4(_market,_res,_exp) => request.security(_market,res,_exp[barstate.isconfirmed ? 1 : 0])
f_sec5(_market,_res,_exp) => request.security(_market,res,_exp[barstate.isconfirmed ? 1 : 0])

  //=============================================\\
 // get the percentage of the daiy time frame only\\
//=================================================\\
currentChange = ta.change(close [1]- close[2]) / 100
mktchange     = ta.change(close[1]) / close[2] * 100
rounding      = ta.change(close[1]  / 1000000000)
rounding1      = ta.change(close[1]  / 1000000)
rounding2      = ta.change(close[1]  / 1000)
rounding3      = ta.change(close[1]  / 1)
morning       = (close[1]  / 1000000000000) 

  //=============\\
 // get percentage\\
//=================\\

percent    = f_sec(market, res, mktchange)
dpercent   = f_sec1(syminfo.tickerid, res,mktchange)
cmcChange  = f_sec2(market, res, rounding)
cmcOpen    = f_sec(market, res, morning)
baseData   = f_sec4(reference, res, referenceChange)
dataChange = f_sec5(reference, res, referenceChange2)


  //===========\\
 // create table\\
//===============\\ 
 
var table mytable = table.new(position.top_right, 1, 7, border_width=1)

  //=========\\
 // fill cells\\
//=============\\ 
 
if barstate.islast 
    txt1 = 'Charts Daily\npercentage change\n'  + str.tostring(truncate(dpercent, 2)) + '%'
    txt2 = "Todays BTC Dominance\n" + str.tostring(truncate(baseData, 2)) + "%\n Yesterdays BTC Dominance\n" + str.tostring(truncate(dataChange, 2)) + "%\nFrom CryptoCap"
//    txt3 = "BTC Dominance\nfrom yesterday\n"  + str.tostring(truncate(dataChange, 2)) + "%"
    txt4 = 'Crypto Cap Total\n daily open\n $' + str.tostring(truncate(cmcOpen, 12)) +  "00 T"
    txt5 = 'Crypto Cap Total\n daily dollar change \n $' + str.tostring(truncate(cmcChange, 3)) +  " B"
    txt6 = "Crypto Cap Total daily\npercentage change\n"  + str.tostring(truncate(percent, 2)) + '%'
    
    
  //==========================\\
 //----> color my table <------\\
//==============================\\
    table.cell(mytable, 0, 1, text=txt1, bgcolor=dpercent > 0 ? color.green : color.red, text_color=color.black)
    table.cell(mytable, 0, 2, text=txt2, bgcolor=baseData > dataChange ? color.green : color.red, text_color=color.white)
//    table.cell(mytable, 0, 3, text=txt3, bgcolor=percent > 0 ? color.green : color.red, text_color=color.white)
    table.cell(mytable, 0, 4, text=txt4, bgcolor=percent > 0 ? color.green : color.red, text_color=color.black)
    table.cell(mytable, 0, 5, text=txt5, bgcolor=percent > 0 ? color.green : color.red, text_color=color.white)
    table.cell(mytable, 0, 6, text=txt6, bgcolor=percent > 0 ? color.green : color.red, text_color=color.black)
    


// Define custom security function

f_sec6(_market,_res2,_exp) => request.security(_market,res2,_exp[barstate.isconfirmed ? 1 : 0])
f_sec7(_market,_res3,_exp) => request.security(_market,res3,_exp[barstate.isconfirmed ? 1 : 0])
f_sec8(_market,_res3,_exp) => request.security(_market,res3,_exp[barstate.isconfirmed ? 1 : 0])
f_sec9(_market,_res3,_exp) => request.security(_market,res3,_exp[barstate.isconfirmed ? 1 : 0])
f_sec10(_market,_res2,_exp) => request.security(_market,res2,_exp[barstate.isconfirmed ? 1 : 0])
f_sec11(_market,_res3,_exp) => request.security(_market,res2,_exp[barstate.isconfirmed ? 1 : 0])




//secuurity functions for table 2

dpercent1  = f_sec1(syminfo.tickerid, res2,mktchange)
dpercent2  = f_sec6(syminfo.tickerid, res2,mktchange)
cmcChange2 = f_sec6(syminfo.tickerid, res2,rounding)
dpercent3  = f_sec7(syminfo.tickerid, res3,mktchange)
cmcChange3 = f_sec8(syminfo.tickerid, res3,rounding1)
cmcChange4 = f_sec9(syminfo.tickerid, res3,rounding2)
cmcChange5 = f_sec10(syminfo.tickerid, res2,rounding3)
cmcChange6 = f_sec11(syminfo.tickerid, res2,rounding1)


// Create table
var table myTable = table.new(position.bottom_right, 5, 2, border_width=1)

// fill cells
if barstate.islast 
    txt1 = "Charts previous Weeks\npercentage change "  + str.tostring(truncate(dpercent2, 2)) + "%"//\n The dollar change is\n$" + str.tostring(cmcChange5) +  " M"
    txt2 = "Charts previous Months\npercentage change "  + str.tostring(truncate(dpercent3, 2)) + "%"//\n The dollar change is\n$" + str.tostring(cmcChange4) +  " M"
    
    
  //==========================\\
 //----> color my table <------\\
//==============================\\
    table.cell(myTable, 0, 1, text=txt1, bgcolor=dpercent2 > 0 ? color.green : color.red, text_color=color.black) 
    table.cell(myTable, 1, 1, text=txt2, bgcolor=dpercent3 > 0 ? color.green : color.red, text_color=color.white)


plot(na)
