public string CacheFraTradeWithProperties(ILogger logger, ICoreCache cache, FraInputRange fraInputRange, NamedValueSet properties) { var fra = ProductFactory.GetFpMLFra(fraInputRange); var trade = new Trade(); XsdClassesFieldResolver.TradeSetFra(trade, fra); //Get the id. ProductIdentifier = new Identifiers.TradeIdentifier(properties); var fraId = ProductIdentifier.UniqueIdentifier; //Cache the trade. cache.SaveObject(trade, fraId, properties); return(fraId); }
/// <summary> /// /// </summary> /// <param name="logger"></param> /// <param name="cache"></param> /// <param name="nameSpace"></param> /// <param name="legCalendars"></param> /// <param name="trade"></param> /// <param name="tradeProps"></param> /// <param name="forecastRateInterpolation"></param> public TradePricer(ILogger logger, ICoreCache cache, string nameSpace, List <Pair <IBusinessCalendar, IBusinessCalendar> > legCalendars, Trade trade, NamedValueSet tradeProps, bool forecastRateInterpolation) { if (tradeProps == null) { tradeProps = new NamedValueSet();//TODO Need to generate properties for the FpML examples. } var tradeIdentifier = new Identifiers.TradeIdentifier(tradeProps); TradeIdentifier = tradeIdentifier; TradeHeader = trade.tradeHeader; BaseParty = tradeProps.GetValue <string>(TradeProp.BaseParty, false) ?? TradeProp.Party1; var party1 = tradeProps.GetValue <string>(TradeProp.Party1, true); var party2 = tradeProps.GetValue <string>(TradeProp.Party2, true); Parties = new List <Party> { new Party { partyName = new PartyName { Value = party1 } }, new Party { partyName = new PartyName { Value = party2 } } }; TradeType = trade.ItemElementName; //TODO These are for whether the derivative is covered by a collateral agreement or not. //var collateral = trade.collateral; //var documentation = trade.documentation; //Determine the product type, so that the appropriate productPricer can be instantiated. //Set the product type var productType = tradeIdentifier.ProductType; //Check whether the business calendars list is null. Pair <IBusinessCalendar, IBusinessCalendar> firstCalendarPair = null; if (legCalendars?.Count > 0) { firstCalendarPair = legCalendars[0]; } //Instantiate the productPricer. if (productType != null && productType != ProductTypeSimpleEnum.Undefined) { ProductType = (ProductTypeSimpleEnum)productType; switch (ProductType) { //case ProductTypeSimpleEnum.CommodityForward: //{ // IBusinessCalendar fixingCalendar = null; // IBusinessCalendar paymentCalendar = null; // if (firstCalendarPair != null) // { // fixingCalendar = firstCalendarPair.First; // paymentCalendar = firstCalendarPair.Second; // } // var commodityForward = (CommodityForward)trade.Item; // var tradeDate = tradeProps.GetValue<DateTime>(TradeProp.TradeDate, false); // //Get the instrument configuration data. // //Modify the pricer to include this data. // var collateralised = true; // var collateralCurrency = "USD"; // PriceableProduct = new CommodityForwardPricer(logger, cache, collateralised, collateralCurrency, fixingCalendar, paymentCalendar, // commodityForward, nameSpace, BaseParty); // //ProductReporter = new CommodityForwardReporter(); // } // break; case ProductTypeSimpleEnum.LeaseTransaction: { IBusinessCalendar settlementCalendar = null; if (firstCalendarPair != null) { settlementCalendar = firstCalendarPair.First; } var property = (LeaseTransaction)trade.Item; var tradeDate = tradeProps.GetValue <DateTime>(TradeProp.TradeDate, false); var referenceProperty = tradeProps.GetValue <string>(LeaseProp.LeaseIdentifier, false); //Get the instrument configuration data. //Modify the pricer to include this data. //PriceableProduct = new LeaseTransactionPricer(logger, cache, nameSpace, tradeDate, referenceProperty, settlementCalendar, property, BaseParty, forecastRateInterpolation); //ProductReporter = new LeaseTransactionReporter(); } break; case ProductTypeSimpleEnum.PropertyTransaction: { IBusinessCalendar settlementCalendar = null; if (firstCalendarPair != null) { settlementCalendar = firstCalendarPair.First; } var property = (PropertyTransaction)trade.Item; var tradeDate = tradeProps.GetValue <DateTime>(TradeProp.TradeDate, false); var referenceProperty = tradeProps.GetValue <String>(PropertyProp.PropertyIdentifier, false); //Get the instrument configuration data. //Modify the pricer to include this data. PriceableProduct = new PropertyTransactionPricer(logger, cache, nameSpace, tradeDate, referenceProperty, settlementCalendar, property, BaseParty, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new PropertyTransactionReporter(); } break; case ProductTypeSimpleEnum.EquityTransaction: { IBusinessCalendar settlementCalendar = null; if (firstCalendarPair != null) { settlementCalendar = firstCalendarPair.First; } var equity = (EquityTransaction)trade.Item; var tradeDate = tradeProps.GetValue <DateTime>(TradeProp.TradeDate, true); var effectiveDate = tradeProps.GetValue <DateTime>(TradeProp.EffectiveDate, true); var referenceEquity = tradeProps.GetValue <string>(EquityProp.ReferenceEquity, false); //Get the instrument configuration data. //Modify the pricer to include this data. PriceableProduct = new EquityTransactionPricer(logger, cache, nameSpace, tradeDate, effectiveDate, referenceEquity, settlementCalendar, equity, BaseParty, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new EquityTransactionReporter(); } break; case ProductTypeSimpleEnum.BondTransaction: { IBusinessCalendar settlementCalendar = null; if (firstCalendarPair != null) { settlementCalendar = firstCalendarPair.First; } var bond = (BondTransaction)trade.Item; var tradeDate = tradeProps.GetValue <DateTime>(TradeProp.TradeDate, true); var effectiveDate = tradeProps.GetValue <DateTime>(TradeProp.EffectiveDate, true); var bondType = tradeProps.GetValue <string>(BondProp.BondType, false); //Get the instrument configuration data. //Modify the pricer to include this data. PriceableProduct = new BondTransactionPricer(logger, cache, nameSpace, tradeDate, effectiveDate, settlementCalendar, settlementCalendar, bond, BaseParty, bondType, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new BondTransactionReporter(); } break; case ProductTypeSimpleEnum.FutureTransaction: { IBusinessCalendar settlementCalendar = null; if (firstCalendarPair != null) { settlementCalendar = firstCalendarPair.First; } var future = (FutureTransaction)trade.Item; var tradeDate = tradeProps.GetValue <DateTime>(TradeProp.TradeDate, false); var type = tradeProps.GetValue <String>(FuturesProp.FuturesType, true); var futureType = EnumHelper.Parse <ExchangeContractTypeEnum>(type); //Get the instrument configuration data. //Modify the pricer to include this data. PriceableProduct = new FutureTransactionPricer(logger, cache, nameSpace, tradeDate, futureType, settlementCalendar, future, BaseParty, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new FutureTransactionReporter(); } break; case ProductTypeSimpleEnum.InterestRateSwap: { var swap = (Swap)trade.Item; PriceableProduct = new InterestRateSwapPricer(logger, cache, nameSpace, legCalendars, swap, BaseParty, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new InterestRateSwapReporter(); } break; case ProductTypeSimpleEnum.AssetSwap: { var swap = (Swap)trade.Item; //TODO set for the payer. This needs to be modified for the base counterparty. PriceableProduct = new AssetSwapPricer(logger, cache, nameSpace, legCalendars, swap, BaseParty, new Bond(), forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new InterestRateSwapReporter(); } break; case ProductTypeSimpleEnum.CrossCurrencySwap: { var swap = (Swap)trade.Item; //TODO set for the payer. This needs to be modified for the base counterparty. PriceableProduct = new CrossCurrencySwapPricer(logger, cache, nameSpace, legCalendars, swap, BaseParty, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new InterestRateSwapReporter(); } break; case ProductTypeSimpleEnum.FRA: // todo { var fra = (Fra)trade.Item; IBusinessCalendar fixingCalendar = null; IBusinessCalendar paymentCalendar = null; if (firstCalendarPair != null) { fixingCalendar = firstCalendarPair.First; paymentCalendar = firstCalendarPair.Second; } PriceableProduct = new FraPricer(logger, cache, fixingCalendar, paymentCalendar, fra, BaseParty, nameSpace) { ProductIdentifier = tradeIdentifier, ForecastRateInterpolation = forecastRateInterpolation }; ProductReporter = new ForwardRateAgreementReporter(); } break; //case ProductTypeSimpleEnum.InflationSwap: // break; //case ProductTypeSimpleEnum.CreditDefaultSwap: // break; //case ProductTypeSimpleEnum.TotalReturnSwap: // break; //case ProductTypeSimpleEnum.VarianceSwap: // break; case ProductTypeSimpleEnum.CapFloor: { var capFloor = (CapFloor)trade.Item; IBusinessCalendar fixingCalendar = null; IBusinessCalendar paymentCalendar = null; if (firstCalendarPair != null) { fixingCalendar = firstCalendarPair.First; paymentCalendar = firstCalendarPair.Second; } PriceableProduct = new CapFloorPricer(logger, cache, nameSpace, fixingCalendar, paymentCalendar, capFloor, BaseParty) { ProductIdentifier = tradeIdentifier }; ProductReporter = new CapFloorReporter(); } break; case ProductTypeSimpleEnum.FxSpot: { var fxForward = (FxSingleLeg)trade.Item; PriceableProduct = new FxSingleLegPricer(fxForward, BaseParty, ProductTypeSimpleEnum.FxSpot) { ProductIdentifier = tradeIdentifier }; ProductReporter = new FxSingleLegReporter(); } break; case ProductTypeSimpleEnum.FxForward: { var fxForward = (FxSingleLeg)trade.Item; PriceableProduct = new FxSingleLegPricer(fxForward, BaseParty, ProductTypeSimpleEnum.FxForward) { ProductIdentifier = tradeIdentifier }; ProductReporter = new FxSingleLegReporter(); } break; case ProductTypeSimpleEnum.BulletPayment: { if (trade.Item is BulletPayment bullet) { IBusinessCalendar paymentCalendar = null; if (firstCalendarPair != null) { paymentCalendar = firstCalendarPair.Second; } //The calendars if (paymentCalendar == null) { if (bullet.payment.paymentDate != null) { var containsPaymentDateAdjustments = AdjustableOrAdjustedDateHelper.Contains(bullet.payment.paymentDate, ItemsChoiceType.dateAdjustments, out object dateAdjustments); if (containsPaymentDateAdjustments && dateAdjustments != null) { paymentCalendar = BusinessCenterHelper.ToBusinessCalendar(cache, ((BusinessDayAdjustments)dateAdjustments). businessCenters, nameSpace); } } } PriceableProduct = new BulletPaymentPricer(bullet, BaseParty, paymentCalendar) { ProductIdentifier = tradeIdentifier }; ProductReporter = new BulletPaymentReporter(); } } break; case ProductTypeSimpleEnum.FxSwap: { var fxSwap = (FxSwap)trade.Item; PriceableProduct = new FxSwapPricer(fxSwap, BaseParty) { ProductIdentifier = tradeIdentifier }; ProductReporter = new FxSwapReporter(); } break; //case ProductTypeSimpleEnum.EquityOption: // break; //case ProductTypeSimpleEnum.BondOption: // break; case ProductTypeSimpleEnum.FxOption: { IBusinessCalendar fixingCalendar = null; IBusinessCalendar paymentCalendar = null; if (firstCalendarPair != null) { fixingCalendar = firstCalendarPair.First; paymentCalendar = firstCalendarPair.Second; } var fxOption = (FxOption)trade.Item; PriceableProduct = new VanillaEuropeanFxOptionPricer(logger, cache, nameSpace, fixingCalendar, paymentCalendar, fxOption, BaseParty) { ProductIdentifier = tradeIdentifier }; ProductReporter = new FxOptionLegReporter(); } break; //case ProductTypeSimpleEnum.FxOptionStrategy: // break; //case ProductTypeSimpleEnum.CreditDefaultIndex: // break; //case ProductTypeSimpleEnum.CreditDefaultIndexTranche: // break; //case ProductTypeSimpleEnum.CreditDefaultBasket: // break; //case ProductTypeSimpleEnum.CreditDefaultBasketTranche: // break; //case ProductTypeSimpleEnum.CreditDefaultOption: // break; //case ProductTypeSimpleEnum.EquityForward: // break; case ProductTypeSimpleEnum.InterestRateSwaption: { var interestRateSwaption = (Swaption)trade.Item; PriceableProduct = new InterestRateSwaptionPricer(logger, cache, nameSpace, interestRateSwaption, BaseParty, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new InterestRateSwaptionReporter(); } break; case ProductTypeSimpleEnum.TermDeposit: { //var party1 = tradeProps.GetValue<string>(TradeProp.Party1, true); //var party2 = tradeProps.GetValue<string>(TradeProp.Party2, true); //var reportingParty = baseParty == party1 ? "Party1" : "Party2"; // TODO this is for backward compatibility. var deposit = (TermDeposit)trade.Item; PriceableProduct = new TermDepositPricer(logger, cache, deposit, TradeProp.Party1) { ProductIdentifier = tradeIdentifier }; //The payment date must be correct before calling this! ProductReporter = new TermDepositReporter(); } break; //case ProductTypeSimpleEnum.DividendSwap: // break; //case ProductTypeSimpleEnum.ConvertibleBondOption: // break; //case ProductTypeSimpleEnum.Loan: // break; //case ProductTypeSimpleEnum.Repo: // break; default: throw new NotSupportedException("Unsupported ProductType: " + ProductType); } } else { switch (TradeType) { case ItemChoiceType15.leaseTransaction: { var lease = (LeaseTransaction)trade.Item; IBusinessCalendar settlementCalendar = null; if (firstCalendarPair != null) { settlementCalendar = firstCalendarPair.First; } var tradeDate = tradeProps.GetValue <DateTime>(TradeProp.TradeDate, false); var referenceProperty = tradeProps.GetValue <String>(LeaseProp.LeaseIdentifier, false); //PriceableProduct = new LeaseTransactionPricer(logger, cache, nameSpace, tradeDate, referenceProperty, settlementCalendar, lease, BaseParty, forecastRateInterpolation); //ProductReporter = new LeaseTransactionReporter(); } break; case ItemChoiceType15.propertyTransaction: { var property = (PropertyTransaction)trade.Item; IBusinessCalendar settlementCalendar = null; if (firstCalendarPair != null) { settlementCalendar = firstCalendarPair.First; } var tradeDate = tradeProps.GetValue <DateTime>(TradeProp.TradeDate, false); var referenceProperty = tradeProps.GetValue <String>(PropertyProp.PropertyIdentifier, false); PriceableProduct = new PropertyTransactionPricer(logger, cache, nameSpace, tradeDate, referenceProperty, settlementCalendar, property, BaseParty, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new PropertyTransactionReporter(); } break; case ItemChoiceType15.equityTransaction: { var equity = (EquityTransaction)trade.Item; IBusinessCalendar settlementCalendar = null; if (firstCalendarPair != null) { settlementCalendar = firstCalendarPair.First; } var tradeDate = tradeProps.GetValue <DateTime>(TradeProp.TradeDate, false); var effectiveDate = tradeProps.GetValue <DateTime>(TradeProp.EffectiveDate, true); var referenceEquity = tradeProps.GetValue <String>(EquityProp.ReferenceEquity, false); PriceableProduct = new EquityTransactionPricer(logger, cache, nameSpace, tradeDate, effectiveDate, referenceEquity, settlementCalendar, equity, BaseParty, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new EquityTransactionReporter(); } break; case ItemChoiceType15.bondTransaction: { var bond = (BondTransaction)trade.Item; IBusinessCalendar settlementCalendar = null; IBusinessCalendar paymentCalendar = null; if (firstCalendarPair != null) { settlementCalendar = firstCalendarPair.First; paymentCalendar = firstCalendarPair.Second; } var tradeDate = tradeProps.GetValue <DateTime>(TradeProp.TradeDate, true); var effectiveDate = tradeProps.GetValue <DateTime>(TradeProp.EffectiveDate, true); var bondType = tradeProps.GetValue <string>(BondProp.BondType, false); PriceableProduct = new BondTransactionPricer(logger, cache, nameSpace, tradeDate, effectiveDate, settlementCalendar, paymentCalendar, bond, BaseParty, bondType, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new BondTransactionReporter(); } break; case ItemChoiceType15.futureTransaction: { IBusinessCalendar settlementCalendar = null; if (firstCalendarPair != null) { settlementCalendar = firstCalendarPair.First; } var future = (FutureTransaction)trade.Item; var tradeDate = tradeProps.GetValue <DateTime>(TradeProp.TradeDate, false); var type = tradeProps.GetValue <String>(FuturesProp.FuturesType, true); var futureType = EnumHelper.Parse <ExchangeContractTypeEnum>(type); //Get the instrument configuration data. //Modify the pricer to include this data. PriceableProduct = new FutureTransactionPricer(logger, cache, nameSpace, tradeDate, futureType, settlementCalendar, future, BaseParty, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new FutureTransactionReporter(); } break; case ItemChoiceType15.swap: { var swap = (Swap)trade.Item; //TODO this needs to be enhanced ProductType = ProductTypeSimpleEnum.InterestRateSwap; PriceableProduct = new CrossCurrencySwapPricer(logger, cache, nameSpace, legCalendars, swap, BaseParty, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new InterestRateSwapReporter(); //var report = } break; case ItemChoiceType15.fra: // todo { var fra = (Fra)trade.Item; IBusinessCalendar fixingCalendar = null; IBusinessCalendar paymentCalendar = null; if (firstCalendarPair != null) { fixingCalendar = firstCalendarPair.First; paymentCalendar = firstCalendarPair.Second; } ProductType = ProductTypeSimpleEnum.FRA; PriceableProduct = new FraPricer(logger, cache, fixingCalendar, paymentCalendar, fra, BaseParty) { ProductIdentifier = tradeIdentifier, ForecastRateInterpolation = forecastRateInterpolation }; ProductReporter = new ForwardRateAgreementReporter(); } break; case ItemChoiceType15.capFloor: { var capFloor = (CapFloor)trade.Item; IBusinessCalendar fixingCalendar = null; IBusinessCalendar paymentCalendar = null; if (firstCalendarPair != null) { fixingCalendar = firstCalendarPair.First; paymentCalendar = firstCalendarPair.Second; } ProductType = ProductTypeSimpleEnum.CapFloor; PriceableProduct = new CapFloorPricer(logger, cache, nameSpace, fixingCalendar, paymentCalendar, capFloor, BaseParty) { ProductIdentifier = tradeIdentifier }; ProductReporter = new CapFloorReporter(); } break; case ItemChoiceType15.fxSingleLeg: { var fxForward = (FxSingleLeg)trade.Item; ProductType = ProductTypeSimpleEnum.FxSpot; PriceableProduct = new FxSingleLegPricer(fxForward, BaseParty, ProductType) { ProductIdentifier = tradeIdentifier }; ProductReporter = new FxSingleLegReporter(); } break; case ItemChoiceType15.fxSwap: { var fxSwap = (FxSwap)trade.Item; ProductType = ProductTypeSimpleEnum.FxSwap; PriceableProduct = new FxSwapPricer(fxSwap, BaseParty) { ProductIdentifier = tradeIdentifier }; ProductReporter = new FxSwapReporter(); } break; case ItemChoiceType15.bulletPayment: { if (trade.Item is BulletPayment bullet) { IBusinessCalendar paymentCalendar = null; if (firstCalendarPair != null) { paymentCalendar = firstCalendarPair.Second; } //The calendars if (paymentCalendar == null) { if (bullet.payment.paymentDate != null) { var containsPaymentDateAdjustments = AdjustableOrAdjustedDateHelper.Contains(bullet.payment.paymentDate, ItemsChoiceType.dateAdjustments, out object dateAdjustments); if (containsPaymentDateAdjustments && dateAdjustments != null) { paymentCalendar = BusinessCenterHelper.ToBusinessCalendar(cache, ((BusinessDayAdjustments)dateAdjustments). businessCenters, nameSpace); } } } ProductType = ProductTypeSimpleEnum.BulletPayment; PriceableProduct = new BulletPaymentPricer(bullet, BaseParty, paymentCalendar) { ProductIdentifier = tradeIdentifier }; ProductReporter = new BulletPaymentReporter(); } } break; case ItemChoiceType15.termDeposit: { var deposit = (TermDeposit)trade.Item; ProductType = ProductTypeSimpleEnum.TermDeposit; PriceableProduct = new TermDepositPricer(logger, cache, deposit, TradeProp.Party1) { ProductIdentifier = tradeIdentifier }; //The payment date must be correct before calling this! ProductReporter = new TermDepositReporter(); } break; case ItemChoiceType15.swaption: { var interestRateSwaption = (Swaption)trade.Item; ProductType = ProductTypeSimpleEnum.InterestRateSwaption; PriceableProduct = new InterestRateSwaptionPricer(logger, cache, nameSpace, interestRateSwaption, BaseParty, forecastRateInterpolation) { ProductIdentifier = tradeIdentifier }; ProductReporter = new InterestRateSwaptionReporter(); } break; case ItemChoiceType15.fxOption: { IBusinessCalendar fixingCalendar = null; IBusinessCalendar paymentCalendar = null; if (firstCalendarPair != null) { fixingCalendar = firstCalendarPair.First; paymentCalendar = firstCalendarPair.Second; } var fxOption = (FxOption)trade.Item; ProductType = ProductTypeSimpleEnum.FxOption; PriceableProduct = new VanillaEuropeanFxOptionPricer(logger, cache, nameSpace, fixingCalendar, paymentCalendar, fxOption, BaseParty) { ProductIdentifier = tradeIdentifier }; ProductReporter = new FxOptionLegReporter(); } break; default: throw new NotSupportedException("Unsupported TradeType: " + TradeType); } //Adds the extra party info now required. PriceableProduct.OrderedPartyNames.Add(party1); PriceableProduct.OrderedPartyNames.Add(party2); //Check if collateralised if (trade.collateral != null) { PriceableProduct.IsCollateralised = true; } } }