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 from_protos_v2(
        proto_list: List[fra.ForwardRateAgreement],
        config: "ForwardRateAgreementConfig" = None) -> Dict[str, Any]:
    """Creates a dictionary of preprocessed swap data."""
    prepare_fras = {}
    for fra_proto in proto_list:
        short_position = fra_proto.short_position
        h, currency, rate_term, rate_index = _get_hash_v2(fra_proto)
        fixing_date = fra_proto.fixing_date
        fixing_date = [fixing_date.year, fixing_date.month, fixing_date.day]
        notional_amount = instrument_utils.decimal_to_double(
            fra_proto.notional_amount)
        daycount_convention = daycount_conventions.from_proto_value(
            fra_proto.daycount_convention)
        business_day_convention = business_days.convention_from_proto_value(
            fra_proto.business_day_convention)
        fixed_rate = instrument_utils.decimal_to_double(fra_proto.fixed_rate)
        calendar = business_days.holiday_from_proto_value(
            fra_proto.bank_holidays)
        settlement_days = fra_proto.settlement_days
        name = fra_proto.metadata.id
        instrument_type = fra_proto.metadata.instrument_type
        if h not in prepare_fras:
            prepare_fras[h] = {
                "short_position": [short_position],
                "currency": [currency],
                "fixing_date": [fixing_date],
                "fixed_rate": [fixed_rate],
                "notional_amount": [notional_amount],
                "daycount_convention": daycount_convention,
                "business_day_convention": business_day_convention,
                "calendar": calendar,
                "rate_term": rate_term,
                "rate_index": [rate_index],
                "settlement_days": [settlement_days],
                "config": config,
                "batch_names": [[name, instrument_type]]
            }
        else:
            prepare_fras[h]["currency"].append(currency)
            prepare_fras[h]["fixing_date"].append(fixing_date)
            prepare_fras[h]["short_position"].append(short_position)
            prepare_fras[h]["fixed_rate"].append(fixed_rate)
            prepare_fras[h]["rate_index"].append(rate_index)
            current_rate_term = prepare_fras[h]["rate_term"]
            rate_term_type = current_rate_term[0]
            rate_term_amount = current_rate_term[1]
            prepare_fras[h]["rate_term"] = (rate_term_type,
                                            rate_term_amount + rate_term[1])
            prepare_fras[h]["notional_amount"].append(notional_amount)
            prepare_fras[h]["settlement_days"].append(settlement_days)
            prepare_fras[h]["batch_names"].append([name, instrument_type])
    return prepare_fras
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))
    def from_protos(
        cls,
        proto_list: List[fra.ForwardRateAgreement],
        fra_config: ForwardRateAgreementConfig = None
    ) -> List["ForwardRateAgreement"]:
        prepare_swaps = {}
        for fra_instance in proto_list:
            short_position = fra_instance.short_position
            currency = currencies.from_proto_value(fra_instance.currency)
            bank_holidays = fra_instance.bank_holidays
            daycount_convention = fra_instance.daycount_convention
            business_day_convention = fra_instance.business_day_convention
            rate_index = fra_instance.floating_rate_term.floating_rate_type
            rate_index = rate_indices.from_proto_value(rate_index)
            rate_term = fra_instance.floating_rate_term.term

            h = hash(
                tuple([currency] + [bank_holidays] + [rate_term.type] +
                      [rate_term.amount] + [rate_index] +
                      [daycount_convention] + [business_day_convention]))
            fixing_date = fra_instance.fixing_date
            fixing_date = [
                fixing_date.year, fixing_date.month, fixing_date.day
            ]
            notional_amount = instrument_utils.decimal_to_double(
                fra_instance.notional_amount)
            daycount_convention = daycount_conventions.from_proto_value(
                fra_instance.daycount_convention)
            business_day_convention = business_days.convention_from_proto_value(
                fra_instance.business_day_convention)
            fixed_rate = instrument_utils.decimal_to_double(
                fra_instance.fixed_rate)
            calendar = business_days.holiday_from_proto_value(
                fra_instance.bank_holidays)
            settlement_days = fra_instance.settlement_days
            name = fra_instance.metadata.id
            instrument_type = fra_instance.metadata.instrument_type
            if h not in prepare_swaps:
                prepare_swaps[h] = {
                    "short_position": short_position,
                    "currency": currency,
                    "fixing_date": [fixing_date],
                    "fixed_rate": [fixed_rate],
                    "notional_amount": [notional_amount],
                    "daycount_convention": daycount_convention,
                    "business_day_convention": business_day_convention,
                    "calendar": calendar,
                    "rate_term": rate_term,
                    "rate_index": [rate_index],
                    "settlement_days": [settlement_days],
                    "fra_config": fra_config,
                    "batch_names": [[name, instrument_type]]
                }
            else:
                prepare_swaps[h]["fixing_date"].append(fixing_date)
                prepare_swaps[h]["fixed_rate"].append(fixed_rate)
                prepare_swaps[h]["notional_amount"].append(notional_amount)
                prepare_swaps[h]["rate_index"].append(rate_index)
                prepare_swaps[h]["settlement_days"].append(settlement_days)
                prepare_swaps[h]["batch_names"].append([name, instrument_type])
        intruments = []
        for kwargs in prepare_swaps.values():
            intruments.append(cls(**kwargs))
        return intruments