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))
def _get_hash( american_option_proto: american_option_pb2.AmericanEquityOption ) -> Tuple[int, types.CurrencyProtoType]: """Computes hash key for the batching strategy.""" currency = currencies.from_proto_value(american_option_proto.currency) bank_holidays = american_option_proto.bank_holidays business_day_convention = american_option_proto.business_day_convention h = utils.hasher([bank_holidays, business_day_convention]) return h, currency
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 _get_hash_v2( fra_proto: fra.ForwardRateAgreement ) -> Tuple[int, types.CurrencyProtoType, period_pb2.Period, rate_indices.RateIndex]: """Computes hash key for the batching strategy.""" currency = currencies.from_proto_value(fra_proto.currency) bank_holidays = fra_proto.bank_holidays daycount_convention = fra_proto.daycount_convention business_day_convention = fra_proto.business_day_convention # Get rate index rate_index = fra_proto.floating_rate_term.floating_rate_type rate_index = rate_indices.RateIndex.from_proto(rate_index) rate_term = fra_proto.floating_rate_term.term rate_type, multiplier = _frequency_and_multiplier(rate_term.type) h = _hasher( tuple([bank_holidays] + [daycount_convention] + [business_day_convention] + [rate_type])) return h, currency, (rate_type, [multiplier * rate_term.amount ]), rate_index
def _get_hash( fra_proto: fra.ForwardRateAgreement ) -> Tuple[int, types.CurrencyProtoType, period_pb2.Period, rate_indices.RateIndex]: """Computes hash key for the batching strategy.""" currency = currencies.from_proto_value(fra_proto.currency) bank_holidays = fra_proto.bank_holidays daycount_convention = fra_proto.daycount_convention business_day_convention = fra_proto.business_day_convention # Get rate index rate_index = fra_proto.floating_rate_term.floating_rate_type rate_index = rate_indices.RateIndex.from_proto(rate_index) rate_index.name = [rate_index.name] rate_index.source = [rate_index.source] rate_term = fra_proto.floating_rate_term.term h = _hasher( tuple([currency.value] + [bank_holidays] + [rate_term.type] + [rate_term.amount] + [rate_index.type.value] + [daycount_convention] + [business_day_convention])) return h, currency, rate_term, rate_index
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