Created
December 11, 2021 14:15
-
-
Save nikolaybotev/8758bc7f7b67e9ca40ccfe0cf6f17ad3 to your computer and use it in GitHub Desktop.
Robinhood Crypto Transaction Download Script
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
(function () { | |
function toCtDate(event) { | |
const p2 = s => s.toString().length == 1 ? "0" + s : s; | |
return `${p2(event.getUTCMonth()+1)}/${p2(event.getUTCDate())}/${event.getUTCFullYear()} ${p2(event.getUTCHours())}:${p2(event.getUTCMinutes())}:${p2(event.getUTCSeconds())}`; | |
} | |
const coinMap = { | |
"Dogecoin": "DOGE", | |
"Ethereum": "ETH" | |
} | |
function parseHTML() { | |
let csv = "Date,Received Quantity,Received Currency,Sent Quantity,Sent Currency,Fee Amount,Fee Currency,Tag\n"; | |
const sections = document.querySelectorAll('section'); | |
for (let i = 0; i < sections.length; i++) { | |
const transactions = sections[i].querySelectorAll(':scope > div'); | |
for (let j = 0; j < transactions.length; j++) { | |
const transaction = transactions[j].querySelector(":scope div[data-testid=\"rh-ExpandableItem-content\"] div.grid-3"); | |
if (transaction === undefined) { | |
continue; | |
} | |
const detail = {}; | |
transaction.children.forEach(ch => { | |
detail[ch.children[0].textContent] = ch.children[2].children[0]; | |
}); | |
const stat = detail["Status"]?.textContent; | |
// Only filled transactions | |
if (stat != "Filled") { | |
continue; | |
} | |
// Skip stocks (crypto only) | |
if (detail["Symbol"] !== undefined) { | |
continue; | |
} | |
const date = detail["Filled"].textContent; | |
const type = detail["Type"].textContent; | |
const entered = detail["Entered Amount"].textContent; | |
const total = detail["Total Notional"]?.textContent; | |
const filled = detail["Filled Quantity"]; | |
const amount = filled.childNodes[0].textContent; | |
const unit = filled.childNodes[1].textContent.replace(/^ (.+) at $/, "$1"); | |
const unitPrice = filled.childNodes[2].textContent; | |
const ctDate = toCtDate(new Date(date)); | |
const usdAmt = (total ?? entered).replace("$", ""); | |
if (type.indexOf("Buy") != -1) { | |
recvQ = amount.replaceAll(",", ""); | |
recvC = coinMap[unit]; | |
sentQ = usdAmt.replaceAll(",", ""); | |
sentC = "USD"; | |
} else { | |
recvQ = usdAmt.replaceAll(",", ""); | |
recvC = "USD"; | |
sentQ = amount.replaceAll(",", ""); | |
sentC = coinMap[unit]; | |
} | |
csv += `${ctDate},${recvQ},${recvC},${sentQ},${sentC}\n` | |
} | |
} | |
return csv; | |
} | |
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(parseHTML(), 'transactions.csv', 'text/plain'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CoinTracker CSV format: https://community.cointracker.io/t/convert-any-csv-into-the-cointracker-csv-format/553