Skip to content

Instantly share code, notes, and snippets.

@amandadebler
Last active August 26, 2019 10:19
Show Gist options
  • Save amandadebler/d93898fc8605b2617fd762902a28bcfb to your computer and use it in GitHub Desktop.
Save amandadebler/d93898fc8605b2617fd762902a28bcfb to your computer and use it in GitHub Desktop.
S&P 500 weights and latest closing prices
$response = invoke-webrequest "https://www.slickcharts.com/sp500"
$table = $response.parsedhtml.body.childnodes[6].childnodes[1].childnodes[0].childnodes[0].childnodes[1].childnodes[0]
# Column Headers
$columns = foreach ($node in $table.childnodes[0].childnodes[0].childnodes) {$node.innertext}
$stockrows = $table.childnodes[1].childnodes
$stocks = foreach ($row in $stockrows) {
$attributes = @{}
for ($i=0; $i -lt $($columns.count); $i++) {
$attributes.Add($columns[$i],$row.childnodes[$i].innertext.trim())
}
[PSCustomObject]$attributes
}
$stocks
@amandadebler
Copy link
Author

Because buying non-US-domiciled ETFs is a bad idea if you're a US citizen, but EU residents can't buy US-domiciled ETFs, and I'm not yet slick enough to use IEX Cloud to figure out how to DIY a mini-S&P 500 fund out of the plain stocks (which we still can buy, either side of the ocean)

@amandadebler
Copy link
Author

In which I remember the existence of the classic C-style for loop

Usage suggestion: Get-SP500Summary.ps1 | Out-GridView

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment