Last active
March 27, 2021 21:42
-
-
Save aholmes/f16b08489efc4abc1fb803b6a3456b0e to your computer and use it in GitHub Desktop.
Get your cost basis from you Robinhood order history page at https://robinhood.com/account/history?type=orders
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
/* | |
Scroll all the way down on https://robinhood.com/account/history?type=orders | |
Paste this into the dev console | |
`totals` holds your overall cost basis for each position you've held | |
Do note that this is just a quick bang out and likely isn't 100% correct | |
*/ | |
const headers = Array.from(document.querySelectorAll('[data-testid="rh-ExpandableItem-buttonContent"]')).map(e => e.querySelectorAll('h3, h3 ~ span')) | |
const action_regex = /(.+?) (Market|(Stop )?Limit|(\$\d+ (Call|Put) (\d+\/\d+\/\d+))) (Buy|Sell)/ | |
const price_regex = /(\$((\d|,)+\.\d+)|Canceled)/ | |
const amount_regex = /(\d+\.?\d*)/ | |
const reducer = (accum, val, currIdx) => | |
{ | |
// first iteraction has accum set to the first item from `map | |
// so reset accum to properly process that item | |
if (currIdx === 1) accum = reducer({}, | |
{ | |
ticker: accum.ticker, | |
action: accum.action, | |
price: accum.price, | |
amount: accum.amount | |
}, 0); | |
if (!val.price) val.price = 0; | |
let price = parseFloat(val.price.replace(/[^\d.]/g, '')) | |
if (!price || isNaN(price)) price = 0; // guards against "Canceled" | |
if (accum[val.ticker]) | |
{ | |
if (val.action == 'Buy') | |
{ | |
accum[val.ticker].price += price; | |
accum[val.ticker].amount += val.amount; | |
} | |
else if (val.action == 'Sell') | |
{ | |
accum[val.ticker].price -= price; | |
accum[val.ticker].amount -= val.amount; | |
} | |
} | |
else | |
{ | |
accum[val.ticker] = | |
{ | |
price: price, | |
amount: val.amount | |
} | |
} | |
return accum; | |
} | |
let cost_basis = headers.map(e => | |
{ | |
action_match = e[0].innerText.match(action_regex); | |
price_match = e[1].innerText.match(price_regex); | |
amount = e[2] | |
? parseFloat(e[2].innerText.match(amount_regex)) | |
: 0; // no amount - Canceled | |
return { | |
ticker: action_match[1], // Disney | |
action: action_match[7], // Buy/Sell/Canceled | |
price: price_match[1], // 1,123.45 | |
amount: amount | |
}; | |
}) | |
.reduce(reducer); | |
totals = Object.keys(cost_basis) | |
.sort((a,b) => a.localeCompare(b)) | |
.map(key => | |
({ | |
ticker: key, | |
cost_basis: cost_basis[key].amount | |
? cost_basis[key].price / cost_basis[key].amount | |
: 0 | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment