All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cdm.event.common.functions.ResolveSecurityFinanceBillingAmount Maven / Gradle / Ivy

There is a newer version: 6.0.0-dev.89
Show newest version
package cdm.event.common.functions;

import cdm.base.math.FinancialUnitEnum;
import cdm.base.math.NonNegativeQuantitySchedule;
import cdm.base.math.QuantitySchedule;
import cdm.base.math.UnitType;
import cdm.base.math.functions.FilterQuantityByFinancialUnit;
import cdm.base.math.metafields.FieldWithMetaNonNegativeQuantitySchedule;
import cdm.base.math.metafields.ReferenceWithMetaNonNegativeQuantitySchedule;
import cdm.base.staticdata.party.Counterparty;
import cdm.base.staticdata.party.CounterpartyRoleEnum;
import cdm.base.staticdata.party.Party;
import cdm.base.staticdata.party.PayerReceiver;
import cdm.base.staticdata.party.functions.ExtractCounterpartyByRole;
import cdm.base.staticdata.party.metafields.ReferenceWithMetaParty;
import cdm.event.common.CollateralPortfolio;
import cdm.event.common.CollateralPosition;
import cdm.event.common.Reset;
import cdm.event.common.Trade;
import cdm.event.common.TradeState;
import cdm.event.common.Transfer;
import cdm.event.common.Transfer.TransferBuilder;
import cdm.event.common.metafields.ReferenceWithMetaCollateralPortfolio;
import cdm.observable.asset.Price;
import cdm.observable.asset.PriceQuantity;
import cdm.product.asset.FixedRateSpecification;
import cdm.product.asset.FloatingRateSpecification;
import cdm.product.asset.InterestRatePayout;
import cdm.product.asset.RateSpecification;
import cdm.product.asset.functions.FixedAmount;
import cdm.product.asset.functions.FloatingAmount;
import cdm.product.collateral.Collateral;
import cdm.product.collateral.CollateralProvisions;
import cdm.product.collateral.CollateralTreatment;
import cdm.product.collateral.CollateralValuationTreatment;
import cdm.product.collateral.EligibleCollateralCriteria;
import cdm.product.common.schedule.CalculationPeriodData;
import cdm.product.common.schedule.functions.CalculationPeriodRange;
import cdm.product.common.settlement.ResolvablePriceQuantity;
import cdm.product.template.AssetPayout;
import cdm.product.template.EconomicTerms;
import cdm.product.template.NonTransferableProduct;
import cdm.product.template.Payout;
import cdm.product.template.Product;
import cdm.product.template.TradeLot;
import cdm.product.template.util.ProductDeepPathUtil;
import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.expression.CardinalityOperator;
import com.rosetta.model.lib.expression.MapperMaths;
import com.rosetta.model.lib.functions.ModelObjectValidator;
import com.rosetta.model.lib.functions.RosettaFunction;
import com.rosetta.model.lib.mapper.MapperC;
import com.rosetta.model.lib.mapper.MapperS;
import com.rosetta.model.lib.records.Date;
import com.rosetta.model.metafields.FieldWithMetaString;
import java.math.BigDecimal;
import java.util.Optional;
import javax.inject.Inject;

import static com.rosetta.model.lib.expression.ExpressionOperators.*;

@ImplementedBy(ResolveSecurityFinanceBillingAmount.ResolveSecurityFinanceBillingAmountDefault.class)
public abstract class ResolveSecurityFinanceBillingAmount implements RosettaFunction {
	
	@Inject protected ModelObjectValidator objectValidator;
	
	// RosettaFunction dependencies
	//
	@Inject protected CalculationPeriodRange calculationPeriodRange0;
	@Inject protected ExtractCounterpartyByRole extractCounterpartyByRole;
	@Inject protected FilterQuantityByFinancialUnit filterQuantityByFinancialUnit;
	@Inject protected FixedAmount fixedAmount;
	@Inject protected FloatingAmount floatingAmount;
	@Inject protected ProductDeepPathUtil productDeepPathUtil;

	/**
	* @param tradeState 
	* @param reset 
	* @param recordStartDate 
	* @param recordEndDate 
	* @param transferDate 
	* @return transfer 
	*/
	public Transfer evaluate(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
		Transfer.TransferBuilder transferBuilder = doEvaluate(tradeState, reset, recordStartDate, recordEndDate, transferDate);
		
		final Transfer transfer;
		if (transferBuilder == null) {
			transfer = null;
		} else {
			transfer = transferBuilder.build();
			objectValidator.validate(Transfer.class, transfer);
		}
		
		return transfer;
	}

	protected abstract Transfer.TransferBuilder doEvaluate(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperC securityQuantity(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperS interestRatePayout(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperS assetPayout(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperS collateral(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperS haircutPercentage(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperS valuationPercentage(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperS marginRatio(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperS billingQuantity(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperS calculationPeriodRange1(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperS performance(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperS payerPartyReference(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	protected abstract MapperS receiverPartyReference(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate);

	public static class ResolveSecurityFinanceBillingAmountDefault extends ResolveSecurityFinanceBillingAmount {
		@Override
		protected Transfer.TransferBuilder doEvaluate(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			Transfer.TransferBuilder transfer = Transfer.builder();
			return assignOutput(transfer, tradeState, reset, recordStartDate, recordEndDate, transferDate);
		}
		
		protected Transfer.TransferBuilder assignOutput(Transfer.TransferBuilder transfer, TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			transfer
				.getOrCreateQuantity()
				.setValue(performance(tradeState, reset, recordStartDate, recordEndDate, transferDate).get());
			
			final FieldWithMetaString fieldWithMetaString = interestRatePayout(tradeState, reset, recordStartDate, recordEndDate, transferDate).map("getPriceQuantity", _interestRatePayout -> _interestRatePayout.getPriceQuantity()).map("getQuantitySchedule", resolvablePriceQuantity -> resolvablePriceQuantity.getQuantitySchedule()).map("Type coercion", referenceWithMetaNonNegativeQuantitySchedule -> referenceWithMetaNonNegativeQuantitySchedule == null ? null : referenceWithMetaNonNegativeQuantitySchedule.getValue()).map("getUnit", nonNegativeQuantitySchedule -> nonNegativeQuantitySchedule.getUnit()).map("getCurrency", unitType -> unitType.getCurrency()).get();
			transfer
				.getOrCreateQuantity()
				.getOrCreateUnit()
				.setCurrencyValue((fieldWithMetaString == null ? null : fieldWithMetaString.getValue()));
			
			final Party ifThenElseResult0;
			if (greaterThanEquals(performance(tradeState, reset, recordStartDate, recordEndDate, transferDate), MapperS.of(BigDecimal.valueOf(0)), CardinalityOperator.All).getOrDefault(false)) {
				final ReferenceWithMetaParty referenceWithMetaParty0 = payerPartyReference(tradeState, reset, recordStartDate, recordEndDate, transferDate).get();
				ifThenElseResult0 = referenceWithMetaParty0 == null ? null : referenceWithMetaParty0.getValue();
			} else {
				final ReferenceWithMetaParty referenceWithMetaParty1 = receiverPartyReference(tradeState, reset, recordStartDate, recordEndDate, transferDate).get();
				ifThenElseResult0 = referenceWithMetaParty1 == null ? null : referenceWithMetaParty1.getValue();
			}
			transfer
				.getOrCreatePayerReceiver()
				.setPayerPartyReferenceValue(ifThenElseResult0);
			
			final Party ifThenElseResult1;
			if (greaterThanEquals(performance(tradeState, reset, recordStartDate, recordEndDate, transferDate), MapperS.of(BigDecimal.valueOf(0)), CardinalityOperator.All).getOrDefault(false)) {
				final ReferenceWithMetaParty referenceWithMetaParty2 = receiverPartyReference(tradeState, reset, recordStartDate, recordEndDate, transferDate).get();
				ifThenElseResult1 = referenceWithMetaParty2 == null ? null : referenceWithMetaParty2.getValue();
			} else {
				final ReferenceWithMetaParty referenceWithMetaParty3 = payerPartyReference(tradeState, reset, recordStartDate, recordEndDate, transferDate).get();
				ifThenElseResult1 = referenceWithMetaParty3 == null ? null : referenceWithMetaParty3.getValue();
			}
			transfer
				.getOrCreatePayerReceiver()
				.setReceiverPartyReferenceValue(ifThenElseResult1);
			
			transfer
				.getOrCreateSettlementDate()
				.setAdjustedDateValue(transferDate);
			
			return Optional.ofNullable(transfer)
				.map(o -> o.prune())
				.orElse(null);
		}
		
		@Override
		protected MapperC securityQuantity(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			return MapperC.of(filterQuantityByFinancialUnit.evaluate(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).mapC("getTradeLot", trade -> trade.getTradeLot()).mapC("getPriceQuantity", tradeLot -> tradeLot.getPriceQuantity()).mapC("getQuantity", priceQuantity -> priceQuantity.getQuantity()).map("Type coercion", fieldWithMetaNonNegativeQuantitySchedule -> fieldWithMetaNonNegativeQuantitySchedule.getValue()).getMulti(), FinancialUnitEnum.SHARE));
		}
		
		@Override
		protected MapperS interestRatePayout(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			return MapperS.of(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).map("getProduct", trade -> trade.getProduct()).map("getEconomicTerms", nonTransferableProduct -> nonTransferableProduct.getEconomicTerms()).mapC("getPayout", economicTerms -> economicTerms.getPayout()).map("getInterestRatePayout", payout -> payout.getInterestRatePayout()).get());
		}
		
		@Override
		protected MapperS assetPayout(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			return MapperS.of(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).map("getProduct", trade -> trade.getProduct()).map("getEconomicTerms", nonTransferableProduct -> nonTransferableProduct.getEconomicTerms()).map("getCollateral", economicTerms -> economicTerms.getCollateral()).mapC("getCollateralPortfolio", _collateral -> _collateral.getCollateralPortfolio()).map("Type coercion", referenceWithMetaCollateralPortfolio -> referenceWithMetaCollateralPortfolio.getValue()).mapC("getCollateralPosition", collateralPortfolio -> collateralPortfolio.getCollateralPosition()).map("getProduct", collateralPosition -> collateralPosition.getProduct()).map("chooseEconomicTerms", product -> productDeepPathUtil.chooseEconomicTerms(product)).mapC("getPayout", economicTerms -> economicTerms.getPayout()).map("getAssetPayout", payout -> payout.getAssetPayout()).get());
		}
		
		@Override
		protected MapperS collateral(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			return MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).map("getProduct", trade -> trade.getProduct()).map("getEconomicTerms", nonTransferableProduct -> nonTransferableProduct.getEconomicTerms()).map("getCollateral", economicTerms -> economicTerms.getCollateral());
		}
		
		@Override
		protected MapperS haircutPercentage(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			return MapperMaths.subtract(MapperS.of(new BigDecimal("1.0")), MapperS.of(collateral(tradeState, reset, recordStartDate, recordEndDate, transferDate).map("getCollateralProvisions", _collateral -> _collateral.getCollateralProvisions()).mapC("getEligibleCollateral", collateralProvisions -> collateralProvisions.getEligibleCollateral()).get()).map("getTreatment", eligibleCollateralCriteria -> eligibleCollateralCriteria.getTreatment()).map("getValuationTreatment", collateralTreatment -> collateralTreatment.getValuationTreatment()).map("getHaircutPercentage", collateralValuationTreatment -> collateralValuationTreatment.getHaircutPercentage()));
		}
		
		@Override
		protected MapperS valuationPercentage(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			return MapperMaths.divide(MapperS.of(BigDecimal.valueOf(1)), haircutPercentage(tradeState, reset, recordStartDate, recordEndDate, transferDate));
		}
		
		@Override
		protected MapperS marginRatio(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			if (exists(MapperS.of(collateral(tradeState, reset, recordStartDate, recordEndDate, transferDate).map("getCollateralProvisions", _collateral -> _collateral.getCollateralProvisions()).mapC("getEligibleCollateral", collateralProvisions -> collateralProvisions.getEligibleCollateral()).get()).map("getTreatment", eligibleCollateralCriteria -> eligibleCollateralCriteria.getTreatment()).map("getValuationTreatment", collateralTreatment -> collateralTreatment.getValuationTreatment()).map("getHaircutPercentage", collateralValuationTreatment -> collateralValuationTreatment.getHaircutPercentage())).getOrDefault(false)) {
				return valuationPercentage(tradeState, reset, recordStartDate, recordEndDate, transferDate);
			}
			if (exists(MapperS.of(collateral(tradeState, reset, recordStartDate, recordEndDate, transferDate).map("getCollateralProvisions", _collateral -> _collateral.getCollateralProvisions()).mapC("getEligibleCollateral", collateralProvisions -> collateralProvisions.getEligibleCollateral()).get()).map("getTreatment", eligibleCollateralCriteria -> eligibleCollateralCriteria.getTreatment()).map("getValuationTreatment", collateralTreatment -> collateralTreatment.getValuationTreatment()).map("getMarginPercentage", collateralValuationTreatment -> collateralValuationTreatment.getMarginPercentage())).getOrDefault(false)) {
				return MapperS.of(collateral(tradeState, reset, recordStartDate, recordEndDate, transferDate).map("getCollateralProvisions", _collateral -> _collateral.getCollateralProvisions()).mapC("getEligibleCollateral", collateralProvisions -> collateralProvisions.getEligibleCollateral()).get()).map("getTreatment", eligibleCollateralCriteria -> eligibleCollateralCriteria.getTreatment()).map("getValuationTreatment", collateralTreatment -> collateralTreatment.getValuationTreatment()).map("getMarginPercentage", collateralValuationTreatment -> collateralValuationTreatment.getMarginPercentage());
			}
			return MapperS.of(new BigDecimal("1.0"));
		}
		
		@Override
		protected MapperS billingQuantity(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			return MapperMaths.multiply(MapperMaths.multiply(MapperS.of(reset).map("getResetValue", _reset -> _reset.getResetValue()).map("getValue", price -> price.getValue()), securityQuantity(tradeState, reset, recordStartDate, recordEndDate, transferDate).map("getValue", quantitySchedule -> quantitySchedule.getValue())), marginRatio(tradeState, reset, recordStartDate, recordEndDate, transferDate));
		}
		
		@Override
		protected MapperS calculationPeriodRange1(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			return MapperS.of(calculationPeriodRange0.evaluate(recordStartDate, recordEndDate, null));
		}
		
		@Override
		protected MapperS performance(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			if (exists(interestRatePayout(tradeState, reset, recordStartDate, recordEndDate, transferDate).map("getRateSpecification", _interestRatePayout -> _interestRatePayout.getRateSpecification()).map("getFixedRateSpecification", rateSpecification -> rateSpecification.getFixedRateSpecification())).getOrDefault(false)) {
				return MapperS.of(fixedAmount.evaluate(interestRatePayout(tradeState, reset, recordStartDate, recordEndDate, transferDate).get(), billingQuantity(tradeState, reset, recordStartDate, recordEndDate, transferDate).get(), recordEndDate, calculationPeriodRange1(tradeState, reset, recordStartDate, recordEndDate, transferDate).get()));
			}
			if (exists(interestRatePayout(tradeState, reset, recordStartDate, recordEndDate, transferDate).map("getRateSpecification", _interestRatePayout -> _interestRatePayout.getRateSpecification()).map("getFloatingRateSpecification", rateSpecification -> rateSpecification.getFloatingRateSpecification())).getOrDefault(false)) {
				return MapperS.of(floatingAmount.evaluate(interestRatePayout(tradeState, reset, recordStartDate, recordEndDate, transferDate).get(), MapperS.of(reset).map("getResetValue", _reset -> _reset.getResetValue()).map("getValue", price -> price.getValue()).get(), billingQuantity(tradeState, reset, recordStartDate, recordEndDate, transferDate).get(), recordEndDate, calculationPeriodRange1(tradeState, reset, recordStartDate, recordEndDate, transferDate).get()));
			}
			return MapperS.ofNull();
		}
		
		@Override
		protected MapperS payerPartyReference(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			return MapperS.of(extractCounterpartyByRole.evaluate(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).mapC("getCounterparty", trade -> trade.getCounterparty()).getMulti(), interestRatePayout(tradeState, reset, recordStartDate, recordEndDate, transferDate).map("getPayerReceiver", _interestRatePayout -> _interestRatePayout.getPayerReceiver()).map("getPayer", payerReceiver -> payerReceiver.getPayer()).get())).map("getPartyReference", counterparty -> counterparty.getPartyReference());
		}
		
		@Override
		protected MapperS receiverPartyReference(TradeState tradeState, Reset reset, Date recordStartDate, Date recordEndDate, Date transferDate) {
			return MapperS.of(extractCounterpartyByRole.evaluate(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).mapC("getCounterparty", trade -> trade.getCounterparty()).getMulti(), interestRatePayout(tradeState, reset, recordStartDate, recordEndDate, transferDate).map("getPayerReceiver", _interestRatePayout -> _interestRatePayout.getPayerReceiver()).map("getReceiver", payerReceiver -> payerReceiver.getReceiver()).get())).map("getPartyReference", counterparty -> counterparty.getPartyReference());
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy