tea-bond 0.3.11__cp38-abi3-manylinux_2_28_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.

@@ -0,0 +1,557 @@
1
+ from llvmlite import ir
2
+ from numba import types
3
+ from numba.core import cgutils, utils
4
+ from numba.extending import (
5
+ as_numba_type,
6
+ box,
7
+ intrinsic,
8
+ lower_builtin,
9
+ make_attribute_wrapper,
10
+ models,
11
+ overload_attribute,
12
+ overload_method,
13
+ register_model,
14
+ type_callable,
15
+ typeof_impl,
16
+ )
17
+
18
+ from pybond import TfEvaluator
19
+ from pybond.ffi import (
20
+ tf_evaluator_accrued_interest,
21
+ tf_evaluator_basis_spread,
22
+ tf_evaluator_bond_ytm,
23
+ tf_evaluator_calc_all,
24
+ tf_evaluator_capital_rate,
25
+ tf_evaluator_carry,
26
+ tf_evaluator_cf,
27
+ tf_evaluator_clean_price,
28
+ tf_evaluator_deliver_accrued_interest,
29
+ tf_evaluator_deliver_cost,
30
+ tf_evaluator_dirty_price,
31
+ tf_evaluator_duration,
32
+ tf_evaluator_f_b_spread,
33
+ tf_evaluator_future_dirty_price,
34
+ tf_evaluator_future_price,
35
+ tf_evaluator_future_ytm,
36
+ tf_evaluator_irr,
37
+ tf_evaluator_is_deliverable,
38
+ tf_evaluator_net_basis_spread,
39
+ tf_evaluator_reinvest_rate,
40
+ tf_evaluator_remain_cp_num,
41
+ tf_evaluator_remain_cp_to_deliver,
42
+ tf_evaluator_remain_cp_to_deliver_wm,
43
+ tf_evaluator_remain_days_to_deliver,
44
+ )
45
+
46
+ from .nb_date import date_type
47
+
48
+
49
+ class TfEvaluatorType(types.Type):
50
+ def __init__(self):
51
+ super().__init__(name="TfEvaluator")
52
+
53
+
54
+ tf_evaluator_type = TfEvaluatorType()
55
+ as_numba_type.register(TfEvaluator, tf_evaluator_type)
56
+
57
+
58
+ @typeof_impl.register(TfEvaluator)
59
+ def typeof_tf_evaluator(val, c):
60
+ return tf_evaluator_type
61
+
62
+
63
+ @type_callable(TfEvaluator)
64
+ def type_tf_evaluator(context):
65
+ def typer(
66
+ future,
67
+ bond,
68
+ date=None,
69
+ future_price=float("nan"),
70
+ bond_ytm=float("nan"),
71
+ capital_rate=float("nan"),
72
+ reinvest_rate=None,
73
+ ):
74
+ return tf_evaluator_type
75
+
76
+ return typer
77
+
78
+
79
+ @register_model(TfEvaluatorType)
80
+ class TfEvaluatorModel(models.StructModel):
81
+ def __init__(self, dmm, fe_type):
82
+ members = [
83
+ ("ptr", types.voidptr),
84
+ ]
85
+ models.StructModel.__init__(self, dmm, fe_type, members)
86
+
87
+
88
+ make_attribute_wrapper(TfEvaluatorType, "ptr", "ptr")
89
+
90
+
91
+ @lower_builtin(
92
+ TfEvaluator,
93
+ types.string,
94
+ types.string,
95
+ date_type,
96
+ types.float64,
97
+ types.float64,
98
+ types.float64,
99
+ )
100
+ def impl_tf_evaluator_builder(context, builder, sig, args):
101
+ typ = sig.return_type
102
+ (
103
+ future,
104
+ bond,
105
+ date_val,
106
+ future_price,
107
+ bond_ytm,
108
+ capital_rate,
109
+ ) = args
110
+
111
+ # Get string data from Numba strings
112
+ future_code = context.make_helper(builder, types.string, future)
113
+ bond_code = context.make_helper(builder, types.string, bond)
114
+ date = context.make_helper(builder, date_type, date_val)
115
+
116
+ # Call create_tf_evaluator
117
+ fn = cgutils.get_or_insert_function(
118
+ builder.module,
119
+ ir.FunctionType(
120
+ ir.PointerType(ir.IntType(8)),
121
+ [
122
+ ir.PointerType(ir.IntType(8)), # future_code_ptr
123
+ ir.IntType(utils.MACHINE_BITS), # future_code_len
124
+ ir.DoubleType(), # future_price
125
+ ir.PointerType(ir.IntType(8)), # bond_code_ptr
126
+ ir.IntType(utils.MACHINE_BITS), # bond_code_len
127
+ ir.DoubleType(), # bond_ytm
128
+ ir.DoubleType(), # capital_rate
129
+ ir.IntType(32), # year
130
+ ir.IntType(32), # month
131
+ ir.IntType(32), # day
132
+ ],
133
+ ),
134
+ name="create_tf_evaluator",
135
+ )
136
+
137
+ ptr = builder.call(
138
+ fn,
139
+ [
140
+ future_code.data,
141
+ future_code.length,
142
+ future_price,
143
+ bond_code.data,
144
+ bond_code.length,
145
+ bond_ytm,
146
+ capital_rate,
147
+ date.year,
148
+ date.month,
149
+ date.day,
150
+ ],
151
+ )
152
+
153
+ # Create TfEvaluator object
154
+ evaluator = cgutils.create_struct_proxy(typ)(context, builder)
155
+ evaluator.ptr = ptr
156
+ return evaluator._getvalue()
157
+
158
+
159
+ @lower_builtin(
160
+ TfEvaluator,
161
+ types.string,
162
+ types.string,
163
+ date_type,
164
+ types.float64,
165
+ types.float64,
166
+ types.float64,
167
+ types.float64,
168
+ )
169
+ def impl_tf_evaluator_builder_with_reinvest(context, builder, sig, args):
170
+ typ = sig.return_type
171
+ (
172
+ future_code_val,
173
+ bond_code_val,
174
+ date_val,
175
+ future_price_val,
176
+ bond_ytm_val,
177
+ capital_rate_val,
178
+ reinvest_rate_val,
179
+ ) = args
180
+
181
+ # Get string data from Numba strings
182
+ future_code = context.make_helper(builder, types.string, future_code_val)
183
+ bond_code = context.make_helper(builder, types.string, bond_code_val)
184
+
185
+ # Get date components
186
+ date = context.make_helper(builder, date_type, date_val)
187
+
188
+ # Call create_tf_evaluator_with_reinvest
189
+ fn = cgutils.get_or_insert_function(
190
+ builder.module,
191
+ ir.FunctionType(
192
+ ir.PointerType(ir.IntType(8)),
193
+ [
194
+ ir.PointerType(ir.IntType(8)), # future_code_ptr
195
+ ir.IntType(utils.MACHINE_BITS), # future_code_len
196
+ ir.DoubleType(), # future_price
197
+ ir.PointerType(ir.IntType(8)), # bond_code_ptr
198
+ ir.IntType(utils.MACHINE_BITS), # bond_code_len
199
+ ir.DoubleType(), # bond_ytm
200
+ ir.DoubleType(), # capital_rate
201
+ ir.DoubleType(), # reinvest_rate
202
+ ir.IntType(32), # year
203
+ ir.IntType(32), # month
204
+ ir.IntType(32), # day
205
+ ],
206
+ ),
207
+ name="create_tf_evaluator_with_reinvest",
208
+ )
209
+
210
+ ptr = builder.call(
211
+ fn,
212
+ [
213
+ future_code.data,
214
+ future_code.length,
215
+ future_price_val,
216
+ bond_code.data,
217
+ bond_code.length,
218
+ bond_ytm_val,
219
+ capital_rate_val,
220
+ reinvest_rate_val,
221
+ date.year,
222
+ date.month,
223
+ date.day,
224
+ ],
225
+ )
226
+
227
+ # Create TfEvaluator object
228
+ evaluator = cgutils.create_struct_proxy(typ)(context, builder)
229
+ evaluator.ptr = ptr
230
+ return evaluator._getvalue()
231
+
232
+
233
+ # Property getters
234
+ @overload_attribute(TfEvaluatorType, "is_deliverable")
235
+ def tf_evaluator_attr_is_deliverable(evaluator):
236
+ def impl(evaluator):
237
+ return tf_evaluator_is_deliverable(evaluator.ptr) == 1
238
+
239
+ return impl
240
+
241
+
242
+ @overload_attribute(TfEvaluatorType, "bond_ytm")
243
+ def tf_evaluator_attr_bond_ytm(evaluator):
244
+ def impl(evaluator):
245
+ return tf_evaluator_bond_ytm(evaluator.ptr)
246
+
247
+ return impl
248
+
249
+
250
+ @overload_attribute(TfEvaluatorType, "future_price")
251
+ def tf_evaluator_attr_future_price(evaluator):
252
+ def impl(evaluator):
253
+ return tf_evaluator_future_price(evaluator.ptr)
254
+
255
+ return impl
256
+
257
+
258
+ @overload_attribute(TfEvaluatorType, "capital_rate")
259
+ def tf_evaluator_attr_capital_rate(evaluator):
260
+ def impl(evaluator):
261
+ return tf_evaluator_capital_rate(evaluator.ptr)
262
+
263
+ return impl
264
+
265
+
266
+ @overload_attribute(TfEvaluatorType, "reinvest_rate")
267
+ def tf_evaluator_attr_reinvest_rate(evaluator):
268
+ def impl(evaluator):
269
+ return tf_evaluator_reinvest_rate(evaluator.ptr)
270
+
271
+ return impl
272
+
273
+
274
+ # Calculation methods
275
+ @overload_attribute(TfEvaluatorType, "accrued_interest")
276
+ def tf_evaluator_method_accrued_interest(evaluator):
277
+ def impl(evaluator):
278
+ return tf_evaluator_accrued_interest(evaluator.ptr)
279
+
280
+ return impl
281
+
282
+
283
+ @overload_attribute(TfEvaluatorType, "deliver_accrued_interest")
284
+ def tf_evaluator_method_deliver_accrued_interest(evaluator):
285
+ def impl(evaluator):
286
+ return tf_evaluator_deliver_accrued_interest(evaluator.ptr)
287
+
288
+ return impl
289
+
290
+
291
+ @overload_attribute(TfEvaluatorType, "cf")
292
+ def tf_evaluator_method_cf(evaluator):
293
+ def impl(evaluator):
294
+ return tf_evaluator_cf(evaluator.ptr)
295
+
296
+ return impl
297
+
298
+
299
+ @overload_attribute(TfEvaluatorType, "dirty_price")
300
+ def tf_evaluator_method_dirty_price(evaluator):
301
+ def impl(evaluator):
302
+ return tf_evaluator_dirty_price(evaluator.ptr)
303
+
304
+ return impl
305
+
306
+
307
+ @overload_attribute(TfEvaluatorType, "clean_price")
308
+ def tf_evaluator_method_clean_price(evaluator):
309
+ def impl(evaluator):
310
+ return tf_evaluator_clean_price(evaluator.ptr)
311
+
312
+ return impl
313
+
314
+
315
+ @overload_attribute(TfEvaluatorType, "future_dirty_price")
316
+ def tf_evaluator_method_future_dirty_price(evaluator):
317
+ def impl(evaluator):
318
+ return tf_evaluator_future_dirty_price(evaluator.ptr)
319
+
320
+ return impl
321
+
322
+
323
+ @overload_attribute(TfEvaluatorType, "deliver_cost")
324
+ def tf_evaluator_method_deliver_cost(evaluator):
325
+ def impl(evaluator):
326
+ return tf_evaluator_deliver_cost(evaluator.ptr)
327
+
328
+ return impl
329
+
330
+
331
+ @overload_attribute(TfEvaluatorType, "basis_spread")
332
+ def tf_evaluator_method_basis_spread(evaluator):
333
+ def impl(evaluator):
334
+ return tf_evaluator_basis_spread(evaluator.ptr)
335
+
336
+ return impl
337
+
338
+
339
+ @overload_attribute(TfEvaluatorType, "f_b_spread")
340
+ def tf_evaluator_method_f_b_spread(evaluator):
341
+ def impl(evaluator):
342
+ return tf_evaluator_f_b_spread(evaluator.ptr)
343
+
344
+ return impl
345
+
346
+
347
+ @overload_attribute(TfEvaluatorType, "carry")
348
+ def tf_evaluator_method_carry(evaluator):
349
+ def impl(evaluator):
350
+ return tf_evaluator_carry(evaluator.ptr)
351
+
352
+ return impl
353
+
354
+
355
+ @overload_attribute(TfEvaluatorType, "net_basis_spread")
356
+ def tf_evaluator_method_net_basis_spread(evaluator):
357
+ def impl(evaluator):
358
+ return tf_evaluator_net_basis_spread(evaluator.ptr)
359
+
360
+ return impl
361
+
362
+
363
+ @overload_attribute(TfEvaluatorType, "duration")
364
+ def tf_evaluator_method_duration(evaluator):
365
+ def impl(evaluator):
366
+ return tf_evaluator_duration(evaluator.ptr)
367
+
368
+ return impl
369
+
370
+
371
+ @overload_attribute(TfEvaluatorType, "irr")
372
+ def tf_evaluator_method_irr(evaluator):
373
+ def impl(evaluator):
374
+ return tf_evaluator_irr(evaluator.ptr)
375
+
376
+ return impl
377
+
378
+
379
+ @overload_attribute(TfEvaluatorType, "future_ytm")
380
+ def tf_evaluator_method_future_ytm(evaluator):
381
+ def impl(evaluator):
382
+ return tf_evaluator_future_ytm(evaluator.ptr)
383
+
384
+ return impl
385
+
386
+
387
+ @overload_attribute(TfEvaluatorType, "remain_days_to_deliver")
388
+ def tf_evaluator_method_remain_days_to_deliver(evaluator):
389
+ def impl(evaluator):
390
+ return tf_evaluator_remain_days_to_deliver(evaluator.ptr)
391
+
392
+ return impl
393
+
394
+
395
+ @overload_attribute(TfEvaluatorType, "remain_cp_num")
396
+ def tf_evaluator_method_remain_cp_num(evaluator):
397
+ def impl(evaluator):
398
+ return tf_evaluator_remain_cp_num(evaluator.ptr)
399
+
400
+ return impl
401
+
402
+
403
+ @overload_attribute(TfEvaluatorType, "remain_cp_to_deliver")
404
+ def tf_evaluator_method_remain_cp_to_deliver(evaluator):
405
+ def impl(evaluator):
406
+ return tf_evaluator_remain_cp_to_deliver(evaluator.ptr)
407
+
408
+ return impl
409
+
410
+
411
+ @overload_attribute(TfEvaluatorType, "remain_cp_to_deliver_wm")
412
+ def tf_evaluator_method_remain_cp_to_deliver_wm(evaluator):
413
+ def impl(evaluator):
414
+ return tf_evaluator_remain_cp_to_deliver_wm(evaluator.ptr)
415
+
416
+ return impl
417
+
418
+
419
+ @overload_method(TfEvaluatorType, "calc_all")
420
+ def tf_evaluator_method_calc_all(evaluator):
421
+ def impl(evaluator):
422
+ _ = tf_evaluator_calc_all(evaluator.ptr)
423
+ return evaluator
424
+
425
+ return impl
426
+
427
+
428
+ @box(TfEvaluatorType)
429
+ def box_tf_evaluator(typ, val, c):
430
+ """
431
+ Convert a native TfEvaluator structure to python TfEvaluator.
432
+ """
433
+ evaluator = cgutils.create_struct_proxy(typ)(c.context, c.builder, value=val)
434
+
435
+ # Convert the pointer to usize for from_ptr method
436
+ ptr_as_usize = c.builder.ptrtoint(evaluator.ptr, ir.IntType(64))
437
+ ptr_obj = c.pyapi.from_native_value(types.uint64, ptr_as_usize)
438
+
439
+ # Get TfEvaluator class and call from_ptr
440
+ class_obj = c.pyapi.unserialize(c.pyapi.serialize_object(TfEvaluator))
441
+ from_ptr_method = c.pyapi.object_getattr_string(class_obj, "from_ptr")
442
+
443
+ # Call TfEvaluator.from_ptr(ptr)
444
+ res = c.pyapi.call_function_objargs(from_ptr_method, (ptr_obj,))
445
+
446
+ # Clean up Python objects
447
+ c.pyapi.decref(ptr_obj)
448
+ c.pyapi.decref(class_obj)
449
+ c.pyapi.decref(from_ptr_method)
450
+
451
+ return res
452
+
453
+
454
+ @intrinsic
455
+ def _tf_evaluator_update_call(
456
+ typingctx,
457
+ evaluator_t,
458
+ future_price_t,
459
+ bond_ytm_t,
460
+ date_t,
461
+ future_code_t,
462
+ bond_code_t,
463
+ capital_rate_t,
464
+ ):
465
+ """Intrinsic for calling tf_evaluator_update_info FFI function."""
466
+
467
+ def codegen(context, builder, sig, args):
468
+ (
469
+ evaluator_val,
470
+ future_price_val,
471
+ bond_ytm_val,
472
+ date_val,
473
+ future_code_val,
474
+ bond_code_val,
475
+ capital_rate_val,
476
+ ) = args
477
+
478
+ # Get existing evaluator
479
+ evaluator = cgutils.create_struct_proxy(tf_evaluator_type)(
480
+ context, builder, value=evaluator_val
481
+ )
482
+
483
+ # Get string data from Numba strings
484
+ future_code = context.make_helper(builder, types.string, future_code_val)
485
+ bond_code = context.make_helper(builder, types.string, bond_code_val)
486
+
487
+ # Get date components
488
+ date = context.make_helper(builder, date_type, date_val)
489
+
490
+ # Call tf_evaluator_update_info
491
+ fn = cgutils.get_or_insert_function(
492
+ builder.module,
493
+ ir.FunctionType(
494
+ ir.IntType(32), # return type: i32
495
+ [
496
+ ir.PointerType(ir.IntType(8)), # evaluator ptr
497
+ ir.PointerType(ir.IntType(8)), # future_code_ptr
498
+ ir.IntType(utils.MACHINE_BITS), # future_code_len
499
+ ir.DoubleType(), # future_price
500
+ ir.PointerType(ir.IntType(8)), # bond_code_ptr
501
+ ir.IntType(utils.MACHINE_BITS), # bond_code_len
502
+ ir.DoubleType(), # bond_ytm
503
+ ir.DoubleType(), # capital_rate
504
+ ir.IntType(32), # year
505
+ ir.IntType(32), # month
506
+ ir.IntType(32), # day
507
+ ],
508
+ ),
509
+ name="tf_evaluator_update_info",
510
+ )
511
+
512
+ result = builder.call(
513
+ fn,
514
+ [
515
+ evaluator.ptr,
516
+ future_code.data,
517
+ future_code.length,
518
+ future_price_val,
519
+ bond_code.data,
520
+ bond_code.length,
521
+ bond_ytm_val,
522
+ capital_rate_val,
523
+ date.year,
524
+ date.month,
525
+ date.day,
526
+ ],
527
+ )
528
+
529
+ return result
530
+
531
+ sig = types.int32(
532
+ evaluator_t,
533
+ future_price_t,
534
+ bond_ytm_t,
535
+ date_t,
536
+ future_code_t,
537
+ bond_code_t,
538
+ capital_rate_t,
539
+ )
540
+ return sig, codegen
541
+
542
+
543
+ @overload_method(TfEvaluatorType, "update")
544
+ # 采用和update pyi同样的参数顺序
545
+ def tf_evaluator_method_update(
546
+ evaluator, future_price, bond_ytm, date, future, bond, capital_rate
547
+ ):
548
+ def update_impl(
549
+ evaluator, future_price, bond_ytm, date, future, bond, capital_rate
550
+ ):
551
+ _result = _tf_evaluator_update_call(
552
+ evaluator, future_price, bond_ytm, date, future, bond, capital_rate
553
+ )
554
+ # Return the evaluator itself (it's been updated in-place)
555
+ return evaluator
556
+
557
+ return update_impl