Skip to content

Instantly share code, notes, and snippets.

@alfongj
Created December 31, 2020 00:04
Show Gist options
  • Save alfongj/9b5de1d6422cd4348147670753a55f11 to your computer and use it in GitHub Desktop.
Save alfongj/9b5de1d6422cd4348147670753a55f11 to your computer and use it in GitHub Desktop.
Export Robinhood trades to TSV
/*
* Instructions:
* 1. Go to https://robinhood.com/account/history in Chrome
* 2. Scroll down to the bottom to ensure all transactions you care about are rendered
* 3. Open the Chrome Dev Tools window (Windows — Ctrl + Shift + I, Mac — Option + Cmd + I)
* 4. Paste this code into the console
*/
let csv = "Section\tDate\tName\tTotal\tNumber of Shares/Contracts\tPrice per share\n";
const sections = document.querySelectorAll('section');
for (let i = 0; i < sections.length; i++) {
const sectionName = sections[i].querySelector('h2').innerText;
const transactions = sections[i].querySelectorAll(':scope > div')
for (let j = 0; j < transactions.length; j++) {
const transaction = transactions[j].children[0].children[0];
if (transaction == undefined) {
continue;
}
const date = transaction.children[0].children[0].innerText.split('\n')[1];
const name = transaction.children[0].children[0].children[0].textContent;
const type = transaction.children[0].children[1].children[0].innerText;
if ('Robinhood Gold' === name
|| 'Placed' === type
|| 'Canceled' === type
|| 'Rejected' === type ) {
continue;
}
const total = transaction.children[0].children[1].children[0]?.textContent.replace("+","");
const shares = transaction.children[0].children[1].children[2]?.textContent;
let pps = '';
let numberOfShares = '';
if (shares) {
const shareKeywords = ['share', 'shares', 'Contract', 'Contracts'];
for (let k = 0; k < shareKeywords.length; k++) {
let keyword = shareKeywords[k];
if (shares.includes(` ${keyword} at `)) {
pps = shares.split(` ${keyword} at `)[0];
numberOfShares = shares.split(` ${keyword} at `)[1];
break;
}
}
}
csv += `${sectionName}\t${date}\t${name}\t${total}\t${pps}\t${numberOfShares}\n`
}
}
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], {
type: contentType
});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
download(csv, 'transactions.tsv', 'text/plain');
//let testNode = document.querySelectorAll('section')[0].querySelectorAll(':scope > div')[0].children[0].children[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment