Problems with lookup of security


Problems with lookup of security
Atom
10/31/2017


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.


Support

Avatar
Date: 10/31/2017
Reply


Hello,

IB do not provide search by futures or options codes. Only by underlying symbol. So for futures the lookup code should be

Code
connector.LookupSecurities(new Security
{
    Code = "SPX",
    Type = SecurityTypes.Future
});


For options chain

Code
connector.LookupSecurities(new Security
{
    Code = "SPX",
    Board = new ExchangeBoard { Code = "GLOBEX" },
    Type = SecurityTypes.Option,
    Currency = CurrencyTypes.USD,
});
Thanks:

Johan Kirsten

Avatar
Date: 10/31/2017
Reply


Hi

Thank you for the feedback. I can do what you suggest, but the reason for me loading it in the way I did, is I do not want to load 300+ options. It takes time and memory. I want to load a specific set of options/futures into memory. Can I specify strike, expiry and optiontype in the examples you specified?
Thanks:

Mikhail Sukhov

Avatar
Date: 10/31/2017
Reply


Johan Kirsten Go to
Hi

Thank you for the feedback. I can do what you suggest, but the reason for me loading it in the way I did, is I do not want to load 300+ options. It takes time and memory. I want to load a specific set of options/futures into memory. Can I specify strike, expiry and optiontype in the examples you specified?


Yes, you can put more filtering details. Here is sample how to filter by expiration date and option type. Strike also can be specified:

Code
connector.LookupSecurities(new Security
{
    Code = "SPX",
    Board = new ExchangeBoard { Code = "GLOBEX" },
    Type = SecurityTypes.Option,
    Currency = CurrencyTypes.USD,
    ExpiryDate = new DateTime(2017, 12, 14).ApplyTimeZone(TimeZoneInfo.Utc),
    OptionType = OptionTypes.Call,
});
Thanks:

Johan Kirsten

Avatar
Date: 11/1/2017
Reply


Hi

Thank you, this worked for me

Regards
Thanks:


Attach files by dragging & dropping, , or pasting from the clipboard.

loading
clippy