Skip to content

Instantly share code, notes, and snippets.

@abel30567
Last active January 4, 2019 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abel30567/611c52835f7db600a4144617781faa6a to your computer and use it in GitHub Desktop.
Save abel30567/611c52835f7db600a4144617781faa6a to your computer and use it in GitHub Desktop.
Top 20 Cryptocurrency Market Cap Weighted Algorithm
const request = require('request');
const Table = require('cli-table');
// Algorithm will run every 5 seconds
setInterval( () =>{
// API request to Coin Market Cap for top 20 cryptocurrencies
request('https://api.coinmarketcap.com/v1/ticker/', function(error, response, body) {
const chart = JSON.parse(body);
// The cap of how much of the portfolio could allocate to a certain cryptocurrency (0.05 = 5%)
// no cryptocurrency will surpass this cap amount in portfolio allocation.
const cap = 0.10;
const top20 = [];
// Place the top 20 cryptocurrencies to an array.
for (let i = 0; i < 20; i++) {
top20.push(chart[i]);
}
// Sum the top 20 cryptocurrencies market cap
let total20Cap = 0;
top20.forEach( crypto => {
total20Cap += Number(crypto.market_cap_usd);
});
const _table = new Table({
chars: { 'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗'
, 'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝'
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
, 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
});
_table.push(
['Cryptocurrency', 'Market Cap', 'Market Cap Ratio']
);
// Create the ratios of a cryptocurrency's market cap and the top 20 cryptocurrencies
const allocations = [];
top20.forEach( crypto => {
_table.push([crypto.name, crypto.market_cap_usd, (Number(crypto.market_cap_usd) / Number(total20Cap))]);
allocations.push({
'cryptocurrency': crypto.name,
'market_cap': crypto.market_cap_usd,
'ratio': (Number(crypto.market_cap_usd) / Number(total20Cap))
});
});
// Print out our results in the console
console.log("======= Top 20 Cryptocurrency Market Caps =================");
console.log(_table.toString());
// Perform Cap Weighted Algorithm
for(let i = 0; i < allocations.length; i++) {
let alloc = allocations[i];
// If the ratio of cryptocurrency market cap to top 20 cryptocurrency is greater than the cap
if (alloc.ratio > cap){
let overflow = Number(alloc.ratio - cap);
// Set the allocation of this cryptocurrency to the cap
alloc.ratio = cap;
// Create a new array to spread the appropriate allocation with the overflow from cryptocurrency
// that surpassed the cap.
let remainder = allocations.slice(i+1);
// Sum up the cryptocurrencies market cap that need their allocation to be calculated
let total_nested_cap = 0;
remainder.forEach( crypto => {
total_nested_cap += Number(crypto.market_cap);
});
// Give the remaining cryptocurrencies the overflow of allocation from this cryptocurrency based on
// the ratios of the remaining cryptocurrencies.
for(let j = 0; j < remainder.length; j++) {
let remainder_crypto = remainder[j];
let cap_fraction = Number(remainder_crypto.market_cap) / Number(total_nested_cap);
allocations[i+j+1].ratio += overflow * cap_fraction;
};
}
}
const table = new Table({
chars: { 'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗'
, 'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝'
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
, 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
});
table.push(
['Cryptocurrency', 'Market Cap', 'Portfolio Allocation']
);
// See if the allocations sum up to ~100%
let check = 0;
for( let k = 0; k< allocations.length; k++){
table.push([allocations[k].cryptocurrency, allocations[k].market_cap, allocations[k].ratio])
check += allocations[k].ratio;
}
console.log(table.toString());
});
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment