Legend High Positive Correlation High Negative Correlation Insignificant Correlation Equal Correlation (likely same pair)
PAIRS

CODE Block

$(document).ready(() => { console.log('jquery works'); const correlationApp = () => { $('#main-content').attr('width', '100% !important') $('#main-content').attr('margin', '0') let correlationArr = []; let xPairsArr = []; let pd; console.log('inside correlation app'); //////////// Getcache retrieves our data from the cache json file being served from our server.js file/////////////// const getCache = () => { console.log('inside get cache'); $.ajax({ method: 'GET', url: 'https://maketdata.a2hosted.com/correlation/cache', type: 'JSON' }).then((data) => { console.log('this is data', data.data.daily.sort()); pd = data.data.daily; splitPairs(); }); } /////End getCache//////////// ///////////////////// SplitPairs creates the our pairs that we are going to correlat against each other ///////////////////// const splitPairs = () => { let pairsArr = pd; // console.log('inside splitPairs', pairsArr); ////////////creating an array of just the pair names so that we can create a loop that will create every pair combination.///////////////////// $(pairsArr).each((i, d) => { // console.log('this is d', d.pair); xPairsArr.push(d.pair); d.price.pop() // if(d.price.indexOf([]) != -1) { // return d.filter(e=>e.length); // } }) xPairsArr.sort(); console.log('this is now pairsArr', pairsArr); // console.log('here is xpairsarr', xPairsArr); let yPairsArr = xPairsArr; // console.log('here is ypairsarr', yPairsArr); let firstPair; let secondPair; let i; // let arr= []; let pairLen = xPairsArr.length; // for (i = 0; i < pairLen; i++) { // for (let j = i + 1; j < pairLen; j++) { // // firstPair = xPairsArr[i]; // secondPair = xPairsArr[j]; // console.log('this is our pairs', firstPair, secondPair); // assignVals(firstPair, secondPair, pairsArr); // // arr.push(firstPair); // } // } // console.log('this is arr', arr); $(pairsArr).each((i, x) => { $(pairsArr).each((j, y) => { // if (x === y) { firstPair = x.pair; secondPair = y.pair; // console.log('our pairs', x, y); // console.log('firstPair', firstPair); // console.log('secondPair', secondPair); assignVals(firstPair, secondPair, pairsArr); // } }) }) } ///////End Split pairs////////// /////////////////////Assign vals function///////////////////// const assignVals = (firstPair, secondPair, pairsArr) => { // console.log('this is assignVals function', firstPair, secondPair); let xPair; let yPair; let lenArr; let xName; let yName; // console.log('before each'); $(pairsArr).each((i, d) => { // console.log('this is d', d); // console.log('this is i', i); if (d.pair === firstPair) { xPair = d; lenArr = d.price; xName = d.pair; } if (d.pair === secondPair) { yPair = d; yName = d.pair; } }) // console.log('this is xpair now', xPair); // // console.log('this is pd now', pd); // console.log('this is lenArr', lenArr); // // console.log('this is xPair now', xPair); // console.log('this is yPair now', yPair); let xSum = 0; let xSquare = 0; let ySum = 0; let ySquare = 0; let sumXY = 0; let xlen; let ylen; let len; // console.log('this is xpair.price', xPair.price); xlen = xPair.price.length; ylen = yPair.price.length; // //////////// Using Reduce to get our sum values////////////// xSum = xPair.price.reduce((pv, cv) => { // console.log('cv', cv); // console.log('cv parsed', parseFloat(cv)); // console.log('pv', pv); return pv + parseFloat(cv); }, 0) ySum = yPair.price.reduce((pv, cv) => { return pv + parseFloat(cv); }, 0) xSquare = xPair.price.reduce((pv, cv) => { return pv + Math.pow(parseFloat(cv), 2); }, 0) ySquare = yPair.price.reduce((pv, cv) => { return pv + parseFloat(Math.pow(cv, 2)); }, 0) sumXY = xPair.price.reduce((pv, cv, i) => { return pv + parseFloat(cv * yPair.price[i]); }, 0) // console.log('this is x pair', xPair); // console.log('this is y pair', yPair); // console.log('this is sumxysqure', sumXY); // console.log('this is xSum', xSum); // console.log('this is ySum', ySum); // console.log('this is xsquare', xSquare); // console.log('this is ysquare', ySquare); // console.log('this is xlen', xlen); // console.log('this is ylen', ylen); // console.log('this is lenArr', lenArr); // console.log('this is x name', xName); // console.log('this is y name', yName); // // xPriceArr.sum = xSum; // // xPriceArr.square = xSquare; // // yPriceArr.sum = ySum; // // yPriceArr.square = ySquare; // // // yPriceArr.push(2); // // // console.log('this is xpricearr', xPriceArr); // // console.log('this is ypricearr', yPriceArr); // ///////// checking if the number of data points for each array are equal and if not they are adjusted if (xlen === ylen) { len = xlen; // console.log('lengths are equal'); correlate(xSum, xSquare, ySum, ySquare, len, sumXY, xName, yName); } else { // console.log('lengths are not equal'); // // fixVals(xPriceArr, yPriceArr, xSum, xSquare, ySum, ySquare, xlen, sumXY) } } // End assignVals function ///////////Start of Correlat function ////////////////// const correlate = (xSum, xSquare, ySum, ySquare, len, sumXY, xName, yName) => { let corrObj = {}; // console.log('inside correlate'); // /////////////////////////////Numerator/////////////////////////// // console.log('this is len', len); // console.log('this is sumXY', sumXY); // console.log('this is xSum', xSum); // console.log('this is ySum', ySum); // console.log('this is ySquare', ySquare); // console.log('this is xSquare', xSquare); let sumxBySumy = xSum * ySum; // console.log('sumxbysumy', sumxBySumy); let lenBySumxy = len * sumXY; // console.log('lenbysumxy', lenBySumxy); let numerator = lenBySumxy - sumxBySumy; // console.log('numerator', numerator); ///////////////////////////End Numerator///////////////////////// //////////////////////////////Denominator /////////////////////////////// //////////////////First Denominator bracket /////////////// let lenByXsquare = len * xSquare; // console.log('lenbyxsquare', lenByXsquare); let sumofXsquared = Math.pow(xSum, 2); // console.log('sumofXsquared', sumofXsquared); let lenMinusSumXsquare = lenByXsquare - sumofXsquared; // console.log('lenMinussumxsquare', lenMinusSumXsquare); ////////////////////End First Denominator Bracket//////////////// ///////////////////Second Denominator Bracket ///////////////////// let lenBySumySquare = len * ySquare; // console.log('lenbysumysquare', lenBySumySquare); let sumofYsquared = Math.pow(ySum, 2) // console.log('sumofysquared', sumofYsquared); let lensumyMinusYsquare = lenBySumySquare - sumofYsquared; // console.log('lensumyminusysquare', lensumyMinusYsquare); let presqroot = lenMinusSumXsquare * lensumyMinusYsquare; // console.log('presqroot', presqroot); let denominator = Math.sqrt(presqroot); // console.log('denominator', denominator); //////////////////////////End Denominator/////////////////////////////////// let correlationCoefficient = numerator / denominator; // console.log('this is correlation coefficient', correlationCoefficient); corrObj.cf = correlationCoefficient; corrObj.xName = xName; corrObj.yName = yName; correlationArr.push(corrObj); // setTimeout(checkData, 12000); } ///End correlate function getCache() setTimeout(() => { // console.log('this is correlation arr pre sort', correlationArr); createTable(); }, 2000); // End timeout function/// // Begin Create Table function ///// const createTable = () => { let arrOne = [{ 'symbol': 'eur', 'value': 1 }, { 'symbol': 'gbp', 'value': 2 }, { 'symbol': 'aud', 'value': 3 }, { 'symbol': 'nzd', 'value': 4 }, { 'symbol': 'usd', 'value': 5 }, { 'symbol': 'cad', 'value': 6 }, { 'symbol': 'chf', 'value': 7 }, { 'symbol': 'jpy', 'value': 8 }]; // console.log('this is correlationArr', correlationArr, xPairsArr); let arrTwo = []; let value = 1; // Creates table heads for our chart $(arrOne).each((i, d) => { $(arrOne).each((j, e) => { if (d.value < e.value) { let corrPair = {}; currOne = d.symbol; currTwo = e.symbol; corrPair.symbol = currOne + currTwo corrPair.value = value; arrTwo.push(corrPair); value++; $('#thead tr').append(`${corrPair.symbol.toUpperCase()}`); $('#tbody').append(`${corrPair.symbol.toUpperCase()}`); } }) }) // console.log('this is arrTwo', arrTwo); correlationArr.sort((a, b) => { let axn = a.xName; let ayn = a.yName; let bxn = b.xName; let byn = b.yName; let axv = $.grep(arrTwo,(i,d)=>{ if(i.symbol == axn){ return i; } }); let bxv = $.grep(arrTwo,(i,d)=>{ if(i.symbol == bxn){ return i; } }); let ayv = $.grep(arrTwo,(i,d)=>{ if(i.symbol == ayn){ return i; } }); let byv = $.grep(arrTwo,(i,d)=>{ if(i.symbol == byn){ return i; } }); // console.log('this is values compared',axv[0].value, ayv[0].value,bxv[0].value,byv[0].value); if (axv[0].value < bxv[0].value) { return -1; } if (axv[0].value > bxv[0].value) { return 1; } if (ayv[0].value < byv[0].value) { return -1; } if (ayv[0].value > byv[0].value) { return 1; } return 0; }) let halfWayThrough = [] $(correlationArr).each((i,d)=>{ if($.inArray(d.xName, halfWayThrough) === -1 && $.inArray(d.yName, halfWayThrough)=== -1) halfWayThrough.push(d); }) console.log(halfWayThrough); // let arraySecondHalf = yourArray.slice(halfWayThough, yourArray.length); // let corrFilter = correlationArr.filter((i)=>{ // console.log('this is i', i); // let x = i.xName; // let y = i.yName; // if(indexOf i.xName) // console.log('this is x',x); // console.log('this s y', y); // }) // // console.log('this is corrfilter', corrFilter); let count = 1; // console.log('this is data', $('#tbody tr').data()) $('#tbody tr').each((i, x) => { $('#thead th').each((j,y)=>{ // console.log('this is x',$(x).data().xPairs); // console.log('this is y',$(y).data().yPairs); let xp = $(x).data().xPairs; let yp = $(y).data().yPairs; let cf; count ++ }) // console.log('this is thead', $('#audcad-column').data().yPairs); let col = `#${x}-column`; // console.log('this is x', x); // console.log(col); // console.log('this is column', column); let data = $(x).data().xPairs; // let column = $(`${data}-column`).data().yPairs; // console.log('this is column',column); // console.log('this is data', data); $(halfWayThrough).each((k, z) => { if (z.xName === data) { if (z.cf <= -.75) { $(`#${data}-row`).append(`${z.cf.toFixed(3)}`); } else if (z.cf === 1) { $(`#${data}-row`).append(`${z.cf.toFixed(3)}`); } else if (z.cf >= .75 && z.cf < 1) { $(`#${data}-row`).append(`${z.cf.toFixed(3)}`); } else { $(`#${data}-row`).append(`${z.cf.toFixed(3)}`); } } // }) }) }) // console.log('this is count', count); hideStuff(); $('#chart').show(); } const hideStuff = () => { //console.log('inside hide stuff'); let currNum = 0; let numPlaceSetter = 0; let maxNum = 27; $('.corr-item').each((i,d)=> { // console.log('this is currnum', currNum, 'this is numPlaceSetter', numPlaceSetter, 'this is maxNum', maxNum); if (currNum > numPlaceSetter) { if (currNum === maxNum) { $(d).html("").removeClass('bg-danger').removeClass('bg-success').removeClass('border'); // maxNum --; numPlaceSetter ++; currNum = -1; } else { $(d).html("").removeClass('bg-danger').removeClass('bg-success').removeClass('border'); } } // if (num) currNum ++; // console.log('this is d',d); }) } } // End CorrelationApp///////////// correlationApp(); }); //End jquery
>