3

I'm trying to snap real time market data using IBrokers API on R.

For an odd reason, Microsoft (MSFT) doesn't work.

For example, this works:

library("IBrokers")
tws <- twsConnect()
nms <- c("AAPL","YHOO")
reqMktData(tws, lapply(nms, twsSTK), tickGenerics="", snapshot=T)
twsDisconnect(tws)

However, this doesn't work:

library("IBrokers")
tws <- twsConnect()
nms <- c("AAPL","YHOO","MSFT")
reqMktData(tws, lapply(nms, twsSTK), tickGenerics="", snapshot=T)
twsDisconnect(tws)

The error message is below:

2 3 200 The contract description specified for MSFT is ambiguous. 

However, this isn't an ambiguous ticker, and is on the same exchange as YHOO and AAPL.

Does anyone know what I need to do to get around this issue? Thank you.

Trexion Kameha
  • 3,362
  • 10
  • 34
  • 60
  • 1
    Look at `reqContractDetails(tws, twsSTK("MSFT", currency=""))`. There are multiple instruments with a ticker of "MSFT". You could pass a single contract to `reqMktData`. e.g. `reqContractDetails(tws, twsSTK("MSFT"))[[1]][["contract"]]` – GSee Dec 16 '14 at 03:33
  • I see, thank you GSEE. It looks like the first one is always the primary listing. The other two are trading on the AEB exchange and IBIS exchange, both of which are not in the United States. Do you know how I can have it default to the first one that comes out, as per your suggestion? – Trexion Kameha Dec 16 '14 at 04:50
  • Thank you GSee. Looks like the right syntax will be: `reqMktData(tws,twsSTK("MSFT",exch="SMART", primary="NASDAQ", currency="USD"),tickGenerics="",snapshot=T)` I was wondering if you had a suggestion on how I can integrate it into the original code above? It would require dynamically selecting the "primary" and "exch" for each identifier. Thank you much! This is really helpful! – Trexion Kameha Dec 16 '14 at 05:01
  • You cannot assume that the first one is the one you want. I think that if the last contract you worked with was a European stock, then if you call `reqContractDetails`, it will show the European stock in the first slot. I'm not 100% sure about that, but I'm fairly confident that the order in which the contracts are returned will change, perhaps even in the same session. – GSee Dec 16 '14 at 13:35

3 Answers3

5

To get around this I simply specified the stock exchange for separate tickers that trade ambiguously on nasdaq.

tickers_nasdaq<-c("MSFT","INTC","CSCO")
reqMktData(tws, lapply(tickers_nasdaq, twsSTK, exch = "SMART", primary="NASDAQ", currency = "USD"), tickGenerics="", snapshot=T)

Obviously this isn't ideal, but at least it works.

Trexion Kameha
  • 3,362
  • 10
  • 34
  • 60
1

Later answer...
You shouldn't specify the primary exchange because you'll have problems with other symbols, instead, specify the m_localSymbol on your contract() to the same value as the m_symbol, in this case: MSFT

https://www.interactivebrokers.com/en/software/api/apiguide/java/contract.htm

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

There are a couple of ways to do this:

  1. Request contract details with both symbol and currency defined. Use the received Contract object going forward. This works for 99.9% of cases, only exceptions would be symbols that trade on different exchanges simultaneously AND have the same currency, this is possible for names listed in London for example where they trade using foreign currencies.

  2. Catch the ambiguous error in your handler and resend again by trying all the primaryExchanges one by one (NYSE, NASDAQ, BATS, ARCA etc).

  3. Build an internal dict or key-value pair of equities with their corresponding exchanges.

misantroop
  • 2,276
  • 1
  • 16
  • 24