Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dharmatech/a2760b2906b9ab999e8c064ab8763662 to your computer and use it in GitHub Desktop.

Select an option

Save dharmatech/a2760b2906b9ab999e8c064ab8763662 to your computer and use it in GitHub Desktop.
$month = '2022-12-'
$rate = 0.83
$dates = @(
'2022-11-30'
'2022-12-07'
'2022-12-14'
'2022-12-21'
'2022-12-28'
'2023-01-04'
)
# ----------------------------------------------------------------------
function soma-mbs-get-asof ($date)
{
$file = 'soma-mbs-get-asof-{0}.json' -f $date
if (Test-Path $file)
{
Write-Host ('Retrieving from file {0}' -f $file)
Get-Content $file | ConvertFrom-Json
}
else
{
Write-Host ('Downloading {0}' -f $date)
$result = Invoke-RestMethod ('https://markets.newyorkfed.org/api/soma/mbs/get/asof/{0}.json' -f $date)
$result | ConvertTo-Json -Depth 100 | Out-File $file
$result
}
}
function get-sum ($text, $date)
{
$result = soma-mbs-get-asof $date
($result.soma.holdings | Where-Object securityDescription -match $text | Measure-Object -Property currentFaceValue -Sum).Sum
}
function get-change ($text, $a, $b)
{
$sum_a = get-sum $text $a
$sum_b = get-sum $text $b
$sum_b - $sum_a
}
# ----------------------------------------------------------------------
$gnma_i_change = $rate * (get-change 'GNMA I ' '2022-11-09' '2022-11-16')
$gld_change = $rate * (get-change 'FHLMCGLD' '2022-11-09' '2022-11-16')
$gnma_ii_change = $rate * (get-change 'GNMA II' '2022-11-16' '2022-11-23')
$umbs_change = $rate * (get-change 'UMBS' '2022-11-23' '2022-11-30')
'UMBS : {0,20}' -f ($umbs_change ).ToString('N')
'GNMA I + GOLD : {0,20}' -f ($gnma_i_change + $gld_change ).ToString('N')
'GNMA II : {0,20}' -f ($gnma_ii_change ).ToString('N')
'TOTAL : {0,20}' -f ($umbs_change + $gnma_i_change + $gld_change + $gnma_ii_change).ToString('N')
Write-Host
# ----------------------------------------------------------------------
$types = @(
[pscustomobject]@{ date = $month + '15'; type = 'GNMA I + GOLD' ; value = $gnma_i_change + $gld_change }
[pscustomobject]@{ date = $month + '20'; type = 'GNMA II' ; value = $gnma_ii_change }
[pscustomobject]@{ date = $month + '25'; type = 'UMBS' ; value = $umbs_change }
)
function loop ($dates)
{
if ($dates.Count -ge 2)
{
$items = $types.Where({ ($_.date -ge $dates[0]) -and ($_.date -le $dates[1]) })
$sum = ($items | Measure-Object -Property value -Sum).Sum
$str = if ($sum -eq $null) { '' } else { $sum.ToString('N') }
Write-Host ('{0} {1,20} {2,30}' -f $dates[1], $str, (($items | ForEach-Object { $_.type }) -join ', '))
loop ($dates | Select-Object -Skip 1)
}
}
loop $dates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment