tea-bond 0.3.10__cp38-abi3-manylinux_2_34_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of tea-bond might be problematic. Click here for more details.
- pybond/__init__.py +16 -0
- pybond/bond.py +190 -0
- pybond/download.py +147 -0
- pybond/ffi/__init__.py +4 -0
- pybond/ffi/bond.py +68 -0
- pybond/ffi/datetime.py +58 -0
- pybond/ffi/duration.py +19 -0
- pybond/ffi/evaluators.py +186 -0
- pybond/ffi/lib.py +8 -0
- pybond/nb/__init__.py +31 -0
- pybond/nb/ir_utils.py +47 -0
- pybond/nb/nb_bond.py +213 -0
- pybond/nb/nb_date.py +209 -0
- pybond/nb/nb_datetime.py +415 -0
- pybond/nb/nb_duration.py +70 -0
- pybond/nb/nb_evaluators.py +557 -0
- pybond/nb/nb_time.py +279 -0
- pybond/pd.py +458 -0
- pybond/pl.py +480 -0
- pybond/pnl.py +108 -0
- pybond/polars_utils.py +96 -0
- pybond/pybond.abi3.so +0 -0
- pybond/pybond.pyi +391 -0
- pybond.libs/libcrypto-fca80f7e.so.3 +0 -0
- pybond.libs/libssl-e50e007b.so.3 +0 -0
- tea_bond-0.3.10.dist-info/METADATA +7 -0
- tea_bond-0.3.10.dist-info/RECORD +28 -0
- tea_bond-0.3.10.dist-info/WHEEL +4 -0
pybond/pd.py
ADDED
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
import polars as pl
|
|
5
|
+
|
|
6
|
+
from .pl import Bonds as PlBonds
|
|
7
|
+
from .pl import Futures as PlFutures
|
|
8
|
+
from .pl import TfEvaluators as PlTfEvaluators
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TfEvaluators:
|
|
12
|
+
"""
|
|
13
|
+
Treasury Futures Evaluators for pandas DataFrames.
|
|
14
|
+
|
|
15
|
+
A pandas-compatible wrapper around the Polars-based TfEvaluators that provides
|
|
16
|
+
various financial calculations for treasury futures and bonds analysis.
|
|
17
|
+
|
|
18
|
+
This class converts pandas inputs to Polars for computation and returns
|
|
19
|
+
pandas Series results for seamless integration with pandas workflows.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
future: str | pd.Series,
|
|
25
|
+
bond: str | pd.Series,
|
|
26
|
+
date: str | pd.Series,
|
|
27
|
+
future_price: pd.Series | None = None,
|
|
28
|
+
bond_ytm: pd.Series | None = None,
|
|
29
|
+
capital_rate: float | pd.Series | None = None,
|
|
30
|
+
reinvest_rate: float | None = None,
|
|
31
|
+
):
|
|
32
|
+
"""
|
|
33
|
+
Initialize TfEvaluators with market data.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
future: Future contract code(s)
|
|
37
|
+
bond: Bond code(s)
|
|
38
|
+
date: Evaluation date(s)
|
|
39
|
+
future_price: Future price(s)
|
|
40
|
+
bond_ytm: Bond yield to maturity
|
|
41
|
+
capital_rate: Capital cost rate for carry calculations
|
|
42
|
+
reinvest_rate: Reinvestment rate for coupon payments (optional)
|
|
43
|
+
"""
|
|
44
|
+
self.pl_df = pl.DataFrame(
|
|
45
|
+
{
|
|
46
|
+
"future": future,
|
|
47
|
+
"bond": bond,
|
|
48
|
+
"date": date,
|
|
49
|
+
"future_price": future_price,
|
|
50
|
+
"bond_ytm": bond_ytm,
|
|
51
|
+
"capital_rate": capital_rate,
|
|
52
|
+
}
|
|
53
|
+
)
|
|
54
|
+
self._evaluators = PlTfEvaluators(
|
|
55
|
+
future_price="future_price",
|
|
56
|
+
bond_ytm="bond_ytm",
|
|
57
|
+
capital_rate="capital_rate",
|
|
58
|
+
reinvest_rate=reinvest_rate,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def net_basis_spread(self):
|
|
63
|
+
"""
|
|
64
|
+
Calculate net basis spread (净基差).
|
|
65
|
+
|
|
66
|
+
Net basis spread = basis spread - carry return
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
pd.Series: Net basis spread values
|
|
70
|
+
"""
|
|
71
|
+
return self.pl_df.select(net_basis_spread=self._evaluators.net_basis_spread)[
|
|
72
|
+
"net_basis_spread"
|
|
73
|
+
].to_pandas()
|
|
74
|
+
|
|
75
|
+
@property
|
|
76
|
+
def accrued_interest(self):
|
|
77
|
+
"""
|
|
78
|
+
Calculate accrued interest (应计利息).
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
pd.Series: Accrued interest values
|
|
82
|
+
"""
|
|
83
|
+
return self.pl_df.select(accrued_interest=self._evaluators.accrued_interest)[
|
|
84
|
+
"accrued_interest"
|
|
85
|
+
].to_pandas()
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def deliver_accrued_interest(self):
|
|
89
|
+
"""
|
|
90
|
+
Calculate delivery accrued interest (国债期货交割应计利息).
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
pd.Series: Delivery accrued interest values
|
|
94
|
+
"""
|
|
95
|
+
return self.pl_df.select(
|
|
96
|
+
deliver_accrued_interest=self._evaluators.deliver_accrued_interest
|
|
97
|
+
)["deliver_accrued_interest"].to_pandas()
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def cf(self):
|
|
101
|
+
"""
|
|
102
|
+
Calculate conversion factor (转换因子).
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
pd.Series: Conversion factor values
|
|
106
|
+
"""
|
|
107
|
+
return self.pl_df.select(cf=self._evaluators.cf)["cf"].to_pandas()
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def dirty_price(self):
|
|
111
|
+
"""
|
|
112
|
+
Calculate bond dirty price (债券全价).
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
pd.Series: Bond dirty price values
|
|
116
|
+
"""
|
|
117
|
+
return self.pl_df.select(dirty_price=self._evaluators.dirty_price)[
|
|
118
|
+
"dirty_price"
|
|
119
|
+
].to_pandas()
|
|
120
|
+
|
|
121
|
+
@property
|
|
122
|
+
def clean_price(self):
|
|
123
|
+
"""
|
|
124
|
+
Calculate bond clean price (债券净价).
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
pd.Series: Bond clean price values
|
|
128
|
+
"""
|
|
129
|
+
return self.pl_df.select(clean_price=self._evaluators.clean_price)[
|
|
130
|
+
"clean_price"
|
|
131
|
+
].to_pandas()
|
|
132
|
+
|
|
133
|
+
@property
|
|
134
|
+
def future_dirty_price(self):
|
|
135
|
+
"""
|
|
136
|
+
Calculate future dirty price (期货全价/发票价格).
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
pd.Series: Future dirty price values
|
|
140
|
+
"""
|
|
141
|
+
return self.pl_df.select(
|
|
142
|
+
future_dirty_price=self._evaluators.future_dirty_price
|
|
143
|
+
)["future_dirty_price"].to_pandas()
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def deliver_cost(self):
|
|
147
|
+
"""
|
|
148
|
+
Calculate delivery cost (交割成本).
|
|
149
|
+
|
|
150
|
+
Delivery cost = bond dirty price - interim coupon payments
|
|
151
|
+
|
|
152
|
+
Returns:
|
|
153
|
+
pd.Series: Delivery cost values
|
|
154
|
+
"""
|
|
155
|
+
return self.pl_df.select(deliver_cost=self._evaluators.deliver_cost)[
|
|
156
|
+
"deliver_cost"
|
|
157
|
+
].to_pandas()
|
|
158
|
+
|
|
159
|
+
@property
|
|
160
|
+
def basis_spread(self):
|
|
161
|
+
"""
|
|
162
|
+
Calculate basis spread (基差).
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
pd.Series: Basis spread values
|
|
166
|
+
"""
|
|
167
|
+
return self.pl_df.select(basis_spread=self._evaluators.basis_spread)[
|
|
168
|
+
"basis_spread"
|
|
169
|
+
].to_pandas()
|
|
170
|
+
|
|
171
|
+
@property
|
|
172
|
+
def f_b_spread(self):
|
|
173
|
+
"""
|
|
174
|
+
Calculate futures-bond spread (期现价差).
|
|
175
|
+
|
|
176
|
+
Returns:
|
|
177
|
+
pd.Series: Futures-bond spread values
|
|
178
|
+
"""
|
|
179
|
+
return self.pl_df.select(f_b_spread=self._evaluators.f_b_spread)[
|
|
180
|
+
"f_b_spread"
|
|
181
|
+
].to_pandas()
|
|
182
|
+
|
|
183
|
+
@property
|
|
184
|
+
def carry(self):
|
|
185
|
+
"""
|
|
186
|
+
Calculate carry return (持有收益).
|
|
187
|
+
|
|
188
|
+
Carry return = (delivery accrued - trading accrued + interim coupons) +
|
|
189
|
+
capital cost rate * (weighted average interim coupons - bond dirty price * remaining days / 365)
|
|
190
|
+
|
|
191
|
+
Returns:
|
|
192
|
+
pd.Series: Carry return values
|
|
193
|
+
"""
|
|
194
|
+
return self.pl_df.select(carry=self._evaluators.carry)["carry"].to_pandas()
|
|
195
|
+
|
|
196
|
+
@property
|
|
197
|
+
def duration(self):
|
|
198
|
+
"""
|
|
199
|
+
Calculate modified duration (修正久期).
|
|
200
|
+
|
|
201
|
+
Returns:
|
|
202
|
+
pd.Series: Modified duration values
|
|
203
|
+
"""
|
|
204
|
+
return self.pl_df.select(duration=self._evaluators.duration)[
|
|
205
|
+
"duration"
|
|
206
|
+
].to_pandas()
|
|
207
|
+
|
|
208
|
+
@property
|
|
209
|
+
def irr(self):
|
|
210
|
+
"""
|
|
211
|
+
Calculate internal rate of return (内部收益率).
|
|
212
|
+
|
|
213
|
+
Returns:
|
|
214
|
+
pd.Series: Internal rate of return values
|
|
215
|
+
"""
|
|
216
|
+
return self.pl_df.select(irr=self._evaluators.irr)["irr"].to_pandas()
|
|
217
|
+
|
|
218
|
+
@property
|
|
219
|
+
def future_ytm(self):
|
|
220
|
+
"""
|
|
221
|
+
Calculate futures implied yield to maturity (期货隐含收益率).
|
|
222
|
+
|
|
223
|
+
Returns:
|
|
224
|
+
pd.Series: Futures implied yield to maturity values
|
|
225
|
+
"""
|
|
226
|
+
return self.pl_df.select(future_ytm=self._evaluators.future_ytm)[
|
|
227
|
+
"future_ytm"
|
|
228
|
+
].to_pandas()
|
|
229
|
+
|
|
230
|
+
@property
|
|
231
|
+
def remain_cp_to_deliver(self):
|
|
232
|
+
"""
|
|
233
|
+
Calculate remaining coupon payments to delivery (到交割的期间付息).
|
|
234
|
+
|
|
235
|
+
Returns:
|
|
236
|
+
pd.Series: Remaining coupon payments to delivery values
|
|
237
|
+
"""
|
|
238
|
+
return self.pl_df.select(
|
|
239
|
+
remain_cp_to_deliver=self._evaluators.remain_cp_to_deliver
|
|
240
|
+
)["remain_cp_to_deliver"].to_pandas()
|
|
241
|
+
|
|
242
|
+
@property
|
|
243
|
+
def remain_cp_to_deliver_wm(self):
|
|
244
|
+
"""
|
|
245
|
+
Calculate weighted average remaining coupon payments to delivery (加权平均到交割的期间付息).
|
|
246
|
+
|
|
247
|
+
Returns:
|
|
248
|
+
pd.Series: Weighted average remaining coupon payments to delivery values
|
|
249
|
+
"""
|
|
250
|
+
return self.pl_df.select(
|
|
251
|
+
remain_cp_to_deliver_wm=self._evaluators.remain_cp_to_deliver_wm
|
|
252
|
+
)["remain_cp_to_deliver_wm"].to_pandas()
|
|
253
|
+
|
|
254
|
+
@property
|
|
255
|
+
def remain_cp_num(self):
|
|
256
|
+
"""
|
|
257
|
+
Calculate remaining number of coupon payments (债券剩余付息次数).
|
|
258
|
+
|
|
259
|
+
Returns:
|
|
260
|
+
pd.Series: Remaining number of coupon payments values
|
|
261
|
+
"""
|
|
262
|
+
return self.pl_df.select(remain_cp_num=self._evaluators.remain_cp_num)[
|
|
263
|
+
"remain_cp_num"
|
|
264
|
+
].to_pandas()
|
|
265
|
+
|
|
266
|
+
@property
|
|
267
|
+
def deliver_date(self):
|
|
268
|
+
"""
|
|
269
|
+
Calculate delivery date (交割日).
|
|
270
|
+
|
|
271
|
+
Returns:
|
|
272
|
+
pd.Series: Delivery date values
|
|
273
|
+
"""
|
|
274
|
+
return self.pl_df.select(deliver_date=self._evaluators.deliver_date)[
|
|
275
|
+
"deliver_date"
|
|
276
|
+
].to_pandas()
|
|
277
|
+
|
|
278
|
+
@property
|
|
279
|
+
def last_trading_date(self):
|
|
280
|
+
"""
|
|
281
|
+
Calculate last trading date (最后交易日).
|
|
282
|
+
|
|
283
|
+
Returns:
|
|
284
|
+
pd.Series: Last trading date values
|
|
285
|
+
"""
|
|
286
|
+
return self.pl_df.select(last_trading_date=self._evaluators.last_trading_date)[
|
|
287
|
+
"last_trading_date"
|
|
288
|
+
].to_pandas()
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
class Bonds:
|
|
292
|
+
"""
|
|
293
|
+
Bond calculations for pandas DataFrames.
|
|
294
|
+
|
|
295
|
+
A pandas-compatible wrapper around the Polars-based Bonds class that provides
|
|
296
|
+
bond-specific financial calculations without requiring futures contract information.
|
|
297
|
+
"""
|
|
298
|
+
|
|
299
|
+
def __init__(self, bond: str | pd.Series):
|
|
300
|
+
"""
|
|
301
|
+
Initialize Bonds with bond identifier.
|
|
302
|
+
|
|
303
|
+
Args:
|
|
304
|
+
bond: Bond code(s)
|
|
305
|
+
"""
|
|
306
|
+
self.bond = bond
|
|
307
|
+
|
|
308
|
+
def accrued_interest(self, date: str | pd.Series):
|
|
309
|
+
"""
|
|
310
|
+
Calculate accrued interest for the bond (应计利息).
|
|
311
|
+
|
|
312
|
+
Args:
|
|
313
|
+
date: Evaluation date(s)
|
|
314
|
+
|
|
315
|
+
Returns:
|
|
316
|
+
pd.Series: Accrued interest values
|
|
317
|
+
"""
|
|
318
|
+
df = pl.DataFrame({"bond": self.bond, "date": date})
|
|
319
|
+
return df.select(accrued_interest=PlBonds("bond").accrued_interest("date"))[
|
|
320
|
+
"accrued_interest"
|
|
321
|
+
].to_pandas()
|
|
322
|
+
|
|
323
|
+
def clean_price(self, ytm: float | pd.Series, date: str | pd.Series):
|
|
324
|
+
"""
|
|
325
|
+
Calculate bond clean price (债券净价).
|
|
326
|
+
|
|
327
|
+
Args:
|
|
328
|
+
ytm: Yield to maturity
|
|
329
|
+
date: Evaluation date(s)
|
|
330
|
+
|
|
331
|
+
Returns:
|
|
332
|
+
pd.Series: Bond clean price values
|
|
333
|
+
"""
|
|
334
|
+
df = pl.DataFrame({"bond": self.bond, "ytm": ytm, "date": date})
|
|
335
|
+
return df.select(clean_price=PlBonds("bond").clean_price("ytm", "date"))[
|
|
336
|
+
"clean_price"
|
|
337
|
+
].to_pandas()
|
|
338
|
+
|
|
339
|
+
def dirty_price(self, ytm: float | pd.Series, date: str | pd.Series):
|
|
340
|
+
"""
|
|
341
|
+
Calculate bond dirty price (债券全价).
|
|
342
|
+
|
|
343
|
+
Args:
|
|
344
|
+
ytm: Yield to maturity
|
|
345
|
+
date: Evaluation date(s)
|
|
346
|
+
|
|
347
|
+
Returns:
|
|
348
|
+
pd.Series: Bond dirty price values
|
|
349
|
+
"""
|
|
350
|
+
df = pl.DataFrame({"bond": self.bond, "ytm": ytm, "date": date})
|
|
351
|
+
return df.select(dirty_price=PlBonds("bond").dirty_price("ytm", "date"))[
|
|
352
|
+
"dirty_price"
|
|
353
|
+
].to_pandas()
|
|
354
|
+
|
|
355
|
+
def duration(self, ytm: float | pd.Series, date: str | pd.Series):
|
|
356
|
+
"""
|
|
357
|
+
Calculate modified duration (修正久期).
|
|
358
|
+
|
|
359
|
+
Args:
|
|
360
|
+
ytm: Yield to maturity
|
|
361
|
+
date: Evaluation date(s)
|
|
362
|
+
|
|
363
|
+
Returns:
|
|
364
|
+
pd.Series: Modified duration values
|
|
365
|
+
"""
|
|
366
|
+
df = pl.DataFrame({"bond": self.bond, "ytm": ytm, "date": date})
|
|
367
|
+
return df.select(duration=PlBonds("bond").duration("ytm", "date"))[
|
|
368
|
+
"duration"
|
|
369
|
+
].to_pandas()
|
|
370
|
+
|
|
371
|
+
def remain_cp_num(self, date: str | pd.Series):
|
|
372
|
+
"""
|
|
373
|
+
Calculate remaining number of coupon payments (债券剩余付息次数).
|
|
374
|
+
|
|
375
|
+
Args:
|
|
376
|
+
date: Evaluation date(s)
|
|
377
|
+
|
|
378
|
+
Returns:
|
|
379
|
+
pd.Series: Remaining number of coupon payments values
|
|
380
|
+
"""
|
|
381
|
+
df = pl.DataFrame({"bond": self.bond, "date": date})
|
|
382
|
+
return df.select(remain_cp_num=PlBonds("bond").remain_cp_num("date"))[
|
|
383
|
+
"remain_cp_num"
|
|
384
|
+
].to_pandas()
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
class Futures:
|
|
388
|
+
def __init__(self, future: str | pd.Series):
|
|
389
|
+
self.future = future
|
|
390
|
+
|
|
391
|
+
def deliver_date(self):
|
|
392
|
+
"""
|
|
393
|
+
Calculate delivery date (交割日).
|
|
394
|
+
|
|
395
|
+
Args:
|
|
396
|
+
date: Evaluation date(s)
|
|
397
|
+
|
|
398
|
+
Returns:
|
|
399
|
+
pd.Series: Delivery date values
|
|
400
|
+
"""
|
|
401
|
+
df = pl.DataFrame({"future": self.future})
|
|
402
|
+
return df.select(deliver_date=PlFutures("future").deliver_date())[
|
|
403
|
+
"deliver_date"
|
|
404
|
+
].to_pandas()
|
|
405
|
+
|
|
406
|
+
def last_trading_date(self):
|
|
407
|
+
"""
|
|
408
|
+
Calculate last trading date (最后交易日).
|
|
409
|
+
|
|
410
|
+
Args:
|
|
411
|
+
date: Evaluation date(s)
|
|
412
|
+
|
|
413
|
+
Returns:
|
|
414
|
+
pd.Series: Last trading date values
|
|
415
|
+
"""
|
|
416
|
+
df = pl.DataFrame({"future": self.future})
|
|
417
|
+
return df.select(last_trading_date=PlFutures("future").last_trading_date())[
|
|
418
|
+
"last_trading_date"
|
|
419
|
+
].to_pandas()
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def find_workday(date: str | pd.Series, market: str, offset: int = 0):
|
|
423
|
+
"""
|
|
424
|
+
Find the workday based on the given date and market calendar.
|
|
425
|
+
|
|
426
|
+
Args:
|
|
427
|
+
date: Input date(s)
|
|
428
|
+
market: Market identifier ("IB" or "SSE")
|
|
429
|
+
offset: Number of workdays to offset (default: 0)
|
|
430
|
+
|
|
431
|
+
Returns:
|
|
432
|
+
pd.Series: Adjusted workday values
|
|
433
|
+
"""
|
|
434
|
+
from .pl import find_workday as pl_find_workday
|
|
435
|
+
|
|
436
|
+
df = pl.DataFrame({"date": date}).select(pl.col("date").dt.date())
|
|
437
|
+
return df.select(workday=pl_find_workday("date", market, offset))[
|
|
438
|
+
"workday"
|
|
439
|
+
].to_pandas()
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def is_business_day(date: str | pd.Series, market: str):
|
|
443
|
+
"""
|
|
444
|
+
Check if the given date is a business day for the specified market.
|
|
445
|
+
|
|
446
|
+
Args:
|
|
447
|
+
date: Input date(s)
|
|
448
|
+
market: Market identifier ("IB" or "SSE")
|
|
449
|
+
|
|
450
|
+
Returns:
|
|
451
|
+
pd.Series: Boolean values indicating if dates are business days
|
|
452
|
+
"""
|
|
453
|
+
from .pl import is_business_day as pl_is_business_day
|
|
454
|
+
|
|
455
|
+
df = pl.DataFrame({"date": date}).select(pl.col("date").dt.date())
|
|
456
|
+
return df.select(is_business=pl_is_business_day("date", market))[
|
|
457
|
+
"is_business"
|
|
458
|
+
].to_pandas()
|