예제 #1
0
        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 FpML.V5r10.Reporting.Identifiers.TradeIdentifier(properties);
            var fraId = ProductIdentifier.UniqueIdentifier;

            //Cache the trade.
            cache.SaveObject(trade, fraId, properties);
            return(fraId);
        }
예제 #2
0
        /// <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 TradeIdentifier(tradeProps);

            TradeIdentifier = tradeIdentifier;
            TradeHeader     = trade.tradeHeader;
            //Get the baseParty, which in GWML is the originating party.
            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;
            //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.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.ReferenceProperty, 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);
                    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);
                    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);
                    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);
                    ProductReporter  = new FutureTransactionReporter();
                }
                break;

                case ProductTypeSimpleEnum.InterestRateSwap:
                {
                    var swap = (Swap)trade.Item;
                    PriceableProduct = new InterestRateSwapPricer(logger, cache, nameSpace, legCalendars, swap, BaseParty, forecastRateInterpolation);
                    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);
                    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);
                    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)
                    {
                        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);
                    ProductReporter  = new CapFloorReporter();
                }
                break;

                case ProductTypeSimpleEnum.FxSpot:
                {
                    var fxForward = (FxSingleLeg)trade.Item;
                    PriceableProduct = new FxSingleLegPricer(fxForward, BaseParty, ProductTypeSimpleEnum.FxSpot);
                    ProductReporter  = new FxSingleLegReporter();
                }
                break;

                case ProductTypeSimpleEnum.FxForward:
                {
                    var fxForward = (FxSingleLeg)trade.Item;
                    PriceableProduct = new FxSingleLegPricer(fxForward, BaseParty, ProductTypeSimpleEnum.FxForward);
                    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);
                        ProductReporter  = new BulletPaymentReporter();
                    }
                }
                break;

                case ProductTypeSimpleEnum.FxSwap:
                {
                    var fxSwap = (FxSwap)trade.Item;
                    PriceableProduct = new FxSwapPricer(fxSwap, BaseParty);
                    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);
                    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);
                    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 compatability.
                    var deposit = (TermDeposit)trade.Item;
                    PriceableProduct = new TermDepositPricer(logger, cache, deposit, TradeProp.Party1);        //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.propertyTransaction:
                {
                    var equity = (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.ReferenceProperty, false);
                    PriceableProduct = new PropertyTransactionPricer(logger, cache, nameSpace, tradeDate, referenceProperty, settlementCalendar, equity, BaseParty, forecastRateInterpolation);
                    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);
                    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);
                    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);
                    ProductReporter  = new FutureTransactionReporter();
                }
                break;

                case ItemChoiceType15.swap:
                {
                    var swap = (Swap)trade.Item;
                    //TODO this needs to be emnhanced
                    ProductType      = ProductTypeSimpleEnum.InterestRateSwap;
                    PriceableProduct = new CrossCurrencySwapPricer(logger, cache, nameSpace, legCalendars, swap, BaseParty, forecastRateInterpolation);
                    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)
                    {
                        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);
                    ProductReporter  = new CapFloorReporter();
                }
                break;

                case ItemChoiceType15.fxSingleLeg:
                {
                    var fxForward = (FxSingleLeg)trade.Item;
                    ProductType      = ProductTypeSimpleEnum.FxSpot;
                    PriceableProduct = new FxSingleLegPricer(fxForward, BaseParty, ProductType);
                    ProductReporter  = new FxSingleLegReporter();
                }
                break;

                case ItemChoiceType15.fxSwap:
                {
                    var fxSwap = (FxSwap)trade.Item;
                    ProductType      = ProductTypeSimpleEnum.FxSwap;
                    PriceableProduct = new FxSwapPricer(fxSwap, BaseParty);
                    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);
                        ProductReporter  = new BulletPaymentReporter();
                    }
                }
                break;

                case ItemChoiceType15.termDeposit:
                {
                    var deposit = (TermDeposit)trade.Item;
                    ProductType      = ProductTypeSimpleEnum.TermDeposit;
                    PriceableProduct = new TermDepositPricer(logger, cache, deposit, TradeProp.Party1);        //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);
                    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);
                    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;
                }
            }
        }