Problems with lookup of security
I have a windows service that hosts a web api interface. The service connects to an interactive broker TWS. A call to the web api must lookup the security and return the Id. The code to do this is:
var criteria = new SecurityLookupMessage()
{
Class = "",
ExpiryDate = DateTimeOffset.Parse(expiryDate),
OptionType = optionType == null ? default(OptionTypes?) : (OptionTypes)Enum.Parse(typeof(OptionTypes), optionType),
Strike = strike,
UnderlyingSecurityCode = underlyingSecurityCode,
SecurityType = securityType == null ? default(SecurityTypes?) : (SecurityTypes)Enum.Parse(typeof(SecurityTypes), securityType),
};
var security = _connector.Securities.FirstOrDefault(s =>
(s.UnderlyingSecurityId.StartsWith(criteria.UnderlyingSecurityCode)) &&
(s.Board?.Code == boardCode) &&
(s.Class == @class) &&
(s.Type == criteria.SecurityType) &&
(s.ExpiryDate == criteria.ExpiryDate) &&
(s.OptionType == criteria.OptionType) &&
(s.Strike == criteria.Strike));
if (security == null)
{
var onNewSecurity = new Action<Security>(s =>
{
if ((!s.UnderlyingSecurityId.StartsWith(criteria.UnderlyingSecurityCode)) ||
(s.Type != criteria.SecurityType) ||
(s.Board?.Code != boardCode) ||
(s.Class != @class) ||
(s.ExpiryDate != criteria.ExpiryDate) ||
(s.OptionType != criteria.OptionType) ||
(s.Strike != criteria.Strike) )
return;
waitHandle.Set();
});
_connector.NewSecurity += onNewSecurity;
_connector.LookupSecurities(criteria);
waitHandle.WaitOne(30000);
_connector.NewSecurity -= onNewSecurity;
security = _connector.Securities.FirstOrDefault(s =>
(s.UnderlyingSecurityId.StartsWith(criteria.UnderlyingSecurityCode)) &&
(s.Board?.Code == boardCode) &&
(s.Class == @class) &&
(s.Type == criteria.SecurityType) &&
(s.ExpiryDate == criteria.ExpiryDate) &&
(s.OptionType == criteria.OptionType) &&
(s.Strike == criteria.Strike));
}
If I search for "SPZ7@GLOBEX" and then "SPXW 171215C02580000@SMART" it works fine. But if I first search for "SPXW 171215C02580000@SMART", then it cannot find "SPZ7@GLOBEX". It is like the option prohibits the searching of the future. Also, sometimes if I search for the option first, then it only finds "SPXW 171215C02580000@ALL" and "SPXW 171215C02580000@CBOE", but then if I search again no search returns "SPXW 171215C02580000@SMART". It is like the incomplete loaded results prohibit the later loading of extra results.