¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io
Date

Little tool for option chain contract creation

juxeiier
 

¿ªÔÆÌåÓý

Hi,

I have created a little library which can scrape option chains in a (hopefully) easy manner.
The topic is discussed here
/g/twsapi/message/46566
/g/twsapi/message/42241
/g/twsapi/topic/81356218

The source code, together with installation details, is here .
The usage is then as simple as
chain = ibt.getOptionContractsUpUntilDays(aapl, 60)

or

aapl=Stock(symbol='AAPL', exchange='SMART/AMEX')
chains = ibt.getOptionContractsUpUntilDays(aapl, 60)
It works on my site, but is very new, so probably some shortcomings :)
Would be great if someone could test it.

Cheers,
Juergen


Order cancelation reason

 

How do I distinguish order rejection at the very beginning (like margin) from order cancellation, say, on time expired for GTD orders? I get order status update event and it has status ¡®Cancelled¡¯.

Thanks,
Alex


Re: How to create a performance efficient trading app so each sub process runs smoothly in Python?

 

I love Python but it has its own place and it's not good for real-time-ish parallel compute. You can hack around things but in the end it won't be crazy.

You can consider Go and node.js from mainstream languages (apart from C++).
I personally always wanted to try Elixir but never got around - perfect for highly concurrent stuff.?

On Sat, Mar 13, 2021 at 2:52 PM matt <rmdeboer82@...> wrote:
What would be the best way to do on-the-fly computations using Numpy, Pandas for trading using IB TWS API, writing to/from mySQL, and in the meanwhile receive and send data and orders?

From (and ) I conclude that CPU-bound programs, those that spend most of the their time processing data (like on the-fly computations using Pandas which is synchronous single threaded), solutions like threading (pre-emptive multitasking) and asyncio are inferior (and may disrupt the sending/receipt of data or introduce some lag or data drops) since there is only one Python interpreter on a single core/processor (asyncio aligns I/O processes more efficiently) that takes care of both the CPU computations and checking for socket data.

I know there is? a ib_insync library that implements cooperative multitasking (asyncio) in Python, but will the overall program be fast enough if you do computations using large dataframes? I read that on a socket level there might not be any limitations (¡°socket itself is actually thread-safe¡±), as suggested by . I do not assume that Ptyhon has it implemented this way since the Global Interpreter Lock is in place (single thread, synchronous). From :

IB.sleep(0); may be an alternative, but I do not think a Pandas groupby() can be interrupted once called.

Should I use the Multiprocessing library and do every task on its own processor (thus dataframe calculations, mySQL connection read/write, sending and receiving orders and data)?
Or should I combine some things using (I think some next level threading which can be combined with the normal way of interpretation of code by Python) or the (has to do with built-in async for Scikit-Learn, Pandas, etc. functions, if I understand correctly) library? I think this is next level, and may be an overkill.

Or should stick to Java (I do not know if there is a Pandas like alternative which allows easy manipulation of data and performing machine learning for example, but threading may be organized better)?

Any suggestions or tutorials?

(I read something about was well, but that¡¯s a whole different story since it is not directly applicable to IB TWS API, and may be prone to delays resulting from single core processing/interpreting by Python as well, if I understand correctly)


Re: Request for options contracts per expirtation date

 

I'm not a fan of TWS API design at all (to say the least). But at this point it has so much history and dependencies that it is not feasible to improve it dramatically in an evolutionary manner. You probably know that being 30 years in software development. :)

On your topic... I think I just replied how I get the chains in another thread using?reqSecDefOptParams as Francois suggested.

On Tue, Mar 16, 2021 at 7:15 AM juxeiier <juxeiier@...> wrote:
Hi Francois,

thx for the links.
All of this leaves me really speachless about the quality of the IB API.
I am a professional software developer since >30years, but a function like reqSecDefOptParams?plays in the primary league of "worst design never".
Not only does it return invalid data, but the concept inherently leads to massive server requests.

So sad, and since this topic is years old, IB obviously does not care.
Do they even have a valid email adress to their developers?


Re: How to get all VALID options

 

I do get valid chains in this way:
chains = ib.reqSecDefOptParams(contract.symbol, '', contract.secType, contract.conId)

then I just get the one for exchange SMART.

Then for expiration date I need, I get the list of strikes and create a list of Option() objects - options.
that list I pass to reqQualifyContracts() -?ib.qualifyContracts(*options)

The combinations of expirations and strikes that don't exist don't pass qualification (you'll see errors in the stderr but they are handled in the background).

The options that were qualified successfully have non-zero conId so I filter them out.

You don't have to do too many calls - just get a product of expirations/strikes you need and then create list of Option() objects, qualify them and filter on conId.

I use the ib-insync Python module so my method calls might be slightly different from standard API (I guess only capitalization?).

Is that what you are also doing?



On Tue, Mar 16, 2021 at 7:01 AM juxeiier <juxeiier@...> wrote:
This is really unbelievable.
The quality of this API is really low, which is especially evident in this problem.
I have now implemented something with Yahoo finance, which gives me the valid option chains.
Then I create all needed contracts and serialize them to a file(because it takes an enormous amount of time to get them).
That way, the cached contracts can be reloaded and only new expiration chains well need to be added.
But still, why do we need to do this?
We deserve better from IB, since all this data is available in the option trader window.

I now struggle in reveiving the greeks for the options. Are they even there when markets are closed?


STK/STK Combo Order with Python

 

Has anyone written a working Python code for sending orders on an STK / STK combo?


Re: Anyone upgraded Java version for TWS on Linux?

 

Thanks Hunter, very helpful.

I did some digging around in that directory as you suggested and found some differences between various files. I copied a few files over from the IB JVM directory to the stock JDK 8 directory. Still didn't work. I've given up as the performance actually does seem to be OK now; at least not bad enough that it's worth spending more time digging.

But this was helpful advice that I've filed away in case it becomes a big enough issue.

Thanks
John


On Mon, Mar 15, 2021 at 1:53 PM Hunter C Payne via <hunterpayne2001=[email protected]> wrote:
Hi,
I believe TWS comes with its own Java built into the installer.? I'm pretty sure I use that one.? TWS has never been the best written app but if you stick with stable its OK.?

I believe there are some weird security things built into IB's JVM they distribute which is why you can't make a stock JVM work.? I think if you dig into the IB JVM, you will probably find some security configs in the jre/lib directory (or inside of one of its sub-directories) that allow TWS to work.? Perhaps jre/lib/security/java.policy has been altered to only run IB signed jars or that TWS needs a custom Java security policy.? If you really want to hammer away at this that's where I would look.? As for the performance issues with TWS, perhaps the screen config you have is too busy or perhaps its using up all the pacing capacity and that's causing issues with refreshing the screen.? I have a hard time believing that upgrading the JVM would help.? The Java 8 JVM is really mature and the changes to it are mostly about very minor things that probably wouldn't impact the performance of a GUI app.

Hunter

On Monday, March 15, 2021, 11:32:32 AM PDT, John <jpelly@...> wrote:


No such luck for me - I let that error dialog sit and nothing happens; just sits there forever (rest of TWS will not start up).

I'd like to run TWS on my local Linux laptop, so hopefully someone else comments on this thread with some additional suggestions.

Correction to original message: I'm running Linux Mint 20.

Thanks all
John


On Mon, Mar 15, 2021 at 1:04 PM Ray Racine <ray.racine@...> wrote:
On latest Fedora Linux and I'm getting the exact same error as of today as well.? TWS ultimately starts up fine however.? I assume just not on the latest update :).

With regard to performance,? I've never had to do anything beyond adjusting the Max Heap. ? Performance is retalive, but I have some pretty big watch lists, charts with studies, plus API clients and performance has been fine for me.

I do get quite a few UI issues with menus and mouse pointing (not calculating the X-Y position correctly mostly) when I run under Sway / Wayland.??? The occasional annoying UI issues with XMonad / XOrg.? And no real UI issues to speak of with Gnome - Xorg or Wayland.

On Mon, Mar 15, 2021 at 11:52 AM John <jpelly@...> wrote:
I'm experiencing pretty bad performance of TWS GUI on Linux (Mint 19). I've tried to upgrade Java version that TWS uses, thinking that will help.

However I'm unable to change versions. Reading through the tws shell script, it looks like it pins the JRE version at 1.8.0_152. If I remove the check in the shell script and try to upgrade to openjdk version 1.8.0_282 (upgraded patch level), TWS won't start - says "we are unable to update the platform due to a networking error" (see attached screenshot)

Has anyone successfully been able to do this? It looks like you can upgrade Java versions on Windows so I was hoping to do it on Linux.

jws networking error.png


Option settlement price AM/PM

 

When a stock closes for trading at the end of business on expiration Friday, the last trade determines the options settlement price (PM settlement).
That is not true for those European-style options that expire in the morning (AM settlement), for instance RUT, NDX, and 3rd-Friday SPX. Settlement prices for RUT, NDX and the "original 3rd-Friday SPX options" are calculated by using the opening stock price for each stock in the index. These options stop trading when the market closes on Thursday, one day prior to expiration Friday.

Then, is there any way to get this settlement timing info about options, using API? For instance AM/PM settlement, or exact date+time of settlement time?


Re: How to get all VALID options

juxeiier
 

I probably will not need all call and put contracts, but at least say 2SD strikes prices away from current underlying prices.
Then I scan all chains for all symbols, for example to see if there is a nice Jade Lizard opportunity somewhere.
I definetly have no time to do this manually ;)


Re: How to get all VALID options

Matthias Frener
 

>??If I get all contracts for all chains for 120days from today, that takes 2hours just for 3 symbols!!
Do you need them all? (and you probably do something wrong, should be way faster with?reqSecDefOptParams).
Wanted to speed up option chain on UI - so no point on download all because I won't look at all anyhow. I keep the last 16 queried option chains on cache. If I open #17, #0 falls out of cache and #17 goes into. So if you want to look at #0 again, it will need to get strikes again, but #17 is fast now.



Am Di., 16. M?rz 2021 um 11:26?Uhr schrieb juxeiier <juxeiier@...>:

Hi Matthias,

thx for help.
You really cache the contracts locally? If I get all contracts for all chains for 120days from today, that takes 2hours just for 3 symbols!!
That is the reason I implemented a little serializer/caching mechanism with the pickler module(python).
After restart of the algorithm, I have all the contracts almost immediatly at my disposal.


Re: How to get all VALID options

juxeiier
 

Hi Matthias,

thx for help.
You really cache the contracts locally? If I get all contracts for all chains for 120days from today, that takes 2hours just for 3 symbols!!
That is the reason I implemented a little serializer/caching mechanism with the pickler module(python).
After restart of the algorithm, I have all the contracts almost immediatly at my disposal.


Re: Getting last Expired Option Price

Matthias Frener
 

I actually the closing the pricing of the current trading day.
Maybe we have different use-cases, but I only need expired option prices for the daily?settlement. So I use last price just after markte close.
The exact value (used on monthly balance) will than later sync from FlexQuery after the transaction shows up there.

Am Di., 16. M?rz 2021 um 10:19?Uhr schrieb Francois G via <namasteparis=[email protected]>:

Hi Mattias & Alexandre,
To get the underlying closing price, do you use reqHistoricalData for the expiration date?
Using Regular Trading Hours (RTH) = 1 or 0 ?
The closing price of the underlying for option calculation is normally based on the price AFTER market close, so RTH=0
Please confirm.


Re: How to get all VALID options

Matthias Frener
 

> I now struggle in reveiving the greeks for the options. Are they even there when markets are closed?

You probably have to switch to frozen market-data type. You will only see live-greeks while market is open, otherwise there is no bid/ask and difficult to calculate it. (the exact time depends on markte, e.g. 9:30-16:00 for stock options on CBEO or 24/5 (with 10min stop each day) at GLOBEX),

> But still,?why do we need to do this?
We deserve better from IB, since all this data is available in the option trader window.

TWS has same issue. Option an option chain and change between dates and scroll up/down the strikes. You will that is terrible slow, takes seconds until you have all the strikes and even longer until prices show up - because they probably have to da same as we have to.
What I did to make it smooth in my App is indeed to store it locally. I cache option chains that have been requested from TWS locally. So it only slow the first time, second time it's cache hit and there immediatly.










Am Di., 16. M?rz 2021 um 11:01?Uhr schrieb juxeiier <juxeiier@...>:

This is really unbelievable.
The quality of this API is really low, which is especially evident in this problem.
I have now implemented something with Yahoo finance, which gives me the valid option chains.
Then I create all needed contracts and serialize them to a file(because it takes an enormous amount of time to get them).
That way, the cached contracts can be reloaded and only new expiration chains well need to be added.
But still, why do we need to do this?
We deserve better from IB, since all this data is available in the option trader window.

I now struggle in reveiving the greeks for the options. Are they even there when markets are closed?


Re: Request for options contracts per expirtation date

juxeiier
 

Hi Francois,

thx for the links.
All of this leaves me really speachless about the quality of the IB API.
I am a professional software developer since >30years, but a function like reqSecDefOptParams?plays in the primary league of "worst design never".
Not only does it return invalid data, but the concept inherently leads to massive server requests.

So sad, and since this topic is years old, IB obviously does not care.
Do they even have a valid email adress to their developers?


Re: How to get all VALID options

juxeiier
 

This is really unbelievable.
The quality of this API is really low, which is especially evident in this problem.
I have now implemented something with Yahoo finance, which gives me the valid option chains.
Then I create all needed contracts and serialize them to a file(because it takes an enormous amount of time to get them).
That way, the cached contracts can be reloaded and only new expiration chains well need to be added.
But still, why do we need to do this?
We deserve better from IB, since all this data is available in the option trader window.

I now struggle in reveiving the greeks for the options. Are they even there when markets are closed?


Re: Getting last Expired Option Price

 

Hi Mattias & Alexandre,
To get the underlying closing price, do you use reqHistoricalData for the expiration date?
Using Regular Trading Hours (RTH) = 1 or 0 ?
The closing price of the underlying for option calculation is normally based on the price AFTER market close, so RTH=0
Please confirm.


Re: Request for options contracts per expirtation date

 

Hi! well, again and again the same pain in the... Api about reqSecDefOptParams
/g/twsapi/message/46566
/g/twsapi/message/42241
Welcome to the party!


Re: Anyone upgraded Java version for TWS on Linux?

 

Hi,
I believe TWS comes with its own Java built into the installer.? I'm pretty sure I use that one.? TWS has never been the best written app but if you stick with stable its OK.?

I believe there are some weird security things built into IB's JVM they distribute which is why you can't make a stock JVM work.? I think if you dig into the IB JVM, you will probably find some security configs in the jre/lib directory (or inside of one of its sub-directories) that allow TWS to work.? Perhaps jre/lib/security/java.policy has been altered to only run IB signed jars or that TWS needs a custom Java security policy.? If you really want to hammer away at this that's where I would look.? As for the performance issues with TWS, perhaps the screen config you have is too busy or perhaps its using up all the pacing capacity and that's causing issues with refreshing the screen.? I have a hard time believing that upgrading the JVM would help.? The Java 8 JVM is really mature and the changes to it are mostly about very minor things that probably wouldn't impact the performance of a GUI app.

Hunter

On Monday, March 15, 2021, 11:32:32 AM PDT, John <jpelly@...> wrote:


No such luck for me - I let that error dialog sit and nothing happens; just sits there forever (rest of TWS will not start up).

I'd like to run TWS on my local Linux laptop, so hopefully someone else comments on this thread with some additional suggestions.

Correction to original message: I'm running Linux Mint 20.

Thanks all
John


On Mon, Mar 15, 2021 at 1:04 PM Ray Racine <ray.racine@...> wrote:
On latest Fedora Linux and I'm getting the exact same error as of today as well.? TWS ultimately starts up fine however.? I assume just not on the latest update :).

With regard to performance,? I've never had to do anything beyond adjusting the Max Heap. ? Performance is retalive, but I have some pretty big watch lists, charts with studies, plus API clients and performance has been fine for me.

I do get quite a few UI issues with menus and mouse pointing (not calculating the X-Y position correctly mostly) when I run under Sway / Wayland.??? The occasional annoying UI issues with XMonad / XOrg.? And no real UI issues to speak of with Gnome - Xorg or Wayland.

On Mon, Mar 15, 2021 at 11:52 AM John <jpelly@...> wrote:
I'm experiencing pretty bad performance of TWS GUI on Linux (Mint 19). I've tried to upgrade Java version that TWS uses, thinking that will help.

However I'm unable to change versions. Reading through the tws shell script, it looks like it pins the JRE version at 1.8.0_152. If I remove the check in the shell script and try to upgrade to openjdk version 1.8.0_282 (upgraded patch level), TWS won't start - says "we are unable to update the platform due to a networking error" (see attached screenshot)

Has anyone successfully been able to do this? It looks like you can upgrade Java versions on Windows so I was hoping to do it on Linux.

jws networking error.png


Re: Anyone upgraded Java version for TWS on Linux?

 

No such luck for me - I let that error dialog sit and nothing happens; just sits there forever (rest of TWS will not start up).

I'd like to run TWS on my local Linux laptop, so hopefully someone else comments on this thread with some additional suggestions.

Correction to original message: I'm running Linux Mint 20.

Thanks all
John


On Mon, Mar 15, 2021 at 1:04 PM Ray Racine <ray.racine@...> wrote:
On latest Fedora Linux and I'm getting the exact same error as of today as well.? TWS ultimately starts up fine however.? I assume just not on the latest update :).

With regard to performance,? I've never had to do anything beyond adjusting the Max Heap. ? Performance is retalive, but I have some pretty big watch lists, charts with studies, plus API clients and performance has been fine for me.

I do get quite a few UI issues with menus and mouse pointing (not calculating the X-Y position correctly mostly) when I run under Sway / Wayland.??? The occasional annoying UI issues with XMonad / XOrg.? And no real UI issues to speak of with Gnome - Xorg or Wayland.

On Mon, Mar 15, 2021 at 11:52 AM John <jpelly@...> wrote:
I'm experiencing pretty bad performance of TWS GUI on Linux (Mint 19). I've tried to upgrade Java version that TWS uses, thinking that will help.

However I'm unable to change versions. Reading through the tws shell script, it looks like it pins the JRE version at 1.8.0_152. If I remove the check in the shell script and try to upgrade to openjdk version 1.8.0_282 (upgraded patch level), TWS won't start - says "we are unable to update the platform due to a networking error" (see attached screenshot)

Has anyone successfully been able to do this? It looks like you can upgrade Java versions on Windows so I was hoping to do it on Linux.

jws networking error.png


Re: Anyone upgraded Java version for TWS on Linux?

 

On latest Fedora Linux and I'm getting the exact same error as of today as well.? TWS ultimately starts up fine however.? I assume just not on the latest update :).

With regard to performance,? I've never had to do anything beyond adjusting the Max Heap. ? Performance is retalive, but I have some pretty big watch lists, charts with studies, plus API clients and performance has been fine for me.

I do get quite a few UI issues with menus and mouse pointing (not calculating the X-Y position correctly mostly) when I run under Sway / Wayland.??? The occasional annoying UI issues with XMonad / XOrg.? And no real UI issues to speak of with Gnome - Xorg or Wayland.


On Mon, Mar 15, 2021 at 11:52 AM John <jpelly@...> wrote:
I'm experiencing pretty bad performance of TWS GUI on Linux (Mint 19). I've tried to upgrade Java version that TWS uses, thinking that will help.

However I'm unable to change versions. Reading through the tws shell script, it looks like it pins the JRE version at 1.8.0_152. If I remove the check in the shell script and try to upgrade to openjdk version 1.8.0_282 (upgraded patch level), TWS won't start - says "we are unable to update the platform due to a networking error" (see attached screenshot)

Has anyone successfully been able to do this? It looks like you can upgrade Java versions on Windows so I was hoping to do it on Linux.

jws networking error.png