rtradingibrokers

R IBrokers. How to get currency contract to work in reqHistoricalData calls? e.g. CADUSD rates? And how to pull index prices? e.g. S&P, DJI?


I am currently learning how to use the IBrokers package.

I can pull equity prices at frequencies I want, and option prices, but I'm currently stuck on pulling foreign exchange rates.

I don't know how to set up the right twsContract, for calling with reqHistoricalData.

I am interested in getting CADUSD currencies on say a 1 min basis. How can I do this?

The example in the manual gives:

currency <- twsCurrency("EUR")

When I try and call this with say reqHistoricalData(tws, currency) it comes back with an error saying something like "no historical data available 1D". As I can't even get the example in the manual to work, I'm rather stuck...

Can someone please enlighten me as to what the correct syntax is? If I have a couple of examples that actually work, I can take it from there.

Also, when I try using "USD.CAD" or "CAD.USD" as the ticker in the twsContract object, I'm getting complaints that this isn't a valid security. How can I find out what the right ticker name for USD.CAD would be? Right now I figure out the ticket for stocks by looking at my interactive brokers application which is open at the same time obviously, and typing the company name and doing a search. The ticker generally comes back e.g. (type apple, get back AAPL when I want to add this security to my portfolio in the interactive brokers workstation).

Any help would be much appreciated.

Also, another related question is how can I pull the historical of say the S&P500 price index on a 1 min basis say?

I am guessing I would use a twsContract object in R, but I don't know what parameters to use. What should the ticker be? The trade workstation mentions it's an "index" product type (obviously), which doesn't fit into any of the wrappers like twsStock, twsCurrency etc... Again, Is there an example of this somewhere, which actually works?


Solution

  • You're having trouble getting data for FX because you're trying to get TRADES data which IBrokers does not disseminate for FX. Instead, you should use whatToShow="BID" or whatToShow="Ask". e.g.

    tws <- twsConnect()
    ccy <- reqContractDetails(tws, twsCurrency("USD", "CAD"))[[1]]$contract
    reqHistoricalData(tws, ccy, whatToShow='BID')
    

    Getting data for the S&P 500 index is similar

    reqHistoricalData(tws, reqContractDetails(tws, twsIndex("SPX", "CBOE", "USD"))[[1]]$contract)
    

    Your question is pretty broad and reads like a request for an overview of the package. Have you read the vignette? (vignette("IBrokers"))


    I have a package on R-Forge called twsInstrument that provides some wrappers to make things easier for you.

    library(twsInstrument)
    get_quote("USD.CAD")
    getBAT("USD.CAD") #gets the last 5 days of minutely Bid/Ask/Trade/Midpoint
    

    Also, see ?reqTBBOhistory and twsInstrument:::update.data to download lots of data to disk.

    Finally, I recommend asking these types of questions on the r-sig-finance mailing list where Jeff is likely to see it.