-
Notifications
You must be signed in to change notification settings - Fork 0
/
us_debt.py
399 lines (335 loc) · 18.8 KB
/
us_debt.py
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
import os
import requests
import pandas as pd
import datetime
import numpy as np
from utils import *
from io import StringIO, BytesIO
def to_weekend(x):
dt = pd.to_datetime(x, format='%Y-%m-%d')
dt = dt + pd.Timedelta(days=(6-dt.weekday()))
t = dt.strftime('%Y-%m-%d')
return t
def to_monthend(x):
dt = pd.to_datetime(x, format='%Y-%m-%d')
last_day = calendar.monthrange(dt.year, dt.month)[-1]
dt = dt + pd.Timedelta(days=(last_day-dt.day))
t = dt.strftime('%Y-%m-%d')
return t
def update_treasury_auction_data():
se = requests.session()
US_TREASURY_HEADERS = {"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)",
'Host': 'api.fiscaldata.treasury.gov'}
URL = 'https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/od/auctions_query?filter=record_date:gt:{}&format=csv&page[size]={}'
earlist_time = '2000-01-01'
page_size = 100
while (1):
path = os.path.join(treasury_auction_dir, 'auction.csv')
if os.path.exists(path):
old_df = pd.read_csv(path)
t = pd.DatetimeIndex(pd.to_datetime(old_df['record_date'], format='%Y-%m-%d'))
start_time_dt = t[-1] - pd.Timedelta(days=20)
start_time = start_time_dt.strftime('%Y-%m-%d')
else:
start_time = earlist_time
url = URL.format(start_time, str(page_size))
print('TREASURY AUCTION FROM', start_time)
r = se.get(url, headers=US_TREASURY_HEADERS)
auction_df = pd.read_csv(StringIO(r.text))
if os.path.exists(path):
old_df = pd.read_csv(path)
old_df = pd.concat([old_df, auction_df], axis=0)
old_df.drop_duplicates(subset=['auction_date', 'security_type', 'security_term'], keep='last', inplace=True)
old_df.to_csv(path, encoding='utf-8', index=False)
else:
auction_df.to_csv(path, encoding='utf-8', index=False)
security_type = np.array(auction_df['security_type'], dtype=str)
# print(auction_df.columns.tolist())
tips = np.array(auction_df['inflation_index_security'], dtype=str)
floating_rate = np.array(auction_df['floating_rate'], dtype=str)
df_dict = {}
# Bill
idx = np.where(security_type == 'Bill')[0]
for i in idx:
temp_df = pd.DataFrame(auction_df.loc[i]).T
temp_df.reset_index(inplace=True, drop=True)
security_term = temp_df.loc[0, 'security_term']
if temp_df.loc[0, 'cash_management_bill_cmb'] == 'No':
name = 'Bill' + '_' + security_term.split('-')[0] + 'W'
elif temp_df.loc[0, 'cash_management_bill_cmb'] == 'Yes':
name = 'CMB'
else:
continue
weekend = pd.to_datetime(temp_df['auction_date'], format='%Y-%m-%d')
for i in range(len(weekend)):
weekend[i] = weekend[i] + pd.Timedelta(days=(6-weekend[i].weekday()))
temp_df['period_end_date'] = temp_df['auction_date']
temp_df['period_end_date'] = temp_df['period_end_date'].apply(to_weekend)
temp_df = temp_df[['auction_date', 'period_end_date',
'security_type', 'security_term', 'security_term_day_month', 'security_term_week_year',
'reopening', 'original_issue_date', 'original_security_term',
'announcemt_date', 'record_date', 'issue_date', 'maturity_date',
'price_per100',
'allocation_pctage', 'avg_med_discnt_rate',
'high_discnt_rate', 'high_investment_rate', 'high_price', 'low_discnt_rate',
'bid_to_cover_ratio',
'currently_outstanding', 'est_pub_held_mat_by_type_amt',
'total_accepted', 'total_tendered',
'offering_amt', 'comp_accepted', 'comp_tendered', 'noncomp_accepted',
'direct_bidder_accepted', 'direct_bidder_tendered', 'indirect_bidder_accepted', 'indirect_bidder_tendered',
'primary_dealer_accepted', 'primary_dealer_tendered', 'soma_accepted', 'soma_tendered', 'soma_holdings',
'treas_retail_accepted', 'fima_noncomp_accepted', 'fima_noncomp_tendered',
]]
if name in df_dict:
df_dict[name] = pd.concat([df_dict[name], temp_df], axis=0)
else:
df_dict[name] = temp_df.copy()
idx = np.where(security_type == 'Note')[0]
tips_idx = np.where(tips == 'Yes')[0]
frn_idx = np.where(floating_rate == 'Yes')[0]
note_idx = np.setdiff1d(idx, tips_idx)
note_idx = np.setdiff1d(note_idx, frn_idx)
# Note
for i in note_idx:
temp_df = pd.DataFrame(auction_df.loc[i]).T
temp_df.reset_index(inplace=True, drop=True)
try:
original_security_term = temp_df.loc[0, 'original_security_term']
name = 'Note' + '_' + original_security_term.split('-')[0] + 'Y'
except:
continue
temp_df['period_end_date'] = temp_df['auction_date']
temp_df['period_end_date'] = temp_df['period_end_date'].apply(to_monthend)
temp_df = temp_df[['auction_date', 'period_end_date',
'security_type', 'security_term', 'security_term_day_month', 'security_term_week_year',
'reopening', 'original_issue_date', 'original_security_term',
'announcemt_date', 'record_date', 'issue_date', 'maturity_date',
'strippable',
'price_per100',
'allocation_pctage', 'avg_med_yield',
'high_yield', 'high_price', 'int_rate', 'low_yield',
'bid_to_cover_ratio',
'currently_outstanding', 'est_pub_held_mat_by_type_amt',
'total_accepted', 'total_tendered',
'offering_amt', 'comp_accepted', 'comp_tendered', 'noncomp_accepted',
'direct_bidder_accepted', 'direct_bidder_tendered', 'indirect_bidder_accepted', 'indirect_bidder_tendered',
'primary_dealer_accepted', 'primary_dealer_tendered', 'soma_accepted', 'soma_tendered', 'soma_holdings',
'treas_retail_accepted', 'fima_noncomp_accepted', 'fima_noncomp_tendered',
]]
if name in df_dict:
df_dict[name] = pd.concat([df_dict[name], temp_df], axis=0)
else:
df_dict[name] = temp_df.copy()
# Tips
for i in tips_idx:
temp_df = pd.DataFrame(auction_df.loc[i]).T
temp_df.reset_index(inplace=True, drop=True)
try:
original_security_term = temp_df.loc[0, 'original_security_term']
name = 'Tips' + '_' + original_security_term.split('-')[0] + 'Y'
except:
continue
temp_df['period_end_date'] = temp_df['auction_date']
temp_df['period_end_date'] = temp_df['period_end_date'].apply(to_monthend)
temp_df = temp_df[['auction_date', 'period_end_date',
'security_type', 'security_term', 'security_term_day_month', 'security_term_week_year',
'reopening', 'original_issue_date', 'original_security_term',
'announcemt_date', 'record_date', 'issue_date', 'maturity_date',
'strippable',
'price_per100', 'adj_price', 'unadj_price',
'cpi_base_reference_period', 'index_ratio_on_issue_date',
'ref_cpi_on_dated_date', 'ref_cpi_on_issue_date', 'tiin_conversion_factor_per1000',
'allocation_pctage', 'avg_med_yield',
'high_yield', 'high_price', 'int_rate', 'low_yield',
'bid_to_cover_ratio',
'currently_outstanding', 'est_pub_held_mat_by_type_amt',
'total_accepted', 'total_tendered',
'offering_amt', 'comp_accepted', 'comp_tendered', 'noncomp_accepted',
'direct_bidder_accepted', 'direct_bidder_tendered', 'indirect_bidder_accepted', 'indirect_bidder_tendered',
'primary_dealer_accepted', 'primary_dealer_tendered', 'soma_accepted', 'soma_tendered', 'soma_holdings',
'treas_retail_accepted', 'fima_noncomp_accepted', 'fima_noncomp_tendered',
]]
if name in df_dict:
df_dict[name] = pd.concat([df_dict[name], temp_df], axis=0)
else:
df_dict[name] = temp_df.copy()
# Floating rate note
for i in frn_idx:
temp_df = pd.DataFrame(auction_df.loc[i]).T
temp_df.reset_index(inplace=True, drop=True)
try:
original_security_term = temp_df.loc[0, 'original_security_term']
name = 'FRN' + '_' + original_security_term.split('-')[0] + 'Y'
except:
continue
temp_df['period_end_date'] = temp_df['auction_date']
temp_df['period_end_date'] = temp_df['period_end_date'].apply(to_monthend)
temp_df = temp_df[['auction_date', 'period_end_date',
'security_type', 'security_term', 'security_term_day_month', 'security_term_week_year',
'reopening', 'original_issue_date', 'original_security_term',
'announcemt_date', 'record_date', 'issue_date', 'maturity_date',
'strippable',
'price_per100',
'allocation_pctage', 'avg_med_discnt_margin',
'frn_index_determination_date', 'frn_index_determination_rate',
'high_discnt_margin', 'high_price', 'low_discnt_margin', 'spread',
'bid_to_cover_ratio',
'currently_outstanding', 'est_pub_held_mat_by_type_amt',
'total_accepted', 'total_tendered',
'offering_amt', 'comp_accepted', 'comp_tendered', 'noncomp_accepted',
'direct_bidder_accepted', 'direct_bidder_tendered', 'indirect_bidder_accepted', 'indirect_bidder_tendered',
'primary_dealer_accepted', 'primary_dealer_tendered', 'soma_accepted', 'soma_tendered', 'soma_holdings',
'treas_retail_accepted', 'fima_noncomp_accepted', 'fima_noncomp_tendered',
]]
if name in df_dict:
df_dict[name] = pd.concat([df_dict[name], temp_df], axis=0)
else:
df_dict[name] = temp_df.copy()
# Bond
bond_idx = np.where(security_type == 'Bond')[0]
for i in bond_idx:
temp_df = pd.DataFrame(auction_df.loc[i]).T
temp_df.reset_index(inplace=True, drop=True)
try:
original_security_term = temp_df.loc[0, 'original_security_term']
name = 'Bond' + '_' + original_security_term.split('-')[0] + 'Y'
except:
continue
temp_df['period_end_date'] = temp_df['auction_date']
temp_df['period_end_date'] = temp_df['period_end_date'].apply(to_monthend)
temp_df = temp_df[['auction_date', 'period_end_date',
'security_type', 'security_term', 'security_term_day_month', 'security_term_week_year',
'reopening', 'original_issue_date', 'original_security_term',
'announcemt_date', 'record_date', 'issue_date', 'maturity_date',
'strippable',
'price_per100',
'allocation_pctage', 'avg_med_yield',
'high_yield', 'high_price', 'int_rate', 'low_yield',
'bid_to_cover_ratio',
'currently_outstanding', 'est_pub_held_mat_by_type_amt',
'total_accepted', 'total_tendered',
'offering_amt', 'comp_accepted', 'comp_tendered', 'noncomp_accepted',
'direct_bidder_accepted', 'direct_bidder_tendered', 'indirect_bidder_accepted', 'indirect_bidder_tendered',
'primary_dealer_accepted', 'primary_dealer_tendered', 'soma_accepted', 'soma_tendered', 'soma_holdings',
'treas_retail_accepted', 'fima_noncomp_accepted', 'fima_noncomp_tendered',
]]
if name in df_dict:
df_dict[name] = pd.concat([df_dict[name], temp_df], axis=0)
else:
df_dict[name] = temp_df.copy()
for name in df_dict:
print('AUCTION', name)
path = os.path.join(treasury_auction_dir, name+'.csv')
df = df_dict[name]
df.replace("/", '-', inplace=True)
if os.path.exists(path):
old_df = pd.read_csv(path)
old_df = pd.concat([old_df, df], axis=0)
old_df.drop_duplicates(subset=['auction_date'], keep='last', inplace=True)
old_df['auction_date'] = old_df['auction_date'].apply(lambda x:pd.to_datetime(x, format='%Y-%m-%d'))
old_df.sort_values(by = 'auction_date', inplace=True)
old_df['auction_date'] = old_df['auction_date'].apply(lambda x:datetime.datetime.strftime(x,'%Y-%m-%d'))
old_df.to_csv(path, encoding='utf-8', index=False)
else:
df['auction_date'] = df['auction_date'].apply(lambda x:pd.to_datetime(x, format='%Y-%m-%d'))
df.sort_values(by = 'auction_date', inplace=True)
df['auction_date'] = df['auction_date'].apply(lambda x:datetime.datetime.strftime(x,'%Y-%m-%d'))
df.to_csv(path, encoding='utf-8', index=False)
if len(auction_df) < page_size:
break
def plot_treasury_auction_data():
bill = ['Bill_4W', 'Bill_8W', 'Bill_13W', 'Bill_17W', 'Bill_26W', 'Bill_52W']
cmb = ['CMB']
frn = ['FRN_2Y']
note = ['Note_2Y', 'Note_3Y', 'Note_5Y', 'Note_7Y', 'Note_10Y']
bond = ['Bond_20Y', 'Bond_30Y']
tips = ['Tips_5Y', 'Tips_10Y', 'Tips_20Y', 'Tips_30Y']
df_dict = {}
for security in bill+cmb+frn+note+bond+tips:
path = os.path.join(treasury_auction_dir, security+'.csv')
df = pd.read_csv(path)
t = pd.DatetimeIndex(pd.to_datetime(df['auction_date'], format='%Y-%m-%d'))
end_t = pd.DatetimeIndex(pd.to_datetime(df['period_end_date'], format='%Y-%m-%d'))
df_dict[security] = [t, end_t, df]
### offering amount ###
# bill+cmb
datas = [
[[],[],''],
]
for security in bill+cmb:
datas[0][0].append([df_dict[security][0], np.array(df_dict[security][2]['offering_amt'], dtype=float)/10000000000, security, 'style=dot_line'])
plot_many_figure(datas)
# frn+note
datas = [
[[],[],''],
]
for security in frn+note:
datas[0][0].append([df_dict[security][0], np.array(df_dict[security][2]['offering_amt'], dtype=float)/10000000000, security, 'style=dot_line'])
plot_many_figure(datas)
# bond
datas = [
[[],[],''],
]
for security in bond:
datas[0][0].append([df_dict[security][0], np.array(df_dict[security][2]['offering_amt'], dtype=float)/10000000000, security, 'style=dot_line'])
plot_many_figure(datas)
# tips
datas = [
[[],[],''],
]
for security in tips:
datas[0][0].append([df_dict[security][0], np.array(df_dict[security][2]['offering_amt'], dtype=float)/10000000000, security, 'style=dot_line'])
plot_many_figure(datas)
### bid to cover ratio
# bill
datas = []
for security in bill+cmb:
datas.append(
[[[df_dict[security][0], np.array(df_dict[security][2]['offering_amt'], dtype=float)/10000000000, security, 'style=dot_line']],
[[df_dict[security][0], np.array(df_dict[security][2]['bid_to_cover_ratio'], dtype=float), 'bid to cover ratio', 'style=dot_line']],''],
)
plot_many_figure(datas, max_height=1500)
# frn+note
datas = []
for security in frn+note:
datas.append(
[[[df_dict[security][0], np.array(df_dict[security][2]['offering_amt'], dtype=float)/10000000000, security, 'style=dot_line']],
[[df_dict[security][0], np.array(df_dict[security][2]['bid_to_cover_ratio'], dtype=float), 'bid to cover ratio', 'style=dot_line']],''],
)
plot_many_figure(datas, max_height=1500)
# bond
datas = []
for security in bond:
datas.append(
[[[df_dict[security][0], np.array(df_dict[security][2]['offering_amt'], dtype=float)/10000000000, security, 'style=dot_line']],
[[df_dict[security][0], np.array(df_dict[security][2]['bid_to_cover_ratio'], dtype=float), 'bid to cover ratio', 'style=dot_line']],''],
)
plot_many_figure(datas, max_height=700)
# tips
datas = []
for security in tips:
datas.append(
[[[df_dict[security][0], np.array(df_dict[security][2]['offering_amt'], dtype=float)/10000000000, security, 'style=dot_line']],
[[df_dict[security][0], np.array(df_dict[security][2]['bid_to_cover_ratio'], dtype=float), 'bid to cover ratio', 'style=dot_line']],''],
)
plot_many_figure(datas, max_height=700)
def plot_onrrp_data():
path = os.path.join(fed_dir, 'onrrp'+'.csv')
df = pd.read_csv(path)
t1 = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d'))
onrrp_amount = np.array(df['Overnight Reverse Repurchase Agreements: Treasury Securities Sold by the Federal Reserve in the Temporary Open Market Operations'], dtype=float)
path = os.path.join(fed_dir, 'Factors Affecting Reserve Balances of Depository Institutions'+'.csv')
df = pd.read_csv(path)
t2 = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d'))
tga = np.array(df['Liabilities and Capital: Liabilities: Deposits with F.R. Banks, other than reserve balances: U.S. Treasury, General Account: Wednesday level'], dtype=float)
datas = [
[[[t1,onrrp_amount,'ONRRP $bn','color=black'],
[t2,tga/1000,'TGA $bn','']
],
[],''],
]
plot_many_figure(datas, start_time='2015-01-01')
if __name__=="__main__":
update_treasury_auction_data()
# plot_treasury_auction_data()
# plot_onrrp_data()