-
-
Save luiscarbonell/9ec2a6636659fde5e7cc792769e617e6 to your computer and use it in GitHub Desktop.
Top 20 Cryptocurrency Market Cap Weighted Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require('lodash'); | |
const request = require('request'); | |
// Algorithm will run every 5 seconds | |
setInterval(() => { | |
// API request to Coin Market Cap for top 20 cryptocurrencies | |
request('https://api.coinmarketcap.com/v1/ticker/', (error, response, body) => { | |
const coins = 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; | |
// Place the top 20 cryptocurrencies to an array. | |
const top = _.slice(chart, 0, 20); | |
// Sum the top 20 cryptocurrencies market cap | |
const market_cap = _.sum(_.map(top, number => parseFloat(number))); | |
// Create the ratios of a cryptocurrency's market cap and the top 20 cryptocurrencies | |
const allocations = _.map(top, crypto => ({ | |
'cryptocurrency': crypto.name, | |
'market_cap': parseFloat(crypto.market_cap_usd), | |
'ratio': parseFloat(crypto.market_cap_usd) / parseFloat(market_cap) | |
})) | |
// Print out our results in the console | |
console.log(); | |
console.log("======= Top 20 Cryptocurrency Market Caps ================="); | |
console.log(); | |
console.log(allocations); | |
console.log(); | |
// Perform Cap Weighted Algorithm | |
_.times(allocations.length, index => { | |
// If the ratio of cryptocurrency market cap to top 20 cryptocurrency is greater than the cap | |
if(allocations[index].ratio > CAP) { | |
let overflow = allocations[index].ratio - CAP; | |
// Set the allocation of this cryptocurrency to the cap | |
allocations[index].ratio = CAP; | |
// Create a new array to spread the appropriate allocation with the overflow from cryptocurrency that surpassed the cap. | |
let remainder = _.slice(allocations, index + 1); | |
// Sum up the cryptocurrencies market cap that need their allocation to be calculated | |
let nested_cap = _.sum(_.map(remainder, crypto => crypto.market_cap)); | |
// Give the remaining cryptocurrencies the overflow of allocation from this cryptocurrency based on the ratios of the remaining cryptocurrencies. | |
_.times(remainder.length, rindex => { | |
let fraction = remainder[rindex].market_cap / nested_cap; | |
allocations[index+rindex+1] += overflow * fraction; | |
}); | |
} | |
}); | |
// See if the allocations sum up to ~100% | |
let check = _.sum(_.map(allocations, allocation => allocation.ratio)); | |
console.log(); | |
console.log("++++++ Market Cap Weighted Portfolio Allocation ++++++++++"); | |
console.log(); | |
console.log(allocations, check); | |
console.log(); | |
}); | |
}, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment