-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathustCusipPanel.py
More file actions
1316 lines (1119 loc) · 46.4 KB
/
Copy pathustCusipPanel.py
File metadata and controls
1316 lines (1119 loc) · 46.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
ustCusipPanel - U.S. Treasury CUSIP Panel Data Generator
=========================================================
This module fetches and processes Treasury auction data from the U.S. Treasury's
Fiscal Data API to create a complete CUSIP-date panel with the following features:
- Business date completion (no missing dates), ideal for merging
- Tenor and vintage classifications
- Cumulative issuance tracking
- Auction markers and reopening indicators
- Maturity date, coupon, and CUSIP information
The data is cached locally for efficient subsequent access.
Main Function
-------------
ustCusipPanel(startDate, endDate, silent, forceDownload) -> pl.DataFrame
Dependencies
------------
- polars: High-performance DataFrame library (required)
- requests: HTTP library for API calls (required)
- platformdirs: Cross-platform user directories (required)
Author: Corey Garriott
License: Unlicense (Public Domain)
"""
import sys
from pathlib import Path
from datetime import date, timedelta
from typing import Optional, Union
# Check for Polars dependency before anything else
try:
import polars as pl
except ImportError:
print("\n" + "=" * 70)
print("ERROR: Polars is required but not installed")
print("=" * 70)
print("\nPolars offers significant advantages over Pandas:")
print(" • Faster data processing (written in Rust)")
print(" • Better memory efficiency")
print(" • Lazy evaluation support")
print(" • More intuitive API")
print("\nInstall Polars using:")
print(" conda install -c conda-forge polars")
print(" # OR")
print(" pip install polars")
print("=" * 70 + "\n")
sys.exit(1)
# Import other dependencies
try:
import requests
except ImportError:
print("\nERROR: 'requests' library is required. Install with: pip install requests")
sys.exit(1)
try:
from platformdirs import user_data_dir
except ImportError:
print("\nERROR: 'platformdirs' library is required. Install with: pip install platformdirs")
sys.exit(1)
# API Configuration
API_BASE_URL = "https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/od/auctions_query"
REQUIRED_FIELDS = [
'cusip',
'security_type',
'issue_date',
'original_issue_date',
'maturity_date',
'int_rate',
'total_accepted',
'reopening',
'inflation_index_security',
'floating_rate',
'announcemt_date',
'announcemtd_cusip',
'auction_date'
]
def _getCacheDirectory() -> Path:
"""
Get the appropriate cache directory for storing auction data.
Uses platformdirs to determine the correct location based on OS:
- Linux: ~/.local/share/ustCusipPanel/
- macOS: ~/Library/Application Support/ustCusipPanel/
- Windows: C:\\Users\\<username>\\AppData\\Local\\ustCusipPanel\\
Returns
-------
Path
Path object pointing to the cache directory
"""
cache_dir = Path(user_data_dir("ustCusipPanel", "ustCusipPanel"))
cache_dir.mkdir(parents=True, exist_ok=True)
return cache_dir
def _fetchAuctionData(startDate: str, endDate: str) -> pl.DataFrame:
"""
Fetch Treasury auction data from the Fiscal Data API with pagination.
Parameters
----------
startDate : str
Starting date for data retrieval in YYYY-MM-DD format
endDate : str
Ending date for data retrieval in YYYY-MM-DD format
Returns
-------
pl.DataFrame
Polars DataFrame containing raw auction data
"""
allData = []
page = 1
pageSize = 10000 # API maximum page size
print(f"\nFetching auction data from {startDate} to {endDate}...")
while True:
# Build filter string with date range
filterStr = f'auction_date:gte:{startDate},auction_date:lte:{endDate}'
params = {
'fields': ','.join(REQUIRED_FIELDS),
'filter': filterStr,
'format': 'json',
'page[number]': page,
'page[size]': pageSize
}
response = requests.get(API_BASE_URL, params=params)
if response.status_code != 200:
raise Exception(f"API request failed on page {page}: HTTP {response.status_code}")
data = response.json()
if 'data' not in data or len(data['data']) == 0:
break
allData.extend(data['data'])
print(f" Retrieved page {page}: {len(data['data'])} records")
# Check if we've gotten all data
if len(data['data']) < pageSize:
break
page += 1
# Create Polars DataFrame
df = pl.DataFrame(allData)
print(f" Total records retrieved: {len(df)}")
return df
def _classifyTenor(df: pl.DataFrame) -> pl.DataFrame:
"""
Classify securities by tenor based on original term to maturity.
This function assigns tenor values to Treasury securities based on their
original term to maturity:
- Bills: Measured in weeks (e.g., 1, 2, 4, 8, 13, 17, 22, 26, 52)
- Notes/Bonds: Measured in years (e.g., 2, 3, 4, 5, 7, 10, 20, 30)
Special handling for unscheduled reopenings: When a security was announced
with one CUSIP but reopened under a different CUSIP at auction, the tenor
is calculated from the issue date rather than the original issue date.
Parameters
----------
df : pl.DataFrame
DataFrame with issue_date, maturity_date, and reopening information
Returns
-------
pl.DataFrame
DataFrame with added 'tenor' column (Int64) and 'unscheduledReopeningDate'
"""
# Sort by CUSIP and issue_date to identify first issuance
df = df.sort(["cusip", "issue_date"])
# Get the earliest issue_date and maturity_date for each CUSIP
earliestDates = df.group_by("cusip").agg([
pl.col("issue_date").first().alias("earliestIssueDate"),
pl.col("maturity_date").first().alias("earliestMaturityDate")
])
# Join back and calculate term to maturity in days
df = df.join(earliestDates, on="cusip", how="left")
df = df.with_columns(
(pl.col("earliestMaturityDate") - pl.col("earliestIssueDate"))
.dt.total_days()
.alias("termToMaturityDays")
)
# Special case: For reopenings with announced CUSIP, recalculate term to maturity
# using issue_date instead of earliest_issue_date (unscheduled reopenings)
df = df.with_columns([
pl.when(
(pl.col("announcemtd_cusip") != "null") &
(pl.col("reopening") == "Yes")
)
.then(
(pl.col("earliestMaturityDate") - pl.col("issue_date"))
.dt.total_days()
)
.otherwise(pl.col("termToMaturityDays"))
.alias("termToMaturityDays"),
# Mark unscheduled reopenings with the issue date
pl.when(
(pl.col("announcemtd_cusip") != "null") &
(pl.col("reopening") == "Yes")
)
.then(pl.col("issue_date"))
.otherwise(None)
.alias("unscheduledReopeningDate")
])
# Assign tenor classification
df = df.with_columns(
pl.when((pl.col("termToMaturityDays") >= 6) &
(pl.col("termToMaturityDays") <= 8))
.then(pl.lit(1)) # 1-week bills
.when((pl.col("termToMaturityDays") >= 13) &
(pl.col("termToMaturityDays") <= 15))
.then(pl.lit(2)) # 2-week bills
.when((pl.col("termToMaturityDays") >= 26) &
(pl.col("termToMaturityDays") <= 30))
.then(pl.lit(4)) # 4-week bills
.when((pl.col("termToMaturityDays") >= 53) &
(pl.col("termToMaturityDays") <= 59))
.then(pl.lit(8)) # 8-week bills
.when((pl.col("termToMaturityDays") >= 86) &
(pl.col("termToMaturityDays") <= 96))
.then(pl.lit(13)) # 13-week bills
.when((pl.col("termToMaturityDays") >= 114) &
(pl.col("termToMaturityDays") <= 124))
.then(pl.lit(17)) # 17-week bills
.when((pl.col("termToMaturityDays") >= 149) &
(pl.col("termToMaturityDays") <= 159))
.then(pl.lit(22)) # 22-week bills
.when((pl.col("termToMaturityDays") >= 176) &
(pl.col("termToMaturityDays") <= 188))
.then(pl.lit(26)) # 26-week bills
.when((pl.col("termToMaturityDays") >= 357) &
(pl.col("termToMaturityDays") <= 371))
.then(pl.lit(52)) # 52-week bills
.when((pl.col("termToMaturityDays") >= (2*365.25 - 93)) &
(pl.col("termToMaturityDays") <= (2*365.25 + 93)))
.then(pl.lit(2)) # 2-year notes
.when((pl.col("termToMaturityDays") >= (3*365.25 - 93)) &
(pl.col("termToMaturityDays") <= (3*365.25 + 93)))
.then(pl.lit(3)) # 3-year notes
.when((pl.col("termToMaturityDays") >= (4*365.25 - 93)) &
(pl.col("termToMaturityDays") <= (4*365.25 + 93)))
.then(pl.lit(4)) # 4-year notes
.when((pl.col("termToMaturityDays") >= (5*365.25 - 180)) &
(pl.col("termToMaturityDays") <= (5*365.25 + 180)))
.then(pl.lit(5)) # 5-year notes
.when((pl.col("termToMaturityDays") >= (7*365.25 - 180)) &
(pl.col("termToMaturityDays") <= (7*365.25 + 180)))
.then(pl.lit(7)) # 7-year notes
.when((pl.col("termToMaturityDays") >= (10*365.25 - 240)) &
(pl.col("termToMaturityDays") <= (10*365.25 + 240)))
.then(pl.lit(10)) # 10-year notes
.when((pl.col("termToMaturityDays") >= (20*365.25 - 540)) &
(pl.col("termToMaturityDays") <= (20*365.25 + 540)))
.then(pl.lit(20)) # 20-year bonds
.when((pl.col("termToMaturityDays") >= (30*365.25 - 720)) &
(pl.col("termToMaturityDays") <= (30*365.25 + 720)))
.then(pl.lit(30)) # 30-year bonds
.otherwise(None)
.cast(pl.Int64)
.alias("tenor")
)
# Drop intermediate calculation columns
df = df.drop(["termToMaturityDays", "earliestIssueDate", "earliestMaturityDate"])
return df
def _processRawAuctionData(rawDf: pl.DataFrame) -> pl.DataFrame:
"""
Process raw auction data from API: type conversions, tenor classification, etc.
Parameters
----------
rawDf : pl.DataFrame
Raw auction data from _fetchAuctionData()
Returns
-------
pl.DataFrame
Processed auction data ready for caching or panel creation
"""
# Replace "null" strings with None for all relevant columns (before type conversions)
auctionsDf = rawDf.with_columns([
pl.when(pl.col("int_rate") == "null")
.then(None)
.otherwise(pl.col("int_rate"))
.alias("int_rate"),
pl.when(pl.col("announcemtd_cusip") == "null")
.then(None)
.otherwise(pl.col("announcemtd_cusip"))
.alias("announcemtd_cusip"),
pl.when(pl.col("original_issue_date") == "null")
.then(None)
.otherwise(pl.col("original_issue_date"))
.alias("original_issue_date"),
pl.when(pl.col("total_accepted") == "null")
.then(None)
.otherwise(pl.col("total_accepted"))
.alias("total_accepted"),
pl.when(pl.col("issue_date") == "null")
.then(None)
.otherwise(pl.col("issue_date"))
.alias("issue_date"),
pl.when(pl.col("maturity_date") == "null")
.then(None)
.otherwise(pl.col("maturity_date"))
.alias("maturity_date"),
pl.when(pl.col("auction_date") == "null")
.then(None)
.otherwise(pl.col("auction_date"))
.alias("auction_date"),
pl.when(pl.col("announcemt_date") == "null")
.then(None)
.otherwise(pl.col("announcemt_date"))
.alias("announcemt_date")
])
# Convert all date columns to Date type (from API string format)
auctionsDf = auctionsDf.with_columns([
pl.col("issue_date").str.to_date("%Y-%m-%d"),
pl.col("original_issue_date").str.to_date("%Y-%m-%d"),
pl.col("maturity_date").str.to_date("%Y-%m-%d"),
pl.col("auction_date").str.to_date("%Y-%m-%d"),
pl.col("announcemt_date").str.to_date("%Y-%m-%d")
])
# Classify tenor (creates unscheduledReopeningDate as Date type)
auctionsDf = _classifyTenor(auctionsDf)
# Set coupon to zero for Bills (zero-coupon securities)
auctionsDf = auctionsDf.with_columns(
pl.when(pl.col("security_type") == "Bill")
.then(pl.lit("0"))
.otherwise(pl.col("int_rate"))
.alias("int_rate")
)
# Convert inflation_index_security and floating_rate to Boolean
auctionsDf = auctionsDf.with_columns([
(pl.col("inflation_index_security") == "Yes").alias("inflation_index_security"),
(pl.col("floating_rate") == "Yes").alias("floating_rate")
])
# Convert numeric columns to proper types
auctionsDf = auctionsDf.with_columns([
pl.col("int_rate").cast(pl.Float64, strict=False),
pl.col("total_accepted").cast(pl.Float64, strict=False),
pl.col("tenor").cast(pl.Int32, strict=False)
])
# Transform reopening column to auction with natural labeling
auctionsDf = auctionsDf.with_columns(
pl.when(pl.col("reopening") == "No")
.then(pl.lit("Opening"))
.when(pl.col("reopening") == "Yes")
.then(pl.lit("Re-opening"))
.otherwise(pl.lit(None))
.alias("issuanceType")
).drop("reopening")
# Replace any remaining "null" strings with None before saving
for col in auctionsDf.columns:
if auctionsDf.schema[col] == pl.Utf8:
auctionsDf = auctionsDf.with_columns(
pl.when(pl.col(col) == "null")
.then(None)
.otherwise(pl.col(col))
.alias(col)
)
return auctionsDf
def _loadOrDownloadData(startDate: str, endDate: str, forceDownload: bool) -> pl.DataFrame:
"""
Load cached auction data or download fresh data from the API.
This function manages data caching to avoid unnecessary API calls.
It checks if cached data exists and matches the requested date range.
Parameters
----------
startDate : str
Starting date in YYYY-MM-DD format
endDate : str
Ending date in YYYY-MM-DD format
forceDownload : bool
If True, ignore cache and download fresh data
Returns
-------
pl.DataFrame
Processed auction data with tenor classifications
"""
cacheDir = _getCacheDirectory()
csvFile = cacheDir / "auctions.csv"
txtFile = cacheDir / "auctions.txt"
# Handle forceDownload flag - bypass cache entirely
if forceDownload:
print("\nforceDownload=True, ignoring cache...")
rawDf = _fetchAuctionData(startDate, endDate)
auctionsDf = _processRawAuctionData(rawDf)
# Save to cache
auctionsDf.write_csv(csvFile)
print(f"\nData cached to: {csvFile}")
# Save date range
with open(txtFile, 'w') as f:
f.write(f"{startDate},{endDate}")
print(f"Date range saved to: {txtFile}")
return auctionsDf
# Define schema for reading cached CSV
cacheSchema = {
"issue_date": pl.Date,
"original_issue_date": pl.Date,
"maturity_date": pl.Date,
"announcemt_date": pl.Date,
"auction_date": pl.Date,
"unscheduledReopeningDate": pl.Date,
"int_rate": pl.Float64,
"total_accepted": pl.Float64,
"tenor": pl.Int32,
"inflation_index_security": pl.Boolean,
"floating_rate": pl.Boolean
}
# Smart caching: Analyze date range overlap
if csvFile.exists() and txtFile.exists():
print(f"\nFound cached data in: {cacheDir}")
# Parse cached date range
with open(txtFile, 'r') as f:
cachedRange = f.read().strip()
cachedStartStr, cachedEndStr = cachedRange.split(',')
cachedStart = date.fromisoformat(cachedStartStr)
cachedEnd = date.fromisoformat(cachedEndStr)
# Convert requested dates to date objects
requestedStart = date.fromisoformat(startDate)
requestedEnd = date.fromisoformat(endDate)
# Analyze overlap between requested and cached ranges
overlap = _analyzeDateRangeOverlap(
requestedStart, requestedEnd,
cachedStart, cachedEnd
)
# SCENARIO 1: Exact match - use cache as-is
if overlap['scenario'] == 'exact':
print(f"Cached data matches requested range ({startDate} to {endDate})")
print("Loading data from cache...")
return pl.read_csv(
csvFile,
null_values=["null"],
schema_overrides=cacheSchema
)
# SCENARIO 2: Subset - filter cached data
elif overlap['scenario'] == 'subset':
print(f"Requested range ({startDate} to {endDate}) is within cache ({cachedStartStr} to {cachedEndStr})")
print("Filtering cached data...")
cachedData = pl.read_csv(
csvFile,
null_values=["null"],
schema_overrides=cacheSchema
)
return cachedData.filter(
(pl.col("auction_date") >= pl.lit(startDate).str.to_date("%Y-%m-%d")) &
(pl.col("auction_date") <= pl.lit(endDate).str.to_date("%Y-%m-%d"))
)
# SCENARIO 3-5: Extension/overlap - fetch missing ranges and merge
elif overlap['use_cache']:
print(f"Requested range ({startDate} to {endDate}) extends beyond cache ({cachedStartStr} to {cachedEndStr})")
print(f"Will fetch {len(overlap['fetch_ranges'])} missing range(s) and merge with cache")
# Load cached data
cachedData = pl.read_csv(
csvFile,
null_values=["null"],
schema_overrides=cacheSchema
)
# Fetch missing ranges
allNewData = []
for fetchStart, fetchEnd in overlap['fetch_ranges']:
print(f"\nFetching missing range: {fetchStart} to {fetchEnd}")
rawDf = _fetchAuctionData(fetchStart, fetchEnd)
if rawDf.height > 0:
processedDf = _processRawAuctionData(rawDf)
allNewData.append(processedDf)
# Merge cached data with new data
if allNewData:
newData = pl.concat(allNewData) if len(allNewData) > 1 else allNewData[0]
auctionsDf = pl.concat([cachedData, newData])
# Deduplicate in case of boundary overlaps
auctionsDf = _deduplicateAuctions(auctionsDf)
else:
auctionsDf = cachedData
# Update cache with expanded range
newCacheStart = min(requestedStart, cachedStart)
newCacheEnd = max(requestedEnd, cachedEnd)
# SCENARIO 6: No overlap - full download
else:
print(f"No overlap with cache ({cachedStartStr} to {cachedEndStr})")
print(f"Downloading full range ({startDate} to {endDate})")
rawDf = _fetchAuctionData(startDate, endDate)
auctionsDf = _processRawAuctionData(rawDf)
# New cache range is just the requested range
newCacheStart = requestedStart
newCacheEnd = requestedEnd
else:
# No cache exists - full download
print("\nNo cache found")
print(f"Downloading full range ({startDate} to {endDate})")
rawDf = _fetchAuctionData(startDate, endDate)
auctionsDf = _processRawAuctionData(rawDf)
# New cache range is the requested range
newCacheStart = date.fromisoformat(startDate)
newCacheEnd = date.fromisoformat(endDate)
# Save to cache
auctionsDf.write_csv(csvFile)
print(f"\nCache updated: {csvFile}")
# Save expanded date range
with open(txtFile, 'w') as f:
f.write(f"{newCacheStart.strftime('%Y-%m-%d')},{newCacheEnd.strftime('%Y-%m-%d')}")
print(f"Cache range: {newCacheStart.strftime('%Y-%m-%d')} to {newCacheEnd.strftime('%Y-%m-%d')}")
return auctionsDf
def _analyzeDateRangeOverlap(
requestedStart: date,
requestedEnd: date,
cachedStart: date,
cachedEnd: date
) -> dict:
"""
Analyze the relationship between requested and cached date ranges.
Determines which caching strategy to use based on how the requested
date range overlaps with the cached date range.
Parameters
----------
requestedStart : date
Start date of requested range
requestedEnd : date
End date of requested range
cachedStart : date
Start date of cached range
cachedEnd : date
End date of cached range
Returns
-------
dict
Dictionary with keys:
- scenario: str ('exact', 'subset', 'left_extend', 'right_extend', 'superset', 'no_overlap')
- use_cache: bool (whether cached data can be used)
- fetch_ranges: List[Tuple[str, str]] (date ranges to fetch from API)
- filter_cache: bool (whether to filter cached data to requested range)
"""
# Scenario 1: Exact match
if requestedStart == cachedStart and requestedEnd == cachedEnd:
return {
'scenario': 'exact',
'use_cache': True,
'fetch_ranges': [],
'filter_cache': False
}
# Scenario 2: Subset (requested fully within cache)
if cachedStart <= requestedStart and requestedEnd <= cachedEnd:
return {
'scenario': 'subset',
'use_cache': True,
'fetch_ranges': [],
'filter_cache': True
}
# Scenario 3: Superset (cache fully within requested)
if requestedStart < cachedStart and requestedEnd > cachedEnd:
fetch_ranges = []
# Fetch earlier data
fetch_ranges.append((
requestedStart.strftime('%Y-%m-%d'),
(cachedStart - timedelta(days=1)).strftime('%Y-%m-%d')
))
# Fetch later data
fetch_ranges.append((
(cachedEnd + timedelta(days=1)).strftime('%Y-%m-%d'),
requestedEnd.strftime('%Y-%m-%d')
))
return {
'scenario': 'superset',
'use_cache': True,
'fetch_ranges': fetch_ranges,
'filter_cache': False
}
# Scenario 4: Left extension (extends before cache)
if requestedStart < cachedStart and requestedEnd >= cachedStart and requestedEnd <= cachedEnd:
fetch_ranges = [(
requestedStart.strftime('%Y-%m-%d'),
(cachedStart - timedelta(days=1)).strftime('%Y-%m-%d')
)]
return {
'scenario': 'left_extend',
'use_cache': True,
'fetch_ranges': fetch_ranges,
'filter_cache': False
}
# Scenario 5: Right extension (extends after cache)
if requestedStart >= cachedStart and requestedStart <= cachedEnd and requestedEnd > cachedEnd:
fetch_ranges = [(
(cachedEnd + timedelta(days=1)).strftime('%Y-%m-%d'),
requestedEnd.strftime('%Y-%m-%d')
)]
return {
'scenario': 'right_extend',
'use_cache': True,
'fetch_ranges': fetch_ranges,
'filter_cache': False
}
# Scenario 6: No overlap (completely disjoint)
return {
'scenario': 'no_overlap',
'use_cache': False,
'fetch_ranges': [(
requestedStart.strftime('%Y-%m-%d'),
requestedEnd.strftime('%Y-%m-%d')
)],
'filter_cache': False
}
def _deduplicateAuctions(df: pl.DataFrame) -> pl.DataFrame:
"""
Remove duplicate auctions based on auction_date and cusip.
This handles boundary overlaps when merging new data with cache.
Keeps the first occurrence after sorting by auction_date, cusip, and issue_date.
Parameters
----------
df : pl.DataFrame
DataFrame potentially containing duplicate auctions
Returns
-------
pl.DataFrame
DataFrame with duplicates removed
"""
return df.sort(['auction_date', 'cusip', 'issue_date']).unique(
subset=['auction_date', 'cusip'],
keep='first'
)
def _createCusipPanel(auctionsDf: pl.DataFrame) -> pl.DataFrame:
"""
Create a complete CUSIP-date panel from auction data.
This function:
1. Computes firstIssueDate for each CUSIP
2. Creates complete date ranges (time series completion)
3. Forward/backward fills CUSIP characteristics
4. Calculates cumulative issuance
5. Computes vintage rankings
6. Filters out weekends
Parameters
----------
auctionsDf : pl.DataFrame
Raw auction data with tenor classifications
Returns
-------
pl.DataFrame
Complete CUSIP-date panel with all features
"""
# 1. Get the earliest issue_date for each CUSIP (for firstIssueDate column)
auctionsDf = auctionsDf.with_columns(
pl.col("issue_date").min().over("cusip").alias("firstIssueDate")
)
# 2. Time series completion (fill in missing dates for each CUSIP)
# Get the date range boundaries for each CUSIP
cusipDateRanges = auctionsDf.group_by("cusip").agg([
pl.col("announcemt_date").min().alias("start_date"),
pl.col("maturity_date").first().alias("end_date")
])
# Create complete date range for each CUSIP
allCusipDates = []
today = date.today()
for row in cusipDateRanges.iter_rows(named=True):
# Create date range from first announcement to maturity (bounded by today)
endDate = min(row["end_date"], today)
dateRange = pl.date_range(
pl.lit(row["start_date"]),
pl.lit(endDate),
interval="1d",
eager=True
)
# Create dataframe for this CUSIP with all dates
cusipDates = pl.DataFrame({
"cusip": [row["cusip"]] * len(dateRange),
"date": dateRange
})
allCusipDates.append(cusipDates)
# Concatenate all CUSIP date ranges
completeDates = pl.concat(allCusipDates)
# Prepare auction data for joining
auctionData = auctionsDf.select([
pl.col("cusip"),
pl.col("issue_date").alias("date"),
pl.col("total_accepted").alias("amountIssued"),
pl.col("issuanceType"),
pl.col("tenor"),
pl.col("int_rate").alias("coupon"),
pl.col("maturity_date").alias("maturityDate"),
pl.col("announcemt_date").alias("announcementDate"),
pl.col("auction_date").alias("auctionDate"),
pl.col("unscheduledReopeningDate"),
pl.col("firstIssueDate"),
pl.col("inflation_index_security").alias("TIPS"),
pl.col("floating_rate").alias("floatingRate"),
pl.col("security_type").alias("securityType")
])
# Handle bonds to be issued (future-dated)
# For any CUSIP-date with date > today, change date to today if no today observation exists
futureDated = auctionData.filter(pl.col("date") > today)
if futureDated.height > 0:
cusipsWithToday = auctionData.filter(pl.col("date") == today).select("cusip")
# Split future-dated records
futureWithoutToday = futureDated.join(cusipsWithToday, on="cusip", how="anti")
# Remove future-dated records
auctionData = auctionData.filter(pl.col("date") <= today)
# For CUSIPs without a today observation, change their date to today
if futureWithoutToday.height > 0:
futureWithoutToday = futureWithoutToday.with_columns([
pl.lit(today).alias("date"),
pl.lit(None).alias("issuanceType"),
pl.lit(None).alias("unscheduledReopeningDate"),
pl.lit(0.0).cast(pl.Float64).alias("amountIssued")
])
auctionData = pl.concat([auctionData, futureWithoutToday])
# Join complete date range with auction data
auctionsDf = completeDates.join(
auctionData,
on=["cusip", "date"],
how="left"
)
# Sort by CUSIP and date to prepare for forward fill
auctionsDf = auctionsDf.sort(["cusip", "date"])
# Forward-fill static values within each CUSIP group
auctionsDf = auctionsDf.with_columns([
pl.col("tenor").forward_fill().over("cusip"),
pl.col("coupon").forward_fill().over("cusip"),
pl.col("maturityDate").forward_fill().over("cusip"),
pl.col("announcementDate").forward_fill().over("cusip"),
pl.col("auctionDate").forward_fill().over("cusip"),
pl.col("unscheduledReopeningDate").forward_fill().over("cusip"),
pl.col("firstIssueDate").forward_fill().over("cusip"),
pl.col("TIPS").forward_fill().over("cusip"),
pl.col("floatingRate").forward_fill().over("cusip"),
pl.col("securityType").forward_fill().over("cusip")
])
# Backward-fill static values
auctionsDf = auctionsDf.with_columns([
pl.col("tenor").backward_fill().over("cusip"),
pl.col("coupon").backward_fill().over("cusip"),
pl.col("maturityDate").backward_fill().over("cusip"),
pl.col("announcementDate").backward_fill().over("cusip"),
pl.col("auctionDate").backward_fill().over("cusip"),
pl.col("firstIssueDate").backward_fill().over("cusip"),
pl.col("TIPS").backward_fill().over("cusip"),
pl.col("floatingRate").backward_fill().over("cusip"),
pl.col("securityType").backward_fill().over("cusip")
])
# Set amountIssued to 0 for non-issue dates and dates before first issue
auctionsDf = auctionsDf.with_columns(
pl.when(pl.col("date") < pl.col("firstIssueDate"))
.then(pl.lit(0))
.otherwise(pl.col("amountIssued").fill_null(0))
.alias("amountIssued")
)
# 3. Calculate cumulative issuance
auctionsDf = auctionsDf.sort(
["cusip", "date", "tenor", "firstIssueDate"],
descending=[True, False, False, True]
)
auctionsDf = auctionsDf.with_columns(
pl.col("amountIssued").cum_sum().over("cusip").alias("totalIssued")
)
# 4. Calculate vintage (ordinal ranking by firstIssueDate within date-security_type-inflation_index_security-floating_rate-tenor)
# Latest firstIssueDate gets vintage 0, next-latest gets 1, etc.
auctionsDf = auctionsDf.with_columns(
(pl.col("firstIssueDate")
.rank(method="dense", descending=True)
.over(["date", "securityType", "TIPS", "floatingRate", "tenor"]) - 1)
.cast(pl.Int64)
.alias("vintage")
)
# Adjust vintage for "when issued" bonds
auctionsDf = auctionsDf.with_columns(
(pl.col("date") < pl.col("firstIssueDate"))
.any()
.over(["date", "securityType", "TIPS", "floatingRate", "tenor"])
.alias("hasWhenIssued")
).with_columns(
pl.when(pl.col("hasWhenIssued"))
.then(pl.col("vintage") - 1)
.otherwise(pl.col("vintage"))
.alias("vintage")
).drop("hasWhenIssued")
# 5. Filter out weekends (Saturday=6, Sunday=7 in weekday())
auctionsDf = auctionsDf.filter(
pl.col("date").dt.weekday() < 6
)
# Sort final output
auctionsDf = auctionsDf.sort(
["floatingRate", "TIPS", "date", "securityType", "tenor", "vintage"],
descending=[False, False, True, True, False, False]
)
# Reorder columns for consistent output
auctionsDf = auctionsDf.select([
'date', 'cusip', 'securityType', 'tenor', 'vintage',
'coupon', 'maturityDate', 'TIPS',
'floatingRate', 'firstIssueDate',
'issuanceType', 'auctionDate', 'unscheduledReopeningDate',
'amountIssued', 'totalIssued',
'announcementDate'
])
return auctionsDf
def _printSummary(df: pl.DataFrame) -> None:
"""
Print summary statistics about the CUSIP panel.
Parameters
----------
df : pl.DataFrame
Complete CUSIP panel data
"""
print(f"\n{'=' * 70}")
print("Data Statistics Summary")
print(f"{'=' * 70}")
print(f"Total observations: {len(df):,}")
print(f"Unique CUSIPs: {df.select(pl.col('cusip').n_unique()).item():,}")
print(f"Date range: {df.select(pl.col('date').min()).item()} to {df.select(pl.col('date').max()).item()}")
# Bill statistics (by week tenors)
print(f"\n{'=' * 70}")
print("Bill Statistics (by tenor in weeks)")
print(f"{'=' * 70}")
billTenors = sorted(
df.filter(pl.col('securityType') == 'Bill')
.select(pl.col('tenor').unique())
.drop_nulls()
.to_series()
.to_list()
)
for tenor in billTenors:
billData = df.filter(
(pl.col('securityType') == 'Bill') &
(pl.col('tenor') == tenor)
)
if billData.height > 0:
uniqueCusips = billData.select(pl.col('cusip').n_unique()).item()
avgVintages = billData.group_by('date').agg(
pl.col('vintage').n_unique().alias('nVintages')
).select(pl.col('nVintages').mean()).item()
print(f"{tenor}-week: {uniqueCusips:,} unique CUSIPs, {int(round(avgVintages))} avg daily vintages")
# Note/Bond statistics (by year tenors)
print(f"\n{'=' * 70}")
print("Note/Bond Statistics (by tenor in years)")
print(f"{'=' * 70}")
noteBondTenors = sorted(
df.filter(pl.col('securityType') != 'Bill')
.select(pl.col('tenor').unique())
.drop_nulls()
.to_series()
.to_list()
)
for tenor in noteBondTenors:
noteBondData = df.filter(
(pl.col('securityType') != 'Bill') &
(pl.col('tenor') == tenor)
)
if noteBondData.height > 0:
uniqueCusips = noteBondData.select(pl.col('cusip').n_unique()).item()
avgVintages = noteBondData.group_by('date').agg(
pl.col('vintage').n_unique().alias('nVintages')
).select(pl.col('nVintages').mean()).item()
print(f"{tenor}-year: {uniqueCusips:,} unique CUSIPs, {int(round(avgVintages))} avg daily vintages")
print(f"{'=' * 70}\n")
def ustCusipPanel(
startDate: str = "1990-01-01",
endDate: Optional[str] = None,
silent: bool = False,
forceDownload: bool = False
) -> pl.DataFrame:
"""
Download and process Treasury auction data into a CUSIP-date panel.
This is the main public function that fetches Treasury auction data from the
U.S. Treasury's Fiscal Data API and transforms it into a complete panel dataset
with business date completion, tenor classifications, vintage rankings, and
cumulative issuance tracking.
The data is automatically cached locally to avoid repeated API calls. By default,
the function uses cached data if it matches the requested date range.
Parameters
----------
startDate : str, default="1990-01-01"
Starting date for data retrieval in YYYY-MM-DD format.
Default is "1990-01-01" which captures the modern Treasury auction system.
endDate : str, optional
Ending date for data retrieval in YYYY-MM-DD format.
If None (default), uses today's date.
silent : bool, default=False
If True, suppresses summary statistics output.
If False (default), prints detailed statistics about the panel.
forceDownload : bool, default=False
If True, ignores cached data and downloads fresh data from the API.
If False (default), uses cached data when the date range matches.
Returns
-------