Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sort stocks by percent change or my holdings change
/*
Generally, don't run random JS in your browser console, especially on financial sites, but here we are
By default this sorts by Percent Change. If you uncomment the next line it sorts by myDelta (price x your shares)
Caveats:
- I'm not affiliated with Vanguard or any licensed financial advisor or tax preparer. I don't have a clue what's going on with your finances.
- The script assumes you did NOT trade today; it uses today's change and current shares
- Delta-sort does not handle penny stocks as well because the UI says 0.01 and we reverse-engineer from current balance
*/
let sortRule = 'pct';
// sortRule = 'myDelta';
let table = document.querySelector("#account-0-holdings-table tbody");
let nodes = table.querySelectorAll("tr");
let sortNodes = [];
let totalDelta = 0;
for (var n = 0; n < nodes.length; n++) {
let name = nodes[n].querySelector('[data-name="holding-ticker-link"]').textContent;
let pct = 1 * nodes[n].querySelector('[data-name="holding-change-percent"]').textContent.replace('−', '-').replace('%', '').replace('-0.00', '-0.0000001');
let dollarDelta = 1 * nodes[n].querySelector('[data-name="holding-change-amount"]').textContent.replace('−', '-').replace('$', '').replace('-0.00', '-0.0000001');
let myShares = 1 * nodes[n].querySelector('[data-name="holding-quantity"]').textContent;
let myBalance = 1 * nodes[n].querySelector('[data-name="holding-balance"]').textContent.replace('−', '-').replace('$', '').replace(',', '');
let myDelta = (Math.abs(dollarDelta) < 0.1)
? myBalance - myBalance / (1 + pct/100)
: dollarDelta * myShares;
totalDelta += myDelta;
sortNodes.push([n, name, pct, myDelta]);
}
sortNodes = sortNodes.sort((a, b) => {
if ((sortRule === 'pct') || (b[3] === 0 && a[3] === 0)) {
return b[2] - a[2];
} else {
return b[3] - a[3];
}
});
console.log(sortNodes);
table.innerHTML = '';
sortNodes.forEach(newNode => table.appendChild(nodes[newNode[0]]))
console.log('Delta for today: $' + totalDelta.toFixed(2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment