softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

How to get option implied volatility #88

Closed ecpgieicg closed 3 years ago

ecpgieicg commented 4 years ago

Hi Abel,

Greetings.

I am trying to receive option implied volatility (the real time version).

According to this chart from IB, generic tick 106 should have option IV, which has tick id 24. But when I did the following query, the return doesn't have option IV.

>> [databuf,datalh] = TWS.initBufferForEvent(TWS.Events.MARKETDATA,10000);
>> [metabuf,metalh] = TWS.initBufferForEvent(TWS.Events.MARKETMETADATA,1000);
>> lherr  = event.listener(TWS.Events.getInstance, TWS.Events.ERROR, @(s,e)disp(e.event.data));
>> genericTickList = "100,101,104,106,411";

>> contract = com.ib.client.Contract();
>> contract.secType("OPT");contract.exchange("SMART");contract.currency("USD");contract.right("Call");
>> contract.symbol('GLD'); contract.lastTradeDateOrContractMonth("20200320"); contract.strike(152);

>> session.eClientSocket.reqMktData(reqId,contract,genericTickList,false,false,[]); pause(1);
% after a while
>> session.eClientSocket.cancelMktData(reqId);

>> cellfun(@(e)disp(e.data),collection2cell(metabuf))
lastTimestamp:1582228467
bidExch:INQZWHP
askExch:NQZ
OptionPutOpenInterest:0
OptionCallOpenInterest:5283
askExch:QZ
askExch:NQZ
askExch:NZ
askExch:NQZ
askExch:NZ
bidExch:NQZWHP
askExch:CNQZH
bidExch:Z
askExch:NQZH
bidExch:NQZHP
askExch:NQZWHP

Is it because IB4m does not collect option IV returns or is it because IB server didn't return any data on option IV?

Thank you for your help!

softwarespartan commented 4 years ago

IB4m returns all data for this API call. Generally, any API call that IB4m implements is 100%, meaning that IB4m never partially implements a piece of functionality from IB API. It is all or nothing.

Specific to your question, sometimes it can take a while to get a generic tick of particular type and sometimes the types are a bit mixed up in TWS i've found previously. You might try add all generic ticks and see if that triggers what you're looking for then you can start to remove ones you don't need and test that it's still working.

genericTickList = [                                      ...
                   '100,101,105,106,107,125,165,166,'    ...
                   '225,232,221,233,236,258, 47,291,'    ...
                   '293,294,295,318,370,370,377,377,'    ...
                   '381,384,384,387,388,391,407,411,'    ...
                   '428,439,439,456, 59,459,460,499,'    ...
                   '506,511,512,104,513,514,515,516,517' ...
                  ];
Despair2000 commented 4 years ago

Hi @ecpgieicg

Did you somehow manage to get the implied volatility and/or the greeks? I have the exactly same problem now.

Despair2000 commented 4 years ago

I got it working (at least I got something LOL). All the options related data like IV and the greeks doesn't pop up in the meta buffer but triggers an OPTIONSCOMPUTATION event.

softwarespartan commented 4 years ago

If you guys want to share final working example for this, we can add it to the IB4m/examples

Sent from my iPhone

On Jul 7, 2020, at 11:24 AM, Despair2000 notifications@github.com wrote:

 I got it working (at least I got something LOL). All the options related data like IV and the greeks doesn't pop up in the meta buffer but triggers an OPTIONSCOMPUTATION event.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

Despair2000 commented 4 years ago

I can do so when I got so far.

@softwarespartan is it possible that there is another event that has to be exposed? Some of the options related data seems to generate a tickGeneric event. This event doesn't exist in IB4m, if I understand it right.

softwarespartan commented 4 years ago

No additional events that I am aware of. AFAIK, it's all options computation events.

The TWS.Session initializes a buffer to listen for all events (all events implemented which is most of them). So if in doubt, double check what's in the session.eventBuffer

I double checked and everything looks ok. The tickGeneric generates a MarketMetadata event.

https://interactivebrokers.github.io/tws-api/option_computations.html

https://interactivebrokers.github.io/tws-api/tick_types.html

https://interactivebrokers.github.io/tws-api/rtd_simple_syntax.html

Despair2000 commented 4 years ago

I see. Hmm strange. There are many ticks that I don't receive. I got the greeks and I got IV but others for example for historical volatility are not returned. I will keep on testing. Maybe this ticks don't work on a paper trading account.

Despair2000 commented 4 years ago

I must correct myself. I do receive an optionscomputation event only when I call calculateImpliedVolatility. Then I receive IV and the greeks. I do not receive any of them by calling reqMarketData. With reqMarketData I only receive exactly the same like ecpgieicg no matter which ticks I specify in the tickList. :-/ I tried to add all ticks to the tickList like you Abel suggested but then I get an error message that a lot of this ticks are not available for options along with a list which ticks ARE available. But the listed ticks are the same and among them for example also 106 for IV but when I use them nothing is returned. I also checked session.eventBuffer and there is nothing there what I don't find in the meta or data buffer.

All this is no disaster since we can retrieve a price for both the underlying and the option and then use calculateImpliedVolatility to calculate IV and the greeks but well it would be nice if the ticks were working.

Despair2000 commented 4 years ago

Finally, now I got it!

To retrieve the option IV (tick 106) and all the other generic ticks we were looking for, one has to request this generic ticks for the underlying stock and NOT for the options contract. If you want to use the options contract you can use calculateImpliedVolatility to calculate the greeks and IV. For all the other ticks use the underlying contract.

ecpgieicg commented 3 years ago

Hey, sorry for the super late reply. I missed a lot of emails (including github notifications) over the last few months. My apologies.

Hi @ecpgieicg

Did you somehow manage to get the implied volatility and/or the greeks? I have the exactly same problem now.

No I didn't.

Re an earlier question about HMDS query returned no data message. Again, sorry for the late reply. It means IB itself recorded no data for the period you requested but the contract is valid and IB likely has data recorded for a different time. This happens for example when you request beyond the earliest available data for a given contract. However, that's not the only time you will receive the no data message. For option contracts for example, often there is no bid or ask posted a week before expiry and you would receive the same error message if you request historical data for those time.

To retrieve the option IV (tick 106) and all the other generic ticks we were looking for, one has to request this generic ticks for the underlying stock and NOT for the options contract.

There is IV for the main underlying asset. And there is a separate IV for option contracts. Both are ofc specific to IB and not the vanilla Black-Scholes IV. In specific scenarios -- if you mainly reference IB IV, having IB IV archived can help inform trading decisions. Since querying IV and other option greeks do not take up additional data limit, if you are receiving a live feed on a particular option contract, you may as well query IB's IV.

The same line of reasoning applies to option greeks. But I haven't noticed IB using formulas beyond Black-Scholes for option greeks. In other words, the option greeks may be just as available locally without IB's data feed. And so that's a different discussion.

What does calculateImpliedVolatility do? It sounds like you experimented with it. Did the result match with live data feed?

ecpgieicg commented 3 years ago

Hi guys,

I tried experimenting with .eClientSocket.calculateImpliedVolatility.

I was able to submit a query to IB server without receiving error message. But which buffer/listener listens to and collects the data return?

Despair2000 commented 3 years ago

Hi @ecpgieicg

Good to have you back!

i actually never made it work to receive live greeks and option IV. This could be very useful. I ended up using the black-scholes functions of matlabs financial toolbox instead but being able to stream them live directly would definitely be nice.

How do you set it up? When I subscribe to market data for the option I don't receive the greeks or volatility ticks. But later I read that you somehow have to subscribe to the stock too (never tried it though). Maybe you can give me some hints?

The 'calculateImpliedVolatility' method is only a black-scholes calculator. It returns the information to your query to the 'OPTIONCOMPUTATION' callback. I assume this is just the plain-vanilla black-scholes values but actually never double-checked it. I don't use this API-function because the matlab-functions are much faster.

ecpgieicg commented 3 years ago

After Abel chimes in on .eClientSocket.calculateImpliedVolatility buffer/listener, we will need to speak with IB customer service. Because we are talking about IB not returning the right data.

I know it is available through Gateway because Excel API has option IV by strike and option greeks by strike.

I am not sure if you need to query the underlying stock simultaneously. I don't think it is. With Excel, I remember times when underlying wasn't available (e.g. due to incorrect formula) but the options and option greeks were.

.eClientSocket.calculateImpliedVolatility is not local btw. I looked through EClient.java and didn't see any formula locally.

And yes, IB IV is different from vanilla -- by a lot. You can see this by comparing Thinkorswim IVs to IB's. They are very different. ToS IV on the other hand is very close to vanilla Black-Scholes. Eventually, I'll play around modifying vanilla Black-Scholes-Merton myself. Until then, I do value IB's IV -- seeing that they are more likely to be closer to how large liquidity providers do it than I would be.

Despair2000 commented 3 years ago

The API documentation says explicitly "Note that to receive live greek values it is necessary to have market data subscriptions for both the option and the underlying contract."

It is possible that eClientSocket.calculateImpliedVolatility uses not the standard formula. As I said I didn't cross-check it. It just felt like a simple calculator. You plug in the values and it solves for and returns the missing one.

Sounds like I really must have a closer look at this IB option IV if it differs so much from the standard I'm using :-/

ecpgieicg commented 3 years ago

The API documentation says explicitly "Note that to receive live greek values it is necessary to have market data subscriptions for both the option and the underlying contract."

When they say market data subscriptions there, it probably means subscription to the tickers in general. Like a OBBO data pack. You can have subscription for either but not the other.

Despair2000 commented 3 years ago

This makes sense. However why I didn't receive any live greeks then? I will look here into this one more time. It was some time ago that I played around with it and maybe my memory is flawed. Maybe I simply lacking the right subscription? But is it possible to have market data for options but don't have the right package for the greeks? Which packages would I need?

ecpgieicg commented 3 years ago

I have the right packages though. And I am not getting the option greeks nor IV.

Despair2000 commented 3 years ago

Now I'm getting a little bit confused here. Didn't you say that you DO get the live greeks and IV? Or did you get them with EXCEL but were not able to get them with IB4m?

ecpgieicg commented 3 years ago

Excel only. But not through IB4m.

Despair2000 commented 3 years ago

Better late than never, I managed to get it working to receive live greeks and IV.

Actually it is rather simple (like everything once you understand it). You must subscribe to market data for both the stock and the option. Then you need to define a buffer to listen for OPTIONCOMPUTATION events. There you will receive ticks 10-13 along with the IV, the price of the option and the underlying stock.