开云体育

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

Re: TestCppClient doesn't compile for Ubuntu 20.04 (focal)

 

You should be able to download the code from and build the library yourself.

I needed to do this for a Debian Buster system I'm running at it was rather straightforward.


Re: TestCppClient doesn't compile for Ubuntu 20.04 (focal)

 

This fix works but only if I upgrade to Ubuntu 22.04.3 LTS. libintelrdfpmath-dev isn't available for older versions of Ubuntu.


Re: TestCppClient doesn't compile for Ubuntu 20.04 (focal)

 

API_Version=10.19.01
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0


Re: TWS api multiple similar orders submission delays

 

? @amar
I do not wait for anything, the only thing I do is my order send function is synchronized:

static synchronized void sendOrderAndIncrement(Order order, EWrapperImpl wrapper, Contract contract, EClientSocket m_client, int tickerId) {
?
? ? ? ? if (order != null) {
? ? ? ? ? ? int currentOrder = wrapper.currentOrderId;
? ? ? ? ? ? LAST_ORDER = currentOrder;
? ? ? ? ? ? m_client.placeOrder(currentOrder, contract, order);
? ? ? ? ? ? wrapper.currentOrderId++;
? ? ? ? ? ? logAndPrintInfo(contract.symbol() + " PLACING " + tickerId + " ORDER - COMPLETED");
? ? ? ? } else {
? ? ? ? ? ? log("ORDER IS NULL FOR: " + contract.symbol());
? ? ? ? }
?
? ? }

This happens all within 5ms for all orders, but first??"open order:" message appears only after 1.5s+

I do not trade stocks, but options, maybe thats a difference??

This is my api client log for one option:

21:59:55:624 <- 3-44-0-SPX-OPT-20231005-4250.0-C-100-SMART--USD--SPXW---BUY-3.0-LMT-15.2--DAY---O-0--1-0-0-0-0-0-0-0--1.7976931348623157E308--------0---1-0---0---0-0--0------0--2147483647---0-2147483647----------0---0-0---0--0-0-0-0--1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-0---------0-0-0--

21:59:56:835 -> --v5-44-652138644-SPX-OPT-20231005-4250-C-100-SMART-USD-SPXW? 231005C04250000-SPXW-BUY-3-LMT-15.2-0.0-DAY--UXXXXX-O-0--36-1246696869-0-0-0--1246696869.0/UXXXXX/100----------0---1-0-------0-0-0--3-0-0--0-0--0-None--0----?-0-0--0-0------0-0-0-----0--IB-0-0--0-0-PreSubmitted-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308------0-0-0-None-1.7976931348623157E308-6.2-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-0----0-1-0-0-0-----3-44-PreSubmitted-0-3-0-1246696869-0-0-36--0-


21:59:58:159 -> --r5-44-652138644-SPX-OPT-20231005-4250-C-100-SMART-USD-SPXW? 231005C04250000-SPXW-BUY-3-LMT-15.2-0.0-DAY--UXXXXX--0--36-1246696869-0-0-0--1246696869.0/UXXXXX/100----------0---1-0-------0-0-0--3-0-0--0-0--0-None--0----?-0-0--0-0------0-0-0-----0--IB-0-0--0-0-Submitted-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308------0-0-0-None-1.7976931348623157E308-6.2-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-1.7976931348623157E308-0----0-1-0-0-0----*3-44-Submitted-0-3-0-1246696869-0-0-36--0-



so after I call placeOrder, it takes 1200ms to make it into PreSubmitted status, then another 1300ms to move it into submitted status


TestCppClient doesn't compile for Ubuntu 20.04 (focal)

 

Running make on the newly-downloaded TWS SDK produces the following:

g++ -pthread -Wall -Wno-switch -Wpedantic -Wno-unused-function -std=c++11 -I../../../source/cppclient/client -I../../../source/cppclient ../../../source/cppclient/client/*.cpp ./*.cpp ../../../source/cppclient/client/lib/libbid.a -oTestCppClientStatic
collect2: fatal error: ld terminated with signal 11 [Segmentation fault], core dumped
compilation terminated.
/home/linuxbrew/.linuxbrew/bin/ld: BFD (GNU Binutils) 2.37 assertion fail reloc.c:8489
../../../source/cppclient/client/lib/libbid.a(bid64_string.o):bid64_string.c:(.pdata+0x0): dangerous relocation: make: *** [makefile:11: TestCppClientStatic] Error 1

I've seen a few threads (i.e. this and ) that recommend swapping the IBKR bundled libbid.a or libbid.so with libintelrdfpmath-dev. The only problem is that that library isn't available for Ubuntu 20.04 (focal). Is there any way I can build with something else, or with the attached libraries?


Re: How to get Uptick Status through API

Erez Kaplan
 

Hi,

Has there been some update to this limitation, or is there a way?

to get Uptick Status through API?

Cheers, Erez

?


Re: Implied Volatility of an EXPIRY

 

开云体育

Sorry, I just realise that I misread your mail.

My other question is what exactly is returned when you request historical volatility from the API?? My assumption is that it is something similar to the VIX calculation where the two expiries closet to 30 days are interpolated to that 30 day mark, although I haven't tried to do a sample calculation to confirm.


I thought that you were asking about IV, but you are talking about historical volatility. You get the 21 trading trailing days return standard deviation. I have calculated and it matches.


On 4 Oct 2023, at 20:46, Gonzalo Saenz <yo@...> wrote:

Hi,

My other question is what exactly is returned when you request historical volatility from the API?? My assumption is that it is something similar to the VIX calculation where the two expiries closet to 30 days are interpolated to that 30 day mark, although I haven't tried to do a sample calculation to confirm.

It is returning the 30~DTE at the money IV, I haven’t found any confirmation but I’m pretty sure. It’s not the VIX calculation because the VIX calculation takes into account skew.

I think I've convinced myself that the way the "IV of a specific chain" is calculated is the same way that the VIX is calculated as illustrated in this white paper:??There is no Black-Scholles of any sort going on with this calculation.?

Wha do you mean by IV of a specific chain? You get IB by asset, expiration and strike, that’s calculated using a model, I don’t know ?what model uses IBKR, but it’s not a “model free” calculation like VIX

On 3 Oct 2023, at 10:25, ebtrader <jsiddique@...> wrote:

I would save options prices each week before they expire as well as the underlying prices.? That way you should be able to get a historical IV over time using Black Scholes to back into to the IV.? If you need help with the code, feel free to email me and I am happy to help with that.?

Ebtrader

On Mon, Oct 2, 2023 at 9:07 AM Michael Sutton <mikesutton@...> wrote:
Resurrecting an old thread as I have a similar question.

I think I've convinced myself that the way the "IV of a specific chain" is calculated is the same way that the VIX is calculated as illustrated in this white paper:??There is no Black-Scholles of any sort going on with this calculation.?

That means that from the API, it's possible to calculate the _current_ IV of a specific expiry; but not the _history_ of the IV that's shown in Volatility Lab. Has anyone figured out a way to do that?? (I suspect the answer is it's not possible.? If that's the case, are people using IVolatility.com, which looks like there is no longer any free component.? What about??)

My other question is what exactly is returned when you request historical volatility from the API?? My assumption is that it is something similar to the VIX calculation where the two expiries closet to 30 days are interpolated to that 30 day mark, although I haven't tried to do a sample calculation to confirm.





Re: Implied Volatility of an EXPIRY

 

开云体育

Hi Bart D,

I think there is an error there. HV stands for historical volatility, and historical volatility is calculated as the standard deviation of returns.?

So you get price data, for TRADES

bars = reqHistoricalData(contract,endDateTime="",durationStr="2 Y", barSizeSetting="1 day", whatToShow=“TRADES", useRTH=True, formatDate=1, keepUpToDate=False,chartOptions=[])

Then HV is?

log_returns =?np.log(df.close?/?df.close.shift(1)).dropna()
hv = log_returns.rolling(window=21).std() * np.sqrt(252)

Hope it makes sense.


On 4 Oct 2023, at 07:43, Bart D via groups.io <bart@...> wrote:

You can get IVs directly from the API and calculate any HV you want from that.?
Here is how I calculate various HVs, eg for S&P500 (I am using ib_insync so some calls may look different than the native API calls):
  • define the contract as the spx index: spx = Index(symbol="SPX", exchange="CBOE")
  • request from the API the sequence of IV data that is calculated off the options on that contract, eg: bars = reqHistoricalData(contract,endDateTime="",durationStr="2 Y", barSizeSetting="1 day", whatToShow="OPTION_IMPLIED_VOLATILITY", useRTH=True, formatDate=1, keepUpToDate=False,chartOptions=[])
  • Now you have bars over the past 2 years with 1 day resolution, and you can calculate any HV you want eg by putting it in a Pandas dataframe, eg for 10-day HV:
  • ivDF=pd.DataFrame(bars)?
  • ivDF=ivDF.drop(columns=[“Date”]) # can’t apply ‘rolling’ over a string
  • hv10DF=ivDF.rolling(10).mean().trail(timeframeInTradingDays)

How IB calculates the option-implied-volatility is not clear to me but in any case the various HVs I calculate correspond well to what is shown in TWS.





On Oct 3, 2023 at 1:26?AM -0700, ebtrader via groups.io <jsiddique@...>, wrote:
I would save options prices each week before they expire as well as the underlying prices.? That way you should be able to get a historical IV over time using Black Scholes to back into to the IV.? If you need help with the code, feel free to email me and I am happy to help with that.?

Ebtrader

On Mon, Oct 2, 2023 at 9:07 AM Michael Sutton <mikesutton@...> wrote:
Resurrecting an old thread as I have a similar question.

I think I've convinced myself that the way the "IV of a specific chain" is calculated is the same way that the VIX is calculated as illustrated in this white paper:??There is no Black-Scholles of any sort going on with this calculation.?

That means that from the API, it's possible to calculate the _current_ IV of a specific expiry; but not the _history_ of the IV that's shown in Volatility Lab. Has anyone figured out a way to do that?? (I suspect the answer is it's not possible.? If that's the case, are people using IVolatility.com, which looks like there is no longer any free component.? What about ?)

My other question is what exactly is returned when you request historical volatility from the API?? My assumption is that it is something similar to the VIX calculation where the two expiries closet to 30 days are interpolated to that 30 day mark, although I haven't tried to do a sample calculation to confirm.




Re: Implied Volatility of an EXPIRY

 

开云体育

Hi,

My other question is what exactly is returned when you request historical volatility from the API?? My assumption is that it is something similar to the VIX calculation where the two expiries closet to 30 days are interpolated to that 30 day mark, although I haven't tried to do a sample calculation to confirm.

It is returning the 30~DTE at the money IV, I haven’t found any confirmation but I’m pretty sure. It’s not the VIX calculation because the VIX calculation takes into account skew.

I think I've convinced myself that the way the "IV of a specific chain" is calculated is the same way that the VIX is calculated as illustrated in this white paper:??There is no Black-Scholles of any sort going on with this calculation.?

Wha do you mean by IV of a specific chain? You get IB by asset, expiration and strike, that’s calculated using a model, I don’t know ?what model uses IBKR, but it’s not a “model free” calculation like VIX

On 3 Oct 2023, at 10:25, ebtrader <jsiddique@...> wrote:

I would save options prices each week before they expire as well as the underlying prices.? That way you should be able to get a historical IV over time using Black Scholes to back into to the IV.? If you need help with the code, feel free to email me and I am happy to help with that.?

Ebtrader

On Mon, Oct 2, 2023 at 9:07 AM Michael Sutton <mikesutton@...> wrote:
Resurrecting an old thread as I have a similar question.

I think I've convinced myself that the way the "IV of a specific chain" is calculated is the same way that the VIX is calculated as illustrated in this white paper:??There is no Black-Scholles of any sort going on with this calculation.?

That means that from the API, it's possible to calculate the _current_ IV of a specific expiry; but not the _history_ of the IV that's shown in Volatility Lab. Has anyone figured out a way to do that?? (I suspect the answer is it's not possible.? If that's the case, are people using IVolatility.com, which looks like there is no longer any free component.? What about??)

My other question is what exactly is returned when you request historical volatility from the API?? My assumption is that it is something similar to the VIX calculation where the two expiries closet to 30 days are interpolated to that 30 day mark, although I haven't tried to do a sample calculation to confirm.




Re: Implied Volatility of an EXPIRY

 

开云体育

You can get IVs directly from the API and calculate any HV you want from that.?
Here is how I calculate various HVs, eg for S&P500 (I am using ib_insync so some calls may look different than the native API calls):
  • define the contract as the spx index: spx = Index(symbol="SPX", exchange="CBOE")
  • request from the API the sequence of IV data that is calculated off the options on that contract, eg: bars = reqHistoricalData(contract,endDateTime="",durationStr="2 Y", barSizeSetting="1 day", whatToShow="OPTION_IMPLIED_VOLATILITY", useRTH=True, formatDate=1, keepUpToDate=False,chartOptions=[])
  • Now you have bars over the past 2 years with 1 day resolution, and you can calculate any HV you want eg by putting it in a Pandas dataframe, eg for 10-day HV:
  • ivDF=pd.DataFrame(bars)?
  • ivDF=ivDF.drop(columns=[“Date”]) # can’t apply ‘rolling’ over a string
  • hv10DF=ivDF.rolling(10).mean().trail(timeframeInTradingDays)

How IB calculates the option-implied-volatility is not clear to me but in any case the various HVs I calculate correspond well to what is shown in TWS.





On Oct 3, 2023 at 1:26?AM -0700, ebtrader via groups.io <jsiddique@...>, wrote:

I would save options prices each week before they expire as well as the underlying prices.? That way you should be able to get a historical IV over time using Black Scholes to back into to the IV.? If you need help with the code, feel free to email me and I am happy to help with that.?

Ebtrader

On Mon, Oct 2, 2023 at 9:07 AM Michael Sutton <mikesutton@...> wrote:
Resurrecting an old thread as I have a similar question.

I think I've convinced myself that the way the "IV of a specific chain" is calculated is the same way that the VIX is calculated as illustrated in this white paper:??There is no Black-Scholles of any sort going on with this calculation.?

That means that from the API, it's possible to calculate the _current_ IV of a specific expiry; but not the _history_ of the IV that's shown in Volatility Lab. Has anyone figured out a way to do that?? (I suspect the answer is it's not possible.? If that's the case, are people using IVolatility.com, which looks like there is no longer any free component.? What about ?)

My other question is what exactly is returned when you request historical volatility from the API?? My assumption is that it is something similar to the VIX calculation where the two expiries closet to 30 days are interpolated to that 30 day mark, although I haven't tried to do a sample calculation to confirm.



Re: How to estimate order execution impact on excess liquidity?

 

I get the margin delta with "Check Margin" in tws or??IBApi.Order.WhatIf. Also,?using a limit order I can get?cash balance delta. My question was if the formula for?excess_liquidity_delta was correct.


Re: How to estimate order execution impact on excess liquidity?

 

Does the "Check Margin" feature in the TWS Order Entry form give you the information you are looking for?

In that case, take a look at the documentation for via TWS API and the flag of the class.

闯ü谤驳别苍


On Mon, Oct 2, 2023 at 05:43 PM, Lipp F. wrote:
Assuming that an order execution has $cash_balance_delta?impact on cash balance and $margin_delta?on margin, is it correct to assume that the impact on?excess liquidity is
$excess_liquidity_delta =? $cash_balance_delta - $margin_delta?
Is this accurate? Is there any way to have IB estimate?excess liquidity prior to the order execution instead? TIA.


Re: Implied Volatility of an EXPIRY

 

I would save options prices each week before they expire as well as the underlying prices.? That way you should be able to get a historical IV over time using Black Scholes to back into to the IV.? If you need help with the code, feel free to email me and I am happy to help with that.?

Ebtrader

On Mon, Oct 2, 2023 at 9:07 AM Michael Sutton <mikesutton@...> wrote:
Resurrecting an old thread as I have a similar question.

I think I've convinced myself that the way the "IV of a specific chain" is calculated is the same way that the VIX is calculated as illustrated in this white paper:??There is no Black-Scholles of any sort going on with this calculation.?

That means that from the API, it's possible to calculate the _current_ IV of a specific expiry; but not the _history_ of the IV that's shown in Volatility Lab. Has anyone figured out a way to do that?? (I suspect the answer is it's not possible.? If that's the case, are people using IVolatility.com, which looks like there is no longer any free component.? What about ?)

My other question is what exactly is returned when you request historical volatility from the API?? My assumption is that it is something similar to the VIX calculation where the two expiries closet to 30 days are interpolated to that 30 day mark, although I haven't tried to do a sample calculation to confirm.


Re: C++ preventing EReader reading when socket is closed

 

Hey, if you feel that strongly about it and have the time then go ahead and make the pull request. In the worst case scenario it'll be good practice, a learning experience and software development lesson.

?袄冲(ツ)冲/?


Re: C++ preventing EReader reading when socket is closed

 

@Gordon, @Buddy,

"mountain"?? : hardly. There is no impact to existing code with this proposed change (tested). I am simply adding functionality that allows users to manually stop the thread that they started manually.? In fact I can leave the destructor of EReader the same as it was without swapping anything and just create a new function "stop()" which will allow users to disconnect cleanly if they desire to,

(Note to Gordon on your concern about the initial use of EReader during the connection, this is what the "if (m_hReadThread)" statement takes care of in the original TWS API code which is still there in my proposed change.)

Let me remind you that the current situation results in a 509 error, which is basically the API's way of saying something bad happened and I do not know what it is. Your code must be bad, which is not the case here. It is the API code that is bad. Now that we know what causes it, we can probably live with it and safely ignore it, but personally I don't like that sort of situation. If the developers intended to use this way of disconnecting the socket, then they should have properly taken care of the error message it creates. The fact that they didn't means that what they coded was unintentional and therefore is clearly a bug. A bug needs to be fixed.

However, I realise changing (adding to) the interface will make the C++ interface different than the other languages which is probably not desirable.

The only way to correct this and make the existing interface the same is to create an EMutex variable which is shared by both EReader and EClientSocket (suggest using a composition pattern here). Changes to the m_isAlive variable need to be locked by the mutex as well as inside the "while(m_isAlive)" loop. This is a larger code change and needs more care to be done right but it is not that difficult and I have implemented it successfully as well. In fact using EMutex here does not slow the loop down (checking the mutex is as simple as reading a flag) since the only time it is ever locked is when we are disconnecting which means speed is not a "critical trading" issue.

I agree it is good to have this thread capture this discussion for the record but I will definitely raise this problem to the coders as I still firmly believe this to be a bug. I do not wish to have to fix this myself every time there is a new API update.


Re: C++ preventing EReader reading when socket is closed

 

Yeah, I see most of this as making a mountain out of a molehill. It strikes me, largely, as a misunderstanding; much like issuing a SIGKILL but expecting SIGTERM.

If you are familiar with unix signal processing you know that SIGKILL is immediately forced upon the process by the OS and the process my be left in an untidy state. SIGTERM, on the other hand, gives the process an opportunity to clean up and exit gracefully.

Since the API code is readily available, folks can implement their own version of EReader which suits their preference in this regard. Therefore the distinction becomes moot.

There may be some chance of acceptance for a minor pull request which, e.g., swaps the calls to the thread join and socket disconnect. But, even this could be seen as a dog chasing it's own tail since the argument would merely come down to the dislike of a default.

Moreover though, when you consider that the code for EReader hasn't changed in years... even a small change becomes unlikely. And, the suggestion of anything "major", like an intermediate "stop" method, becomes far fetched IMHO.

It's certainly an interesting nuance but probably better addressed by documentation alone. At least we have this thread of conversation now for reference :-)


How to estimate order execution impact on excess liquidity?

 

Assuming that an order execution has $cash_balance_delta?impact on cash balance and $margin_delta?on margin, is it correct to assume that the impact on?excess liquidity is
$excess_liquidity_delta =? $cash_balance_delta - $margin_delta?
Is this accurate? Is there any way to have IB estimate?excess liquidity prior to the order execution instead? TIA.


Re: C++ preventing EReader reading when socket is closed

 

I like evolution, so I may be wrong but here I am not sure I would flag this as a bug.
Just out of curiosity what undesirable side effect the current implementation generate? ?

Some food for thoughts:
Theses are tricky parts as it require tracking all threads together to do proper analysis. again I won't pretend I know all the implications.
I use it in C++/Win32 and it work really fine and fast (async) without any issues that worth a change from my perspective (IMHO).

Because:
1- If I ask a "disconnect" it's to be acted upon immediately I don't want any new data to be processed.
Exiting gracefully and fast a mutithread application can become a tricky process;
I am ready to live with recv receiving remnant of previous telco. It's a philosophical/aesthetic issue I agree,
in an ideal world, we are 100% sure that the last transaction was completed. yes. but ... we are not (at least I am not ) in this ideal world.

Also the "error" system of IB have to be seen as a client messaging service Same for WIN32 where GetLastError() is sometime used as a signal (Example when looking for enumeration of files, this is the only way to get a "no more file" info.)
The OS handle gracefully zombie packet on close socket, so no harm. (surely not the only apps that do that).
Codes are used to that, just look at handleSocketError you see a hard errno = 0; typical of way to reset it without concerns for unhandled error.

2- Related to #1 above, Putting the WaitForSingleObject(m_hReadThread, INFINITE); before eDisconnect may execute a last putMessageToQueue.
While the basic supplied code will do a hard beak, the while() loop with m_buf.size==0 , exit because m_isAlive==false, without executing anything else.

3- Notice that a EReader object is used during connection as a limited life (scoped) heap object to fetch server protocol revision in a synchronous mode, and to call startApi()
(I am not aware of what this proc? does but by the name of it I would avoid missing this call) this looks like a "trick" to overcome an un-anticipated evolution of the protocol,
See EClientSocket::eConnectImpl(....)
Actions happening in the dctor are then to be taken carefully.

I would agree that the inversion of code lines you suggested might not affect the behavior at it's synchronous, (m_hReadThread=0) , that you ought to get the protocol revision and I don't even see how you can abort this single message operation anyway. (aside of a socket shutdown) never see a need.

As buddy point it out:
- it show that playing with the dctor of EReader may have side effect.
In particular there are legacy code without a call to a ->stop(), ?? it's implicit in ~dctor and somewhere does the job.
- Just to mention, the m_signal used for this Connect EReader is shared by this ephemeral EReader, you most probably will use the same for your main EReader.? It need care, there are people who use more than one EReader on same m_signal


Re: Implied Volatility of an EXPIRY

 

Resurrecting an old thread as I have a similar question.

I think I've convinced myself that the way the "IV of a specific chain" is calculated is the same way that the VIX is calculated as illustrated in this white paper:??There is no Black-Scholles of any sort going on with this calculation.?

That means that from the API, it's possible to calculate the _current_ IV of a specific expiry; but not the _history_ of the IV that's shown in Volatility Lab. Has anyone figured out a way to do that?? (I suspect the answer is it's not possible.? If that's the case, are people using IVolatility.com, which looks like there is no longer any free component.? What about getvolatility.com?)

My other question is what exactly is returned when you request historical volatility from the API?? My assumption is that it is something similar to the VIX calculation where the two expiries closet to 30 days are interpolated to that 30 day mark, although I haven't tried to do a sample calculation to confirm.


Re: APIPending status?

Colin Beveridge
 

Thanks, 闯ü谤驳别苍, being less of a doofus with the contract definition seems to have helped.
?
(I had seen that documentation -- I stand by my opinion that it could be better documented; I think that "Uncommonly received" is a less helpful message than, say, "if this occurs repeatedly, check you are sending a properly-defined contract". In any case, I hope future people hitting the same problem[^0] will find this thread and find it helpful.)

[^0]: Even if it's just me, I'm ok with that.