Last active
June 5, 2021 16:10
-
-
Save jgonera/adadbc21591e1dddddeea8b5fff5e172 to your computer and use it in GitHub Desktop.
Save Robinhood transaction history as a CSV.
This file contains hidden or 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
// This is a naive HTML scraper, using the API might be a better idea (https://medium.com/@bartclaeys/how-to-export-your-robinhood-stocks-fc8245b3d118) | |
var timer = setInterval(() => { | |
window.scrollTo(0,document.body.scrollHeight); | |
}, 1000); | |
clearInterval(timer); | |
function parse() { | |
const results = []; | |
document.querySelectorAll('.rh-expandable-item-a32bb9ad').forEach((node) => { | |
if (node.nodeType === Node.ELEMENT_NODE) { | |
const items = node.querySelectorAll('.grid-3 > div'); | |
let submitted, filled, symbol, type, quantity, price, fees = "$0.00", add = false; | |
items.forEach((item) => { | |
if (item.nodeType === Node.ELEMENT_NODE) { | |
const label = item.querySelector(':scope > span')?.textContent; | |
const value = item.querySelector(':scope > div:last-child')?.textContent; | |
if (label === 'Status' && value === 'Filled') { | |
add = true; | |
} else if (label === 'Symbol') { | |
symbol = value; | |
} else if (label === 'Submitted') { | |
submitted = value; | |
} else if (label === 'Filled') { | |
filled = value; | |
} else if (label === 'Type') { | |
type = value; | |
} else if (label === 'Filled Quantity') { | |
[ quantity, price ] = value.split(/ shares? at /); | |
} else if (label === 'Regulatory Fee') { | |
fees = value; | |
} | |
} | |
}); | |
// Option trades have no symbol... | |
if (add && symbol) { | |
results.push({ | |
date: `${submitted}, ${filled}`, | |
symbol, | |
type, | |
quantity, | |
price, | |
fees, | |
}); | |
} | |
} | |
}); | |
return results; | |
} | |
function toCSV(results) { | |
return [ | |
'Date,Symbol,Type,Quantity,Price,Fees', | |
...results.map(({date, symbol, type, quantity, price, fees}) => | |
`"${date}","${symbol}","${type}","${quantity}","${price}","${fees}"`) | |
].join('\n'); | |
} | |
function download(content, fileName, contentType) { | |
const a = document.createElement('a'); | |
const file = new Blob([content], { | |
type: contentType | |
}); | |
a.href = URL.createObjectURL(file); | |
a.download = fileName; | |
a.click(); | |
} | |
download(toCSV(parse()), 'robinhood.csv', 'text/csv'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment