This file contains 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
Param([string[]] $years, [switch]$display_chart_url, [switch]$save_iframe) | |
if ($years -eq $null) | |
{ | |
$years = @(Get-Date -Format 'yyyy') | |
} | |
# ---------------------------------------------------------------------- | |
function get-rrp-award-rate () | |
{ | |
$result = Invoke-RestMethod 'https://fred.stlouisfed.org/graph/fredgraph.csv?id=RRPONTSYAWARD' | |
$result | ConvertFrom-Csv | |
} | |
$result_rrp_award_rate = get-rrp-award-rate | Where-Object RRPONTSYAWARD -NE '.' | |
# ---------------------------------------------------------------------- | |
# $result = Invoke-RestMethod 'https://home.treasury.gov/resource-center/data-chart-center/interest-rates/daily-treasury-rates.csv/2022/all?type=daily_treasury_yield_curve&field_tdr_date_value=2022&page&_format=csv' | |
# $result_effr = Invoke-RestMethod 'https://markets.newyorkfed.org/api/rates/unsecured/effr/last/10.json' | |
# $result_effr.refRates[0].targetRateFrom | |
# | |
# $result_effr.refRates[0].targetRateTo | |
# ---------------------------------------------------------------------- | |
function get-fed-funds-upper () | |
{ | |
$result = Invoke-RestMethod 'https://fred.stlouisfed.org/graph/fredgraph.csv?id=DFEDTARU' | |
$result | ConvertFrom-Csv | |
} | |
function get-fed-funds-lower () | |
{ | |
$result = Invoke-RestMethod 'https://fred.stlouisfed.org/graph/fredgraph.csv?id=DFEDTARL' | |
$result | ConvertFrom-Csv | |
} | |
$fed_funds_upper = get-fed-funds-upper | |
$fed_funds_lower = get-fed-funds-lower | |
# ---------------------------------------------------------------------- | |
$table = @() | |
foreach ($year in $years) | |
{ | |
Write-Host "Retrieving year $year..." -ForegroundColor Yellow -NoNewline | |
$result = Invoke-RestMethod ('https://home.treasury.gov/resource-center/data-chart-center/interest-rates/daily-treasury-rates.csv/{0}/all?type=daily_treasury_yield_curve&field_tdr_date_value={0}&page&_format=csv' -f $year) | |
Write-Host 'done' | |
$table = $table + ($result | ConvertFrom-Csv) | |
} | |
foreach ($row in $table) | |
{ | |
$row.Date = Get-Date $row.Date -Format 'yyyy-MM-dd' | |
} | |
$table = $table | Sort-Object Date | |
foreach ($row in $table) | |
{ | |
$rrp = $result_rrp_award_rate.Where({ $_.DATE -le $row.Date }, 'Last')[0].RRPONTSYAWARD | |
$row | Add-Member -MemberType NoteProperty -Name RRP -Value ([decimal] $rrp).ToString('F') | |
} | |
# ---------------------------------------------------------------------- | |
# $table | Sort-Object Date | Select-Object -First 10 | ft * | |
# # $year = Get-Date -Format 'yyyy' | |
# # $year = Get-Date (Get-Date).AddDays(-365) -Format 'yyyy' | |
# # $year = 2020 | |
# $year = 2023 | |
# $result = Invoke-RestMethod ('https://home.treasury.gov/resource-center/data-chart-center/interest-rates/daily-treasury-rates.csv/{0}/all?type=daily_treasury_yield_curve&field_tdr_date_value={0}&page&_format=csv' -f $year) | |
# $table = $result | ConvertFrom-Csv | Sort-Object Date | |
# foreach ($row in $table) | |
# { | |
# $row.Date = Get-Date $row.Date -Format 'yyyy-MM-dd' | |
# } | |
# # foreach ($row in $table) | |
# # { | |
# # '{0} {1}' -f $row.Date, $result_rrp_award_rate.Where({ $_.DATE -le $row.Date }, 'Last')[0].DATE | |
# # } | |
# foreach ($row in $table) | |
# { | |
# $rrp = $result_rrp_award_rate.Where({ $_.DATE -le $row.Date }, 'Last')[0].RRPONTSYAWARD | |
# $row | Add-Member -MemberType NoteProperty -Name RRP -Value ([decimal] $rrp).ToString('F') | |
# } | |
# ---------------------------------------------------------------------- | |
function color ($a, $b) | |
{ | |
if ($b -gt $a) { 'Green' } | |
elseif ($b -lt $a) { 'Red' } | |
else { 'White' } | |
} | |
# 2022-12-09 3.80 3.81 4.13 4.31 4.54 4.72 4.72 4.33 4.07 3.75 3.69 3.57 3.82 3.56 | |
$header = 'Date RRP 1 Mo 2 Mo 3 Mo 4 Mo 6 Mo 1 Yr 2 Yr 3 Yr 5 Yr 7 Yr 10 Yr 20 Yr 30 Yr' | |
Write-Host $header | |
foreach ($row in $table | Sort-Object Date) | |
{ | |
Write-Host ('{0} ' -f $row.Date) -NoNewline | |
Write-Host ('{0,6}' -f ([decimal] $row.'RRP').ToString('F')) -NoNewline | |
Write-Host ('{0,6}' -f $row.'1 Mo') -ForegroundColor (color $row.'RRP' $row.'1 Mo') -NoNewline | |
Write-Host ('{0,6}' -f $row.'2 Mo') -ForegroundColor (color $row.'1 Mo' $row.'2 Mo') -NoNewline | |
Write-Host ('{0,6}' -f $row.'3 Mo') -ForegroundColor (color $row.'2 Mo' $row.'3 Mo') -NoNewline | |
Write-Host ('{0,6}' -f $row.'4 Mo') -ForegroundColor (color $row.'3 Mo' $row.'4 Mo') -NoNewline | |
Write-Host ('{0,6}' -f $row.'6 Mo') -ForegroundColor (color $row.'4 Mo' $row.'6 Mo') -NoNewline | |
Write-Host ('{0,6}' -f $row.'1 Yr') -ForegroundColor (color $row.'6 Mo' $row.'1 Yr') -NoNewline | |
Write-Host ('{0,6}' -f $row.'2 Yr') -ForegroundColor (color $row.'1 Yr' $row.'2 Yr') -NoNewline | |
Write-Host ('{0,6}' -f $row.'3 Yr') -ForegroundColor (color $row.'2 Yr' $row.'3 Yr') -NoNewline | |
Write-Host ('{0,6}' -f $row.'5 Yr') -ForegroundColor (color $row.'3 Yr' $row.'5 Yr') -NoNewline | |
Write-Host ('{0,6}' -f $row.'7 Yr') -ForegroundColor (color $row.'5 Yr' $row.'7 Yr') -NoNewline | |
Write-Host ('{0,6}' -f $row.'10 Yr') -ForegroundColor (color $row.'7 Yr' $row.'10 Yr') -NoNewline | |
Write-Host ('{0,6}' -f $row.'20 Yr') -ForegroundColor (color $row.'10 Yr' $row.'20 Yr') -NoNewline | |
Write-Host ('{0,6}' -f $row.'30 Yr') -ForegroundColor (color $row.'20 Yr' $row.'30 Yr') -NoNewline | |
Write-Host | |
} | |
Write-Host $header | |
# -------------------------------------------------------------------------------- | |
# $a = $elt.'RRP' | |
# $b = $elt.'1 Mo' | |
# $width = 1 | |
function area-section ($width, $a, $b) | |
{ | |
$a = [decimal] $a | |
$b = [decimal] $b | |
$width * [math]::Min($a, $b) + 1/2 * $width * [math]::Abs($a - $b) | |
} | |
# $elt = $table[0] | |
foreach ($elt in $table) | |
{ | |
# 1 * [math]::Min($elt.RRP, $elt.'1 Mo') + 1/2 * 1 * [math]::Abs($elt.RRP - $elt.'1 Mo') | |
# area-section(1, $elt.RRP, $elt.'1 Mo') + | |
# area-section(1, $elt.'1 Mo', $elt.'2 Mo') + | |
# $area = | |
# (area-section 1 ($elt. 'RRP') ($elt. '1 Mo')) + | |
# (area-section 1 ($elt. '1 Mo') ($elt. '2 Mo')) + | |
# (area-section 1 ($elt. '2 Mo') ($elt. '3 Mo')) + | |
# (area-section 1 ($elt. '3 Mo') ($elt. '4 Mo')) + | |
# (area-section 2 ($elt. '4 Mo') ($elt. '6 Mo')) + | |
# (area-section 6 ($elt. '6 Mo') ($elt. '1 Yr')) + | |
# (area-section 12 ($elt. '1 Yr') ($elt. '2 Yr')) + | |
# (area-section 12 ($elt. '2 Yr') ($elt. '3 Yr')) + | |
# (area-section 24 ($elt. '3 Yr') ($elt. '5 Yr')) + | |
# (area-section 24 ($elt. '5 Yr') ($elt. '7 Yr')) + | |
# (area-section 36 ($elt. '7 Yr') ($elt.'10 Yr')) + | |
# (area-section 120 ($elt.'10 Yr') ($elt.'20 Yr')) + | |
# (area-section 120 ($elt.'20 Yr') ($elt.'30 Yr')) | |
$area = | |
(area-section 1 ($elt. 'RRP') ($elt. '1 Mo')) + | |
(area-section 1 ($elt. '1 Mo') ($elt. '2 Mo')) + | |
(area-section 1 ($elt. '2 Mo') ($elt. '3 Mo')) + | |
# (area-section 1 ($elt. '3 Mo') ($elt. '4 Mo')) + | |
# (area-section 2 ($elt. '4 Mo') ($elt. '6 Mo')) + | |
(area-section 3 ($elt. '3 Mo') ($elt. '6 Mo')) + | |
(area-section 6 ($elt. '6 Mo') ($elt. '1 Yr')) + | |
(area-section 12 ($elt. '1 Yr') ($elt. '2 Yr')) + | |
(area-section 12 ($elt. '2 Yr') ($elt. '3 Yr')) + | |
(area-section 24 ($elt. '3 Yr') ($elt. '5 Yr')) + | |
(area-section 24 ($elt. '5 Yr') ($elt. '7 Yr')) + | |
(area-section 36 ($elt. '7 Yr') ($elt.'10 Yr')) + | |
(area-section 120 ($elt.'10 Yr') ($elt.'20 Yr')) + | |
(area-section 120 ($elt.'20 Yr') ($elt.'30 Yr')) | |
$elt | Add-Member -MemberType NoteProperty -Name area -Value $area -Force | |
} | |
# $table | |
# -------------------------------------------------------------------------------- | |
function download-fred-series ($series, $date) | |
{ | |
Write-Host ('Downloading {0} series since: {1}' -f $series, $date) -ForegroundColor Yellow | |
$result = Invoke-RestMethod ('https://fred.stlouisfed.org/graph/fredgraph.csv?id={0}&cosd={1}' -f $series, $date) | |
$data = @($result | ConvertFrom-Csv) | |
Write-Host ('Received {0} items' -f $data.Count) -ForegroundColor Yellow | |
$data | |
} | |
$sp500 = download-fred-series 'SP500' '2020-01-01' | |
# $sp500 | |
foreach ($elt in $table) | |
{ | |
$result = @($sp500 | ? DATE -EQ $elt.Date) | |
if ($result.Length -gt 0) | |
{ | |
$val = $result[0].SP500 | |
} | |
else | |
{ | |
$val = $null | |
} | |
# $val = [decimal] ($sp500 | ? DATE -EQ $elt.Date | % SP500) | |
$elt | Add-Member -MemberType NoteProperty -Name spx -Value $val -Force | |
} | |
# -------------------------------------------------------------------------------- | |
$items = $table | ? spx -ne '.' | |
$chart = @{ | |
type = 'line' | |
data = @{ | |
labels = $items | % Date | |
datasets = @( | |
@{ label = 'area'; data = $items | % area; fill = $false } | |
@{ label = 'SPX'; data = $items | % spx; yAxisID = 'Y2' } | |
) | |
} | |
options = @{ | |
title = @{ display = $true; text = 'USTS Area Under Yield Cuve' } | |
scales = @{ | |
yAxes = @( | |
@{ id = 'Y1'; position = 'left'; display = $true } | |
@{ id = 'Y2'; position = 'right'; display = $true } | |
) | |
} | |
} | |
} | |
$json = @{ | |
chart = $chart | |
} | ConvertTo-Json -Depth 100 | |
$result = Invoke-RestMethod -Method Post -Uri 'https://quickchart.io/chart/create' -Body $json -ContentType 'application/json' | |
$id = ([System.Uri] $result.url).Segments[-1] | |
if ($save_iframe) | |
{ | |
$html_template -f 'SPX Fair Value', $id > spx-fair-value-chart.html | |
} | |
if ($display_chart_url) | |
{ | |
Write-Host ('SPX Fair Value: https://quickchart.io/chart-maker/view/{0}' -f $id) -ForegroundColor Yellow | |
} | |
else | |
{ | |
Start-Process ('https://quickchart.io/chart-maker/view/{0}' -f $id) | |
} | |
$table | ft * | |
# -------------------------------------------------------------------------------- | |
exit | |
. .\area-under-usts-yield-curve.ps1 -years 2021, 2022, 2023 | |
# -------------------------------------------------------------------------------- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment