JanBoonen / TwsApiCpp

Improved TWS API POSIX C++ library for the Interactive Brokers (IB) TWS (same project as TwsApiC++ in Yahoo TWSAPI).
106 stars 56 forks source link

Need an example of getting a contract specification for use in a combo options trade #11

Closed markmcwiggins closed 5 years ago

markmcwiggins commented 5 years ago

Hi ...

Thanks again for providing this code ... I got the spread example to work last week using it ..

But! I used contractIDs I had gotten through another API ... is there an example somewhere I'm missing?

I would like to be able to do this just using localSymbol, but that doesn't seem to work.

Any guidance on this? Thanks much.

stevegee58 commented 5 years ago

Only way I know of is to download the option chain for that symbol from IB TWS, then you can store the contract IDs accessible by P/C, expiry, and strike. My application will download the chain for the selected symbol and store it in a file if it doesn't already exist. That way if I exit the program during the day I still have the chain if I restart the program. Before the market opens the next day I delete the file and start the program. That way I get a fresh chain since the chain is different every day. Downloading the chain for indexes like SPX and ETFs like SPY can take a minute or so from TWS.

markmcwiggins commented 5 years ago

What I'm doing is just automating a few trades per week; I could download the whole chain but all I really need is a maximum 4 contracts per trade; no reasonable way to do that?

If not would you mind sharing a snippet of your code to download the whole chain?

stevegee58 commented 5 years ago

The following will download the whole chain for 1 symbol. I never download the whole chain, just the symbol I'm interested in.


        Contract C;

        C.symbol = symbol;
        C.secType = *SecType::OPT;      //"OPT"
        C.currency = "USD";
        C.exchange = *Exchange::IB_SMART;   //"SMART";
        if (expiry != "")
        {
            C.expiry = expiry;
        }
        // C.right = call or put
        // C.strike = desired strike

        m_EC->reqContractDetails(m_NextOrderId++, C);

I always just download all the expiries for the symbol I'm trading, once every morning. As you can see you can just download a single expiry too. I haven't done this but if you know you only want calls or puts you could add that to the contract specification, probably the same for strikes. BTW I only trade SPX and SPY.

markmcwiggins commented 5 years ago

Thanks!!