Skip to content

Commit 3d227c6

Browse files
committed
Add fiscal data
1 parent 7a378c7 commit 3d227c6

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

notebooks/_config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# Learn more at https://jupyterbook.org/customize/config.html
33

44
title: Quantflow library
5-
author: Quantmind Team
6-
copyright: "2024"
5+
author: <a href="https://quantmind.com">quantmind</a>
6+
copyright: "2014-2025"
77
logo: assets/quantflow-light.svg
88

99
# Force re-execution of notebooks on each build.

notebooks/data/fiscal_data.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
jupytext:
3+
text_representation:
4+
extension: .md
5+
format_name: myst
6+
format_version: 0.13
7+
jupytext_version: 1.16.6
8+
kernelspec:
9+
display_name: Python 3 (ipykernel)
10+
language: python
11+
name: python3
12+
---
13+
14+
# Fiscal Data
15+
16+
```{code-cell} ipython3
17+
from quantflow.data.fiscal_data import FiscalData
18+
```
19+
20+
```{code-cell} ipython3
21+
fd = FiscalData()
22+
```
23+
24+
```{code-cell} ipython3
25+
data = await fd.securities()
26+
data
27+
```
28+
29+
```{code-cell} ipython3
30+
31+
```

quantflow/data/fiscal_data.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from dataclasses import dataclass
2+
from datetime import date, timedelta
3+
4+
import pandas as pd
5+
from fluid.utils.http_client import AioHttpClient
6+
7+
from quantflow.utils.dates import as_date
8+
9+
URL = (
10+
"https://www.federalreserve.gov/datadownload/Output.aspx?"
11+
"rel=H15&series=bf17364827e38702b42a58cf8eaa3f78&lastobs=&"
12+
)
13+
14+
maturities = [
15+
"month_1",
16+
"month_3",
17+
"month_6",
18+
"year_1",
19+
"year_2",
20+
"year_3",
21+
"year_5",
22+
"year_7",
23+
"year_10",
24+
"year_20",
25+
"year_30",
26+
]
27+
28+
29+
@dataclass
30+
class FiscalData(AioHttpClient):
31+
"""Fiscal Data API client.
32+
33+
THis class is used to fetch data from the
34+
[fiscal data api](https://fiscaldata.treasury.gov/api-documentation/)
35+
"""
36+
37+
url: str = "https://api.fiscaldata.treasury.gov/services/api/fiscal_service"
38+
39+
async def securities(self, record_date: date | None = None) -> pd.DataFrame:
40+
"""Get treasury constant maturities rates"""
41+
rd = as_date(record_date)
42+
pm = rd.replace(day=1) - timedelta(days=1)
43+
params = {"filter": f"record_date:eq:{pm.isoformat()}"}
44+
data = await self.get_all("/v1/debt/mspd/mspd_table_3_market", params)
45+
return pd.DataFrame(data)
46+
47+
async def get_all(self, path: str, params: dict[str, str]) -> list:
48+
"""Get all data from the API"""
49+
url = f"{self.url}{path}"
50+
payload = await self.get(url, params=params)
51+
data = payload["data"]
52+
return data

quantflow/utils/dates.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ def isoformat(date: str | date) -> str:
2222

2323
def start_of_day(dt: date | None = None) -> datetime:
2424
return as_utc(dt).replace(hour=0, minute=0, second=0, microsecond=0)
25+
26+
27+
def as_date(dt: date | None = None) -> date:
28+
if dt is None:
29+
return date.today()
30+
elif isinstance(dt, datetime):
31+
return dt.date()
32+
else:
33+
return dt

0 commit comments

Comments
 (0)