/// <summary> /// Performs a lookup for the givern security id if recursiveLookup is false and referenceLookup is true. /// </summary> /// <param name="securityIdDetails">The details of the security id for which to perform lookup.</param> /// <param name="lookupService">The lookup service to use</param> /// <returns>the security data.</returns> /// /// <exception cref="ServiceNotAvailableException">if the lookup service is not available.</exception> /// <exception cref="SecurityLookupException">if any error occurs when looking up the security data.</exception> /// <exception cref="SecurityDataCombiningException">if fail to combine the security data.</exception> private SecurityData PerformReferenceLookup(SecurityIdDetails securityIdDetails, ISecurityLookupService lookupService) { // check cache to get the securityDataRecord SecurityDataRecord securityDataRecord = securityDataCache[securityIdDetails.Id] as SecurityDataRecord; if (securityDataRecord != null && securityDataRecord.IsLookedUp) { return(securityDataRecord.SecurityData); } else if (securityDataRecord == null) { return(PerformSimpleLookup(securityIdDetails, lookupService)); } else { //Get the security data for lookupService SecurityData securityData = lookupService.Lookup(securityIdDetails); //Combine the record in cache with the one returned. securityData = securityDataCombiner.Combine(securityData, securityDataRecord.SecurityData); //Add or update cache with the cross-referenced security data AddUpdateCache(securityData); return(securityData); } }
/// <summary> /// Performs a lookup for the givern security id if recursiveLookup is true. /// </summary> /// <param name="securityIdDetails">The details of the security id for which to perform lookup.</param> /// <param name="lookupService">The lookup service to use</param> /// <returns>the security data.</returns> /// /// <exception cref="UnknownSecurityIdTypeException"> /// if the type of the given security id is unknown. /// </exception> /// <exception cref="InvalidSecurityIdFormatException">if the format of security id is invalid.</exception> /// <exception cref="SecurityIdParsingException">if any other error occurs in parsing the id.</exception> /// <exception cref="ServiceNotAvailableException">if the lookup service is not available.</exception> /// <exception cref="SecurityLookupException">if any error occurs when looking up the security data.</exception> /// <exception cref="SecurityDataCombiningException">if fail to combine the security data.</exception> private SecurityData PerformRecursiveLookup(SecurityIdDetails securityIdDetails, ISecurityLookupService lookupService) { // check cache to get the securityDataRecord SecurityDataRecord securityDataRecord = securityDataCache[securityIdDetails.Id] as SecurityDataRecord; if (securityDataRecord != null) { return(securityDataRecord.SecurityData); } else { //Get the security data for lookupService SecurityData securityData = lookupService.Lookup(securityIdDetails); //Add current id to cache. This prevents from infinite recursion due to cyclic references. securityDataCache[securityIdDetails.Id] = new SecurityDataRecord(securityData, true); //Create all possible cross-references and store in combinedData SecurityData combinedData = securityData; foreach (string refId in securityData.ReferenceIds) { //Check the reference id in cache SecurityDataRecord refRecord = securityDataCache[refId] as SecurityDataRecord; SecurityData refData; //if not present in cache, the load from lookup if (refRecord == null) { //Get details of the reference id SecurityIdDetails refDetails = securityIdParser.Parse(refId); //get lookup service for the refId ISecurityLookupService refLookupService = null; securityLookupServices.TryGetValue(refDetails.Type, out refLookupService); //No lookup service found for current type. if (refLookupService == null) { throw new NoSuchSecurityLookupServiceException( "No lookup service exists for the security type: " + refDetails.Type); } refData = PerformRecursiveLookup(securityIdParser.Parse(refId), refLookupService); } else { refData = refRecord.SecurityData; } //Combine the current combined data with combinedData = securityDataCombiner.Combine(combinedData, refData); } //All cross-refernced ids must now point to the same combinedData instance AddUpdateCache(combinedData); return(combinedData); } }
public void TestParse() { SecurityIdDetails sid = fsm.Parse("A"); Assert.AreEqual(sid.Id, "A"); Assert.AreEqual(sid.Type, SecurityIdType.SymbolTicker); }
/// <summary> /// <para>Convert the security id of ISIN (US only) type to a security id of CUSIP type.</para> /// </summary> /// /// <param name="isinSecurityId">the security id of CUSIP type.</param> /// <returns>the security id of CUSIP type.</returns> /// /// <exception cref="SelfDocumentingException"> /// <para>Wraps ArgumentException: if the given argument is empty string.</para> /// <para>Wraps ArgumentNullException: if the given argument is null.</para> /// </exception> /// <exception cref="UnknownSecurityIdTypeException">if type of the given security id is unknown.</exception> /// <exception cref="InvalidSecurityIdFormatException">if the format of security id is invalid.</exception> /// <exception cref="SecurityIdParsingException">if any other error occurs in parsing the id.</exception> /// <exception cref="InvalidSecurityIdTypeException">If the given security id is not ISIN (US only) type. /// </exception> public string ConvertFromISINToCUSIP(string isinSecurityId) { SecurityIdDetails securityIdDetails = null; string ret = null; try { Helper.ValidateNotNullNotEmpty(isinSecurityId, "isinSecurityId"); //Must be parseable to type ISIN securityIdDetails = securityIdParser.Parse(isinSecurityId); if (securityIdDetails.Type != SecurityIdType.ISIN) { throw new InvalidSecurityIdTypeException("Type of isinSecurityId is not ISIN."); } //Must start with US if (!isinSecurityId.ToUpper().StartsWith("US")) { throw new InvalidSecurityIdTypeException("isinSecurityId is not a valid US ISIN."); } //Remove the country code and the check digit ret = isinSecurityId.Remove(isinSecurityId.Length - 1).Remove(0, 2); //Check if underlying CUSIP is valid try { securityIdDetails = securityIdParser.Parse(ret); } catch { throw new InvalidSecurityIdTypeException("isinSecurityId has not been derived from a valid CUSIP."); } return(ret); } catch (Exception e) { throw Helper.GetSelfDocumentingException(e, "Unable to convert ISIN code to CUSIP.", "TopCoder.FinancialService.Utility.FinancialSecurityManager.ConvertFromISINToCUSIP", new string[] { "securityIdParser", "securityLookupServices", "recursiveLookup", "referenceLookup", "securityDataCache", "securityDataCombiner" }, new object[] { securityIdParser, securityLookupServices, recursiveLookup, referenceLookup, securityDataCache, securityDataCombiner }, new string[] { "isinSecurityId" }, new object[] { isinSecurityId }, new string[] { "securityIdDetails", "ret" }, new object[] { securityIdDetails, ret }); } }
/// <summary> /// <para>Lookup the security data by the given security id details.</para> /// </summary> /// /// <param name="securityIdDetails">the security id details used to lookup security data.</param> /// <returns>the security data.</returns> /// /// <exception cref="SelfDocumentingException">Wraps ArgumentNullException is argument is null.</exception> /// <exception cref="UnknownSecurityIdTypeException">if type of the given security id is unknown.</exception> /// <exception cref="InvalidSecurityIdFormatException">if the format of security id is invalid.</exception> /// <exception cref="SecurityIdParsingException">if any other error occurs in parsing the id.</exception> /// <exception cref="NoSuchSecurityLookupServiceException">if there is no corresponding ISecurityLookupService /// defined for the specific security identifier type.</exception> /// <exception cref="ServiceNotAvailableException">if the lookup service is not available.</exception> /// <exception cref="SecurityLookupException">if any error occurs when looking up the security data.</exception> public SecurityData Lookup(SecurityIdDetails securityIdDetails) { ISecurityLookupService lookupService = null; try { Helper.ValidateNotNull(securityIdDetails, "securityIdDetails"); // get corresponding lookup service. Use TryGetValue so that KeyNotFoundException is not encountered. securityLookupServices.TryGetValue(securityIdDetails.Type, out lookupService); //No lookup service found for current type. if (lookupService == null) { throw new NoSuchSecurityLookupServiceException( "No lookup service exists for the security type: " + securityIdDetails.Type); } //Since cache is checked and updated many times in the same function, //there needs to control against race conditions. lock (securityDataCache) { if (recursiveLookup) { return(PerformRecursiveLookup(securityIdDetails, lookupService)); } else if (referenceLookup) { return(PerformReferenceLookup(securityIdDetails, lookupService)); } else { return(PerformSimpleLookup(securityIdDetails, lookupService)); } } } catch (Exception e) { throw Helper.GetSelfDocumentingException(e, "Unable to perform lookup.", "TopCoder.FinancialService.Utility.FinancialSecurityManager.Lookup(SecurityIdDetails)", new string[] { "securityIdParser", "securityLookupServices", "recursiveLookup", "referenceLookup", "securityDataCache", "securityDataCombiner" }, new object[] { securityIdParser, securityLookupServices, recursiveLookup, referenceLookup, securityDataCache, securityDataCombiner }, new string[] { "securityIdDetails" }, new object[] { securityIdDetails }, new string[] { "lookupService" }, new object[] { lookupService }); } }
/// <summary> /// <para>Convert the security id of CUSIP type to a security id of ISIN type. /// The country code used is 'US'</para> /// </summary> /// /// <param name="cusipSecurityId">the security id of CUSIP type.</param> /// <returns>the security id of ISIN type.</returns> /// /// <exception cref="SelfDocumentingException"> /// <para>Wraps ArgumentException: if the given argument is empty string.</para> /// <para>Wraps ArgumentNullException: if the given argument is null.</para> /// </exception> /// <exception cref="UnknownSecurityIdTypeException">if type of the given security id is unknown.</exception> /// <exception cref="InvalidSecurityIdFormatException">if the format of security id is invalid.</exception> /// <exception cref="SecurityIdParsingException">if any other error occurs in parsing the id.</exception> /// <exception cref="InvalidSecurityIdTypeException">If the given security id is not of CUSIP type.</exception> public string ConvertFromCUSIPToISIN(string cusipSecurityId) { SecurityIdDetails securityIdDetails = null; string isin = null; try { Helper.ValidateNotNullNotEmpty(cusipSecurityId, "cusipSecurityId"); //Parse the securityId securityIdDetails = securityIdParser.Parse(cusipSecurityId); if (securityIdDetails.Type != SecurityIdType.CUSIP) { throw new InvalidSecurityIdTypeException("Type of securityId is not CUSIP."); } //Append US to it. isin = "US" + cusipSecurityId.ToUpper(); //Append check digit isin += Helper.GetCheckDigitForIsin(isin); return(isin); } catch (Exception e) { throw Helper.GetSelfDocumentingException(e, "Unable to convert CUSIP to ISIN.", "TopCoder.FinancialService.Utility.FinancialSecurityManager.ConvertFromCUSIPToISIN", new string[] { "securityIdParser", "securityLookupServices", "recursiveLookup", "referenceLookup", "securityDataCache", "securityDataCombiner" }, new object[] { securityIdParser, securityLookupServices, recursiveLookup, referenceLookup, securityDataCache, securityDataCombiner }, new string[] { "cusipSecurityId" }, new object[] { cusipSecurityId }, new string[] { "securityIdDetails", "isin" }, new object[] { securityIdDetails, isin }); } }
/// <summary> /// Performs a lookup for the givern security id if recursiveLookup and referenceLookup are both false. /// </summary> /// <param name="securityIdDetails">The details of the security id for which to perform lookup.</param> /// <param name="lookupService">The lookup service to use</param> /// <returns>the security data.</returns> /// /// <exception cref="ServiceNotAvailableException">if the lookup service is not available.</exception> /// <exception cref="SecurityLookupException">if any error occurs when looking up the security data.</exception> /// <exception cref="SecurityDataCombiningException">if fail to combine the security data.</exception> private SecurityData PerformSimpleLookup(SecurityIdDetails securityIdDetails, ISecurityLookupService lookupService) { // check cache to get the securityDataRecord SecurityDataRecord securityDataRecord = securityDataCache[securityIdDetails.Id] as SecurityDataRecord; if (securityDataRecord != null) { return(securityDataRecord.SecurityData); } else { //If not in cache then perform lookup SecurityData securityData = lookupService.Lookup(securityIdDetails); //Combine with itself. Combining a securityData instance with itself has the effect of //adding the securityID to the referenceIds SecurityData combinedData = securityDataCombiner.Combine(securityData, securityData); //Create all possible cross references foreach (string refId in securityData.ReferenceIds) { //Check the reference id in cache SecurityDataRecord refSecurityData = securityDataCache[refId] as SecurityDataRecord; if (refSecurityData != null) { combinedData = securityDataCombiner.Combine(securityData, refSecurityData.SecurityData); } } //Add or update cache with the cross-referenced security data AddUpdateCache(combinedData); return(combinedData); } }
public void TestConstructorFail4() { sid = new SecurityIdDetails("A", " "); }
public void TestConstructorFail3() { sid = new SecurityIdDetails(" ", "ABCD"); }
public void TestConstructorFail2() { sid = new SecurityIdDetails("A", null); }
public void TestConstructorFail1() { sid = new SecurityIdDetails(null, "ABCD"); }
public void TearDown() { sid = null; }
public void SetUp() { sid = new SecurityIdDetails("A", FinancialMarket.AMEX); }