Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

amandadebler commented Aug 26, 2019

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

amandadebler commented Aug 26, 2019

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