Exemplo n.º 1
0
def leg_from_proto_v2(
    leg_proto: ir_swap.SwapLeg
) -> Union[coupon_specs.FixedCouponSpecs, coupon_specs.FloatCouponSpecs]:
    """Initialized coupon specifications from a proto instance."""
    if leg_proto.HasField("fixed_leg"):
        leg = leg_proto.fixed_leg
        coupon_freq = leg.coupon_frequency.type
        coupon_freq, coupon_freq_multiplier = _frequency_and_multiplier(
            coupon_freq)
        return coupon_specs.FixedCouponSpecs(
            currency=[currencies.from_proto_value(leg.currency)],
            coupon_frequency=(coupon_freq, [
                coupon_freq_multiplier * leg.coupon_frequency.amount
            ]),
            notional_amount=[
                instrument_utils.decimal_to_double(leg.notional_amount)
            ],
            fixed_rate=[instrument_utils.decimal_to_double(leg.fixed_rate)],
            settlement_days=[leg.settlement_days],
            businessday_rule=business_days.convention_from_proto_value(
                leg.business_day_convention),
            daycount_convention=daycount_conventions.from_proto_value(
                leg.daycount_convention),
            calendar=business_days.holiday_from_proto_value(leg.bank_holidays))
    else:
        leg = leg_proto.floating_leg
        # Get the index rate object
        rate_index = leg.floating_rate_type
        rate_index = rate_indices.RateIndex.from_proto(rate_index)
        rate_index.name = [rate_index.name]
        rate_index.source = [rate_index.source]
        coupon_freq = leg.coupon_frequency.type
        coupon_freq, coupon_freq_multiplier = _frequency_and_multiplier(
            coupon_freq)
        reset_freq = leg.reset_frequency.type
        reset_freq, reset_freq_multiplier = _frequency_and_multiplier(
            reset_freq)
        return coupon_specs.FloatCouponSpecs(
            currency=[currencies.from_proto_value(leg.currency)],
            coupon_frequency=(coupon_freq, [
                coupon_freq_multiplier * leg.coupon_frequency.amount
            ]),
            reset_frequency=(reset_freq, [
                reset_freq_multiplier * leg.reset_frequency.amount
            ]),
            notional_amount=[
                instrument_utils.decimal_to_double(leg.notional_amount)
            ],
            floating_rate_type=[rate_index],
            settlement_days=[leg.settlement_days],
            businessday_rule=business_days.convention_from_proto_value(
                leg.business_day_convention),
            daycount_convention=daycount_conventions.from_proto_value(
                leg.daycount_convention),
            spread=[instrument_utils.decimal_to_double(leg.spread)],
            calendar=business_days.holiday_from_proto_value(leg.bank_holidays))
Exemplo n.º 2
0
def _setup_leg(start_date, end_date, discount_curve_type, leg, schedule_fn,
               schedule, past_fixing):
    """Setup swap legs."""
    if isinstance(leg, coupon_specs.FixedCouponSpecs):
        return cashflow_streams.FixedCashflowStream(
            start_date=start_date,
            end_date=end_date,
            coupon_spec=leg,
            schedule_fn=schedule_fn,
            schedule=schedule,
            discount_curve_type=discount_curve_type,
            dtype=tf.float64)
    elif isinstance(leg, coupon_specs.FloatCouponSpecs):
        return cashflow_streams.FloatingCashflowStream(
            start_date=start_date,
            end_date=end_date,
            coupon_spec=leg,
            schedule_fn=schedule_fn,
            schedule=schedule,
            discount_curve_type=discount_curve_type,
            past_fixing=past_fixing,
            dtype=tf.float64)
    elif isinstance(leg, dict):
        coupon_spec = leg["coupon_spec"]
        if "fixed_rate" in coupon_spec:
            coupon_spec = coupon_specs.FixedCouponSpecs(**coupon_spec)
            return cashflow_streams.FixedCashflowStream(
                start_date=start_date,
                end_date=end_date,
                schedule_fn=schedule_fn,
                schedule=schedule,
                coupon_spec=coupon_spec,
                discount_curve_type=leg["discount_curve_type"],
                discount_curve_mask=leg["discount_curve_mask"],
                dtype=tf.float64)
        else:
            coupon_spec = coupon_specs.FloatCouponSpecs(**coupon_spec)
            return cashflow_streams.FloatingCashflowStream(
                start_date=start_date,
                end_date=end_date,
                schedule_fn=schedule_fn,
                schedule=schedule,
                coupon_spec=coupon_spec,
                discount_curve_type=leg["discount_curve_type"],
                discount_curve_mask=leg["discount_curve_mask"],
                reference_mask=leg["reference_mask"],
                rate_index_curves=leg["rate_index_curves"],
                past_fixing=past_fixing,
                dtype=tf.float64)
    else:
        raise ValueError(f"Unknown leg type {type(leg)}")
Exemplo n.º 3
0
def leg_from_proto(
    leg_proto: ir_swap.SwapLeg) -> Union[coupon_specs.FixedCouponSpecs,
                                         coupon_specs.FloatCouponSpecs]:
  """Initialized coupon specifications from a proto instance."""
  if leg_proto.HasField("fixed_leg"):
    leg = leg_proto.fixed_leg
    return coupon_specs.FixedCouponSpecs(
        currency=currencies.from_proto_value(leg.currency),
        coupon_frequency=leg.coupon_frequency,
        notional_amount=[instrument_utils.decimal_to_double(
            leg.notional_amount)],
        fixed_rate=[instrument_utils.decimal_to_double(
            leg.fixed_rate)],
        settlement_days=[leg.settlement_days],
        businessday_rule=business_days.convention_from_proto_value(
            leg.business_day_convention),
        daycount_convention=daycount_conventions.from_proto_value(
            leg.daycount_convention),
        calendar=business_days.holiday_from_proto_value(leg.bank_holidays))
  else:
    leg = leg_proto.floating_leg
    return coupon_specs.FloatCouponSpecs(
        currency=currencies.from_proto_value(leg.currency),
        coupon_frequency=leg.coupon_frequency,
        reset_frequency=leg.reset_frequency,
        notional_amount=[instrument_utils.decimal_to_double(
            leg.notional_amount)],
        floating_rate_type=[rate_indices.from_proto_value(
            leg.floating_rate_type)],
        settlement_days=[leg.settlement_days],
        businessday_rule=business_days.convention_from_proto_value(
            leg.business_day_convention),
        daycount_convention=daycount_conventions.from_proto_value(
            leg.daycount_convention),
        spread=[instrument_utils.decimal_to_double(leg.spread)],
        calendar=business_days.holiday_from_proto_value(leg.bank_holidays))