Beispiel #1
0
        public T GetData <T>(string key) where T : GuidObject
        {
            DependentObject dobj = null;

            //_dispatcher.Invoke(new Action(() =>
            //{
            //	try
            //	{
            //		_doManager.GetObject(key, out dobj);
            //	}
            //	catch (Exception ex)
            //	{
            //		Logger.Error(string.Format("Error when building dependency tree {0}", ex.GetDetail()));
            //	}
            //}), DataGetPriority);

            try
            {
                _doManager.GetObject(key, out dobj);
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("Error when building dependency tree {0}", ex.GetDetail()));
            }

            if (dobj == null)
            {
                throw new PricingBaseException(string.Format("Cannot find market object {0}", key));
            }

            return(dobj.Value as T);
        }
Beispiel #2
0
        public void Save()
        {
            DependentObject savedObject = new DependentObject {
                SifObjectName = "StudentAttendance", ObjectKeyValue = "key3"
            };

            (new DependentObjectDao()).Save(savedObject);
            if (log.IsDebugEnabled)
            {
                log.Debug("Saved object: " + savedObject);
            }
            Console.WriteLine("Saved object: " + savedObject);

            using (ISession session = sessionFactory.OpenSession())
            {
                DependentObject comparisonObject = session.Get <DependentObject>(savedObject.Id);
                if (log.IsDebugEnabled)
                {
                    log.Debug("Comparison object: " + comparisonObject);
                }
                Console.WriteLine("Comparison object: " + comparisonObject);
                Assert.IsNotNull(comparisonObject);
                Assert.AreNotSame(savedObject, comparisonObject);
                Assert.AreEqual(savedObject.SifObjectName, comparisonObject.SifObjectName);
                Assert.AreEqual(savedObject.ObjectKeyValue, comparisonObject.ObjectKeyValue);
            }
        }
Beispiel #3
0
        public void RetrieveFail()
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                DependentObject addedObject = new DependentObject {
                    SifObjectName = "StudentPersonal", ObjectKeyValue = "st1", ApplicationId = "app1", ZoneId = "zone1"
                };
                session.Save(addedObject);
            }

            DependentObject retrievedObject = (new DependentObjectDao()).Retrieve("StudentPersonal", "st1", "app1", "zone1");
        }
Beispiel #4
0
        public void Retrieve()
        {
            DependentObject retrievedObject = (new DependentObjectDao()).Retrieve("StudentPersonal", "st1", "app1", "zone1");

            if (log.IsDebugEnabled)
            {
                log.Debug("Retrieved object: " + retrievedObject);
            }
            Console.WriteLine("Retrieved object: " + retrievedObject);
            Assert.AreEqual("StudentPersonal", retrievedObject.SifObjectName);
            Assert.AreEqual("st1", retrievedObject.ObjectKeyValue);
            Assert.AreEqual("app1", retrievedObject.ApplicationId);
            Assert.AreEqual("zone1", retrievedObject.ZoneId);
        }
        public void RetrieveDependentObject()
        {
            DependentObject retrievedObject = (new DependentObjectCacheService()).RetrieveDependentObject("StudentPersonal", "@RefId=7C834EA9EDA12090347F83297E1C290C", "SbpDemo", "SIFDemo");

            if (log.IsDebugEnabled)
            {
                log.Debug("Retrieved object: " + retrievedObject);
            }
            Console.WriteLine("Retrieved object: " + retrievedObject);
            Assert.AreEqual("StudentPersonal", retrievedObject.SifObjectName);
            Assert.AreEqual("@RefId=7C834EA9EDA12090347F83297E1C290C", retrievedObject.ObjectKeyValue);
            Assert.AreEqual("SbpDemo", retrievedObject.ApplicationId);
            Assert.AreEqual("SIFDemo", retrievedObject.ZoneId);
        }
Beispiel #6
0
        public void Delete()
        {
            DependentObject deletedObject = dependentObjects[0];

            (new DependentObjectDao()).Delete(deletedObject);
            if (log.IsDebugEnabled)
            {
                log.Debug("Deleted object: " + deletedObject);
            }
            Console.WriteLine("Deleted object: " + deletedObject);

            using (ISession session = sessionFactory.OpenSession())
            {
                ICollection <DependentObject> objs = session.CreateCriteria(typeof(DependentObject)).List <DependentObject>();
                Assert.AreEqual(dependentObjects.Length - 1, objs.Count);
            }
        }
        /// <see cref="Systemic.Sif.Sbp.Framework.Service.IDependentObjectCacheService.MarkAsRequested(DependentObject, string, string)">MarkAsRequested</see>
        public void MarkAsRequested(DependentObject dependentObject, string agentId, string zoneId)
        {
            if (dependentObject == null)
            {
                throw new ArgumentNullException("cachedObject");
            }

            if (String.IsNullOrEmpty(agentId) || String.IsNullOrEmpty(zoneId))
            {
                throw new ArgumentException("agentId and/or zoneId parameter is null or empty.");
            }

            dependentObject.AgentId       = agentId;
            dependentObject.ZoneId        = zoneId;
            dependentObject.RequestedDate = DateTime.Now;
            dependentObject.Requested     = true;
            dependentObjectDao.Save(dependentObject);
        }
Beispiel #8
0
        /// <summary>
        /// This method will check whether the specified sifDataObject exists as a dependent object in the cache. If
        /// it does, it is no longer required and can be removed.
        /// </summary>
        /// <param name="sifDataObject">SIF Data Object to check against the cache.</param>
        /// <param name="zone">Zone the SIF Data Object was received from.</param>
        /// <returns>This method will always return true.</returns>
        /// <exception cref="System.ArgumentException">sifDataObject or zone parameter is null.</exception>
        private bool PreProcessSifDataObject(T sifDataObject, IZone zone)
        {
            if (sifDataObject == null)
            {
                throw new ArgumentNullException("sifDataObject");
            }

            if (zone == null)
            {
                throw new ArgumentNullException("zone");
            }

            SifDataObjectMetadata <T> metadata = MetadataInstance(sifDataObject);

            if (log.IsDebugEnabled)
            {
                log.Debug(this.GetType().Name + " preprocessing " + metadata.ObjectName + " (" + metadata.SifUniqueId + ") for application " + ApplicationId + " in zone " + zone.ZoneId + ".");
            }
            DependentObject dependentObject = cacheService.RetrieveDependentObject(metadata.ObjectName, metadata.SifUniqueId, ApplicationId, zone.ZoneId);

            // If this object exists as a dependent object in the cache, it is now no longer required and can be
            // removed.
            if (dependentObject != null)
            {
                if (log.IsInfoEnabled)
                {
                    log.Info(metadata.ObjectName + " (" + metadata.SifUniqueId + ") removed from the cache as it has now been received.");
                }
                cacheService.DeleteDependentObject(dependentObject);
            }
            else
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug(metadata.ObjectName + " (" + metadata.SifUniqueId + ") did not exist in the cache.");
                }
            }

            return(true);
        }
Beispiel #9
0
        public void RetrieveById()
        {
            DependentObject retrievedObject = (new DependentObjectDao()).RetrieveById((long)1);

            if (log.IsDebugEnabled)
            {
                log.Debug("Retrieved object: " + retrievedObject);
            }
            Console.WriteLine("Retrieved object: " + retrievedObject);

            using (ISession session = sessionFactory.OpenSession())
            {
                DependentObject comparisonObject = session.Get <DependentObject>((long)1);
                if (log.IsDebugEnabled)
                {
                    log.Debug("Comparison object: " + comparisonObject);
                }
                Console.WriteLine("Comparison object: " + comparisonObject);
                Assert.IsNotNull(comparisonObject);
                Assert.AreNotSame(retrievedObject, comparisonObject);
                Assert.AreEqual(retrievedObject.SifObjectName, comparisonObject.SifObjectName);
                Assert.AreEqual(retrievedObject.ObjectKeyValue, comparisonObject.ObjectKeyValue);
            }
        }
Beispiel #10
0
        public void RetrieveNull()
        {
            DependentObject retrievedObject = (new DependentObjectDao()).Retrieve("FakeSifObject", "key0", "app0", "zone0");

            Assert.IsNull(retrievedObject);
        }
 /// <see cref="Systemic.Sif.Sbp.Framework.Service.IDependentObjectCacheService.DeleteDependentObject(DependentObject)">DeleteDependentObject</see>
 public void DeleteDependentObject(DependentObject dependentObject)
 {
     dependentObjectDao.Delete(dependentObject);
 }
        private void CreateInitialData()
        {
            DependentObject dependentObject1 = new DependentObject {
                SifObjectName = "StudentPersonal", ObjectKeyValue = "st1"
            };
            DependentObject dependentObject2 = new DependentObject {
                SifObjectName = "StudentPersonal", ObjectKeyValue = "st2"
            };

            cachedObjects = new[]
            {
                new CachedObject
                {
                    SifObjectName = "TeachingGroup", ObjectKeyValue = "tg1", ApplicationId = "app1", ZoneId = "zone1", DependentObjects = new Collection <DependentObject>
                    {
                        dependentObject1,
                        dependentObject2
                    }
                },
                new CachedObject
                {
                    SifObjectName = "StudentSchoolEnrolment", ObjectKeyValue = "se1", DependentObjects = new Collection <DependentObject>
                    {
                        dependentObject1
                    }
                },
                new CachedObject
                {
                    SifObjectName = "StudentPersonal", ObjectKeyValue = "sp1", AgentId = "agent", ApplicationId = "app", ExpiryDate = DateTime.Now.Subtract(TimeSpan.FromDays(1)), DependentObjects = new Collection <DependentObject>
                    {
                        new DependentObject {
                            SifObjectName = "DummyObject1", ObjectKeyValue = "do1"
                        }
                    }
                },
                new CachedObject
                {
                    SifObjectName = "StudentPersonal", ObjectKeyValue = "sp2", AgentId = "agent", ApplicationId = "app", ExpiryDate = DateTime.Now.AddDays(1), DependentObjects = new Collection <DependentObject>
                    {
                        new DependentObject {
                            SifObjectName = "DummyObject2", ObjectKeyValue = "do2"
                        }
                    }
                },
                new CachedObject {
                    SifObjectName = "StudentPersonal", ObjectKeyValue = "sp3", AgentId = "agent", ApplicationId = "app"
                }
            };

            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    foreach (CachedObject cachedObject in cachedObjects)
                    {
                        session.Save(cachedObject);
                    }

                    transaction.Commit();
                }
            }
        }
Beispiel #13
0
 public ParentObject(DependentObject dependent)
 {
     _dependent = dependent;
 }
Beispiel #14
0
        private GuidObject CurveBuiltFromDependents(DependentObject fatherNode, DependentObject[] childrenNodes)
        {
            IMarketCondition baseMarket;


            if (childrenNodes.Any(x => x is InstrumentCurveObject))
            {
                var baseCurve = (CurveData)childrenNodes.First().Value;
                baseMarket = new MarketCondition(x => x.ValuationDate.Value = Market.ReferenceDate,
                                                 x => x.HistoricalIndexRates.Value = Market.HistoricalIndexRates,
                                                 x => x.DiscountCurve.Value        = baseCurve.YieldCurve,
                                                 x => x.FixingCurve.Value          = baseCurve.YieldCurve,
                                                 x => x.FgnDiscountCurve.Value     = baseCurve.YieldCurve);
            }
            else
            {
                baseMarket = new MarketCondition(x => x.ValuationDate.Value = Market.ReferenceDate,
                                                 x => x.HistoricalIndexRates.Value = Market.HistoricalIndexRates);
            }

            var rateDefinitions  = childrenNodes.Where(x => x.Value is RateMktData).Select(x => (RateMktData)x.Value).ToArray();
            var fxSpotDefinition = rateDefinitions.Where(x => x.InstrumentType.ToInstrumentType() == InstrumentType.FxSpot).ToArray();

            if (fxSpotDefinition.Count() > 1)
            {
                throw new PricingBaseException("A yield curve cannot have more than 2 fx rates against its base curve's currency");
            }
            if (fxSpotDefinition.Any())
            {
                baseMarket = baseMarket.UpdateCondition(
                    new UpdateMktConditionPack <FxSpot[]>(x => x.FxSpots, new[] { CreateFxSpot(fxSpotDefinition[0]) }));
            }

            var curveConvention = childrenNodes.Last().Value as CurveConvention;

            if (curveConvention == null)
            {
                Logger.ErrorFormat("Curve conventions are missing!");
                return(null);
            }

            var isRegridded = rateDefinitions.All(x => x.IndexType != null && x.IndexType.ToIndexType() == IndexType.Regridded);

            if (isRegridded)
            {
                var yieldCurve          = baseMarket.DiscountCurve.Value;
                var tempRateDefinitions = new List <RateMktData>();
                foreach (var tenor in Definition.RegriddedTenors)
                {
                    var date = new Term(tenor).Next(yieldCurve.ReferenceDate);
                    if (date > yieldCurve.KeyPoints.Last().Item1)
                    {
                        date = yieldCurve.KeyPoints.Last().Item1;
                    }
                    tempRateDefinitions.Add(new RateMktData(date.ToString(), GetParRate(date, yieldCurve), "SwapParRate", "InterestRateSwap", Definition.Name));
                }
                rateDefinitions = tempRateDefinitions.ToArray();
            }

            YieldCurve instrumentCurve;

            //This branch is for bond pnl calculation
            if (rateDefinitions.All(
                    x => x.InstrumentType.ToInstrumentType() == InstrumentType.Dummy || x.InstrumentType.ToInstrumentType() == InstrumentType.None))
            {
                if (rateDefinitions.All(x => x.IsTerm()))
                {
                    instrumentCurve = new YieldCurve(
                        Definition.Name,
                        Market.ReferenceDate,
                        rateDefinitions.Select(x => Tuple.Create((ITerm) new Term(x.Tenor), x.Rate)).ToArray(),
                        curveConvention.BusinessDayConvention.ToBda(),
                        curveConvention.DayCount.ToDayCountImpl(),
                        curveConvention.Calendar.ToCalendarImpl(),
                        curveConvention.Currency.ToCurrencyCode(),
                        curveConvention.Compound.ToCompound(),
                        curveConvention.Interpolation.ToInterpolation(),
                        Definition.Trait.ToYieldCurveTrait(),
                        baseMarket
                        );
                }
                else
                {
                    instrumentCurve = new YieldCurve(
                        Definition.Name,
                        Market.ReferenceDate,
                        rateDefinitions.Select(x => Tuple.Create(new Date(DateTime.Parse(x.Tenor)), x.Rate)).ToArray(),
                        curveConvention.BusinessDayConvention.ToBda(),
                        curveConvention.DayCount.ToDayCountImpl(),
                        curveConvention.Calendar.ToCalendarImpl(),
                        curveConvention.Currency.ToCurrencyCode(),
                        curveConvention.Compound.ToCompound(),
                        curveConvention.Interpolation.ToInterpolation(),
                        Definition.Trait.ToYieldCurveTrait(),
                        baseMarket
                        );
                }
            }
            else
            {
                var mktInstruments = new List <MarketInstrument>();
                foreach (var rateDefinition in rateDefinitions)
                {
                    MktInstrumentCalibMethod calibrationMethod;
                    var instType = rateDefinition.InstrumentType.ToInstrumentType();

                    switch (instType)
                    {
                    case InstrumentType.InterestRateSwap:
                        var swap = CreateIrsInstrument(rateDefinition, out calibrationMethod);
                        mktInstruments.Add(new MarketInstrument(swap, rateDefinition.Rate, calibrationMethod));
                        break;

                    case InstrumentType.Deposit:
                    case InstrumentType.Repo:
                    case InstrumentType.Ibor:
                        var deposit = CreateDepositInstrument(rateDefinition, out calibrationMethod);
                        mktInstruments.Add(new MarketInstrument(deposit, rateDefinition.Rate, calibrationMethod));
                        break;

                    case InstrumentType.Dummy:
                    case InstrumentType.None:
                        var dummy = CreateDummyInstrument(rateDefinition);
                        mktInstruments.Add(new MarketInstrument(dummy, rateDefinition.Rate, MktInstrumentCalibMethod.Default));
                        break;

                    case InstrumentType.CreditDefaultSwap:
                        var cds = CreateCreditDefaultSwap(rateDefinition);
                        mktInstruments.Add(new MarketInstrument(cds, rateDefinition.Rate, MktInstrumentCalibMethod.Default));
                        break;

                    case InstrumentType.CommodityForward:
                        mktInstruments.Add(new MarketInstrument(CreateCommodityForward(rateDefinition), rateDefinition.Rate, MktInstrumentCalibMethod.Default));
                        break;

                    case InstrumentType.CommoditySpot:
                        //baseMarket = baseMarket.UpdateCondition(new UpdateMktConditionPack<double>(x => x.SpotPrices, rateDefinition.Rate));
                        baseMarket = baseMarket.UpdateCondition(new UpdateMktConditionPack <Dictionary <string, double> >(x => x.SpotPrices, new Dictionary <string, double> {
                            { "", rateDefinition.Rate }
                        }));
                        break;

                    case InstrumentType.FxSpot:
                        break;

                    default:
                        throw new PricingLibraryException("Unrecognized product type in calibrating curve.");
                    }
                    #region legacy code

                    //               if (rateDefinition.InstrumentType.ToInstrumentType() == InstrumentType.InterestRateSwap)
                    //{
                    //	var swap = CreateIrsInstrument(rateDefinition, out calibrationMethod);
                    //	mktInstruments.Add(new MarketInstrument(swap, rateDefinition.Rate, calibrationMethod));
                    //}
                    //else if ( instType == InstrumentType.Deposit
                    //	|| rateDefinition.InstrumentType.ToInstrumentType() == InstrumentType.Repo
                    //	|| rateDefinition.InstrumentType.ToInstrumentType() == InstrumentType.Ibor)
                    //{
                    //	var deposit = CreateDepositInstrument(rateDefinition, out calibrationMethod);
                    //	mktInstruments.Add(new MarketInstrument(deposit, rateDefinition.Rate, calibrationMethod));
                    //}
                    //else if (rateDefinition.InstrumentType.ToInstrumentType() == InstrumentType.Dummy ||
                    //				 rateDefinition.InstrumentType.ToInstrumentType() == InstrumentType.None)
                    //{
                    //	var dummy = CreateDummyInstrument(rateDefinition);
                    //	mktInstruments.Add(new MarketInstrument(dummy, rateDefinition.Rate, MktInstrumentCalibMethod.Default));
                    //}
                    //else if (rateDefinition.InstrumentType.ToInstrumentType() == InstrumentType.CreditDefaultSwap)
                    //{
                    //	var cds = CreateCreditDefaultSwap(rateDefinition);
                    //	mktInstruments.Add(new MarketInstrument(cds, rateDefinition.Rate, MktInstrumentCalibMethod.Default));
                    //}
                    //else if (rateDefinition.InstrumentType.ToInstrumentType() == InstrumentType.CommodityForward)
                    //{
                    //	mktInstruments.Add(new MarketInstrument(CreateCommodityForward(rateDefinition), rateDefinition.Rate, MktInstrumentCalibMethod.Default));
                    //}
                    //else if (rateDefinition.InstrumentType.ToInstrumentType() == InstrumentType.CommoditySpot)
                    //{
                    //                   //baseMarket = baseMarket.UpdateCondition(new UpdateMktConditionPack<double>(x => x.SpotPrices, rateDefinition.Rate));
                    //                   baseMarket = baseMarket.UpdateCondition(new UpdateMktConditionPack<Dictionary<string, double>>(x => x.SpotPrices, new Dictionary<string, double> { {"", rateDefinition.Rate } }));
                    //               }
                    //else if (rateDefinition.InstrumentType.ToInstrumentType() == InstrumentType.FxSpot)
                    //{

                    //}
                    //else
                    //{
                    //	throw new PricingLibraryException("Unrecognized product type in calibrating curve.");
                    //}
                    #endregion legacy code
                }

                var isSpcCurve        = rateDefinitions.All(x => x.IndexType != null && x.IndexType.ToIndexType() == IndexType.Spc);
                var isConvenicenYield = rateDefinitions.All(x => x.IndexType != null && x.IndexType.ToIndexType() == IndexType.ConvenienceYield);


                Expression <Func <IMarketCondition, object> >[] expression = null;
                if (isSpcCurve)
                {
                    expression = new Expression <Func <IMarketCondition, object> >[] { x => x.SurvivalProbabilityCurve }
                }
                ;
                if (isConvenicenYield)
                {
                    expression = new Expression <Func <IMarketCondition, object> >[] { x => x.DividendCurves }
                }
                ;

                instrumentCurve = new YieldCurve(
                    Definition.Name,
                    Market.ReferenceDate,
                    mktInstruments.ToArray(),
                    curveConvention.BusinessDayConvention.ToBda(),
                    curveConvention.DayCount.ToDayCountImpl(),
                    curveConvention.Calendar.ToCalendarImpl(),
                    curveConvention.Currency.ToCurrencyCode(),
                    curveConvention.Compound.ToCompound(),
                    curveConvention.Interpolation.ToInterpolation(),
                    Definition.Trait.ToYieldCurveTrait(),
                    baseMarket,
                    expression,
                    null
                    );
            }
            return(new CurveData(instrumentCurve));
        }