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 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
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( proto_list: List[american_option_pb2.AmericanEquityOption], american_option_config: "AmericanOptionConfig" = None ) -> Dict[str, Any]: """Creates a dictionary of preprocessed swap data.""" prepare_fras = {} for am_option_proto in proto_list: short_position = am_option_proto.short_position h, currency = _get_hash(am_option_proto) expiry_date = am_option_proto.expiry_date expiry_date = [expiry_date.year, expiry_date.month, expiry_date.day] equity = am_option_proto.equity contract_amount = instrument_utils.decimal_to_double( am_option_proto.contract_amount) business_day_convention = business_days.convention_from_proto_value( am_option_proto.business_day_convention) strike = instrument_utils.decimal_to_double(am_option_proto.strike) calendar = business_days.holiday_from_proto_value( am_option_proto.bank_holidays) settlement_days = am_option_proto.settlement_days is_call_option = am_option_proto.is_call_option name = am_option_proto.metadata.id instrument_type = am_option_proto.metadata.instrument_type if h not in prepare_fras: prepare_fras[h] = { "short_position": [short_position], "currency": currency, "expiry_date": [expiry_date], "equity": [equity], "contract_amount": [contract_amount], "business_day_convention": business_day_convention, "calendar": calendar, "strike": [strike], "is_call_option": [is_call_option], "settlement_days": [settlement_days], "american_option_config": american_option_config, "batch_names": [[name, instrument_type]] } else: prepare_fras[h]["short_position"].append(short_position) prepare_fras[h]["expiry_date"].append(expiry_date) prepare_fras[h]["equity"].append(equity) prepare_fras[h]["contract_amount"].append(contract_amount) prepare_fras[h]["strike"].append(strike) prepare_fras[h]["is_call_option"].append(is_call_option) prepare_fras[h]["settlement_days"].append(settlement_days) prepare_fras[h]["batch_names"].append([name, instrument_type]) return prepare_fras
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