Last active
April 30, 2023 13:42
-
-
Save dharmatech/93e46fb611b22044acf76226c004c13f to your computer and use it in GitHub Desktop.
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
| # stacked bar of marketable debt maturities per day stacked with bills,bonds,tips | |
| # | |
| # Idea from https://twitter.com/dampedspring | |
| # Thanks to https://twitter.com/whip_n_spur for the help. | |
| # | |
| # Original thread: https://twitter.com/dampedspring/status/1652464875829084160 | |
| # | |
| # https://twitter.com/dharmatrade/status/1652534712462884864 | |
| $date_a = '2023-04-29' | |
| $date_b = '2024-01-01' | |
| $result = Invoke-RestMethod ('https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/debt/mspd/mspd_table_3_market?filter=maturity_date:gte:{0},maturity_date:lte:{1}&page[number]=1&page[size]=5000' -f $date_a, $date_b) | |
| # $result.data | ft * | |
| # $result.data.Count | |
| # $result.data | Group-Object security_class1_desc | Select-Object Count, Name | |
| # $result.data | Sort-Object maturity_date | Export-Csv C:\temp\mspd.csv -NoTypeInformation | |
| # $groups = $result.data | Group-Object maturity_date | Sort-Object Name | |
| $groups = $result.data | Where-Object record_date -EQ 2023-03-31 | Group-Object maturity_date | Sort-Object Name | |
| # $field = 'issued_amt' | |
| $field = 'outstanding_amt' | |
| # $result.data | Select-Object issued_amt, outstanding_amt | |
| # $result.data | Sort-Object maturity_date | Select-Object -First 300 | ft * | |
| # $result.data | Group-Object record_date | Sort-Object Name | |
| $table = foreach ($group in $groups) | |
| { | |
| # $group.Group | Where-Object security_class1_desc -EQ Notes | Measure-Object -Sum $field | |
| # [PSCustomObject]@{ | |
| # date = $group.Name | |
| # notes = [math]::Round(($group.Group | Where-Object security_class1_desc -EQ Notes | Measure-Object -Sum $field).Sum, 2) | |
| # bonds = [math]::Round(($group.Group | Where-Object security_class1_desc -EQ Bonds | Measure-Object -Sum $field).Sum, 2) | |
| # ips = [math]::Round(($group.Group | Where-Object security_class1_desc -EQ 'Inflation-Protected Securities' | Measure-Object -Sum $field).Sum, 2) | |
| # frn = [math]::Round(($group.Group | Where-Object security_class1_desc -EQ 'Floating Rate Notes' | Measure-Object -Sum $field).Sum, 2) | |
| # bmv = [math]::Round(($group.Group | Where-Object security_class1_desc -EQ 'Bills Maturity Value' | Measure-Object -Sum $field).Sum, 2) | |
| # } | |
| [PSCustomObject]@{ | |
| date = $group.Name | |
| notes = [math]::Round(($group.Group | Where-Object security_class1_desc -EQ Notes | ? outstanding_amt -NE null | Measure-Object -Sum $field).Sum, 2) | |
| bonds = [math]::Round(($group.Group | Where-Object security_class1_desc -EQ Bonds | ? outstanding_amt -NE null | Measure-Object -Sum $field).Sum, 2) | |
| ips = [math]::Round(($group.Group | Where-Object security_class1_desc -EQ 'Inflation-Protected Securities' | ? outstanding_amt -NE null | Measure-Object -Sum $field).Sum, 2) | |
| frn = [math]::Round(($group.Group | Where-Object security_class1_desc -EQ 'Floating Rate Notes' | ? outstanding_amt -NE null | Measure-Object -Sum $field).Sum, 2) | |
| bmv = [math]::Round(($group.Group | Where-Object security_class1_desc -EQ 'Bills Maturity Value' | ? outstanding_amt -NE null | Measure-Object -Sum $field).Sum, 2) | |
| } | |
| } | |
| $table | ft * | |
| # ---------------------------------------------------------------------- | |
| $json = @{ | |
| chart = @{ | |
| type = 'bar' | |
| data = @{ | |
| labels = $table.ForEach({ $_.date }) | |
| datasets = @( | |
| @{ label = 'Notes'; data = $table.ForEach({ $_.notes }); } | |
| @{ label = 'Bonds'; data = $table.ForEach({ $_.bonds }); } | |
| @{ label = 'Inflation-Protected Securities'; data = $table.ForEach({ $_.ips }); } | |
| @{ label = 'Floating Rate Notes'; data = $table.ForEach({ $_.frn }); } | |
| @{ label = 'Bills Maturity Value'; data = $table.ForEach({ $_.bmv }); } | |
| ) | |
| } | |
| options = @{ | |
| title = @{ display = $true; text = 'MSPD' } | |
| scales = @{ | |
| xAxes = @(@{ stacked = $true }) | |
| yAxes = @(@{ stacked = $true }) | |
| } | |
| } | |
| } | |
| } | ConvertTo-Json -Depth 100 | |
| $result_chart = Invoke-RestMethod -Method Post -Uri 'https://quickchart.io/chart/create' -Body $json -ContentType 'application/json' | |
| $id = ([System.Uri] $result_chart.url).Segments[-1] | |
| Start-Process ('https://quickchart.io/chart-maker/view/{0}' -f $id) | |
| # ---------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment