/// <summary> /// Loops through the given opportunityList and looks for matching arbitration opportunities in the /// running opportunity dictionary. For opportunities that are matched, the roundsExisted property /// is incremented by 1. If a matching opportunity is not found, it is added to the dictionary. In both /// cases, 'updated' is set to true. /// </summary> /// <param name="opportunityList">List of opportunities to locate within the running dictionary.</param> private void FindAndUpdateMatchingOpportunityInRunningList(List <ArbitrationOpportunity> opportunityList) { //For every opportunity in the given list, see if an opportunity with the same buy/sell exchange pair //exists in the running dictionary. foreach (ArbitrationOpportunity opportunity in opportunityList) { string dictionaryKey = opportunity.BuyExchange.Name + opportunity.SellExchange.Name; if (_opportunityDictionary.ContainsKey(dictionaryKey)) { OpportunityValidationInfo opportunityValidationInfo = _opportunityDictionary[dictionaryKey]; opportunityValidationInfo.Opportunity = opportunity; opportunityValidationInfo.RoundsExisted += 1; opportunityValidationInfo.Updated = true; _opportunityDictionary[dictionaryKey] = opportunityValidationInfo; } //If the current buy/sell exchange pair does not exist in the running opportunity dictionary, add it. else { //This is the first round the opportunity occurs, so roundsExisted = 1 _opportunityDictionary.Add(dictionaryKey, new OpportunityValidationInfo() { Opportunity = opportunity, RoundsExisted = 1, Updated = true }); } } }
private List <ArbitrationOpportunity> CleanRunningDictionaryAndBuildValidateOpportunityList() { //List used to keep track of which entries in the dictionary need to be remvoed. List <string> removalList = new List <string>(); //Can't iterate over the dictionary itself, so build a list of dictionary keys List <string> dictionaryKeys = new List <string>(_opportunityDictionary.Keys); List <ArbitrationOpportunity> returnList = new List <ArbitrationOpportunity>(); foreach (string dictionaryKey in dictionaryKeys) { OpportunityValidationInfo opportunityValidationInfo = _opportunityDictionary[dictionaryKey]; //Opportunity was not updated, which means it no longer exists. Flag that entry for deletion. if (opportunityValidationInfo.Updated == false) { removalList.Add(dictionaryKey); } //Entry was updated, which means the opportunity still exists. If it has existed for the required number of rounds, //add it to the return list. Otherwise, just reset 'updated' to false to prepare for the next round. else { opportunityValidationInfo.Updated = false; _opportunityDictionary[dictionaryKey] = opportunityValidationInfo; if (opportunityValidationInfo.RoundsExisted >= RoundsRequiredForValidaton) { returnList.Add(opportunityValidationInfo.Opportunity); } } } //Remove all entries that were flagged for deletion foreach (string dictionaryKey in removalList) { _opportunityDictionary.Remove(dictionaryKey); } return(returnList); }