开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Re: reqHistoricalData returns low volumes

 

Thank you Gordon and Jurgen for such a complete answer. I did do a thread search but seem to be going through a streak of failure. I didn't spot the CLEARLY written topic, nor did I read the IB docs. I have no excuses and am sorry to been so remiss in my approach here. I am using 10.31.1m ( tws workstation).

Please take the word duplicate out of the topic of my post as that's ANOTHER failure to add to the list :-)

Thank you both SO MUCH for taking the time to do my thinking for me and I'll try not to do this again. cp


Re: reqHistoricalData returns low volumes

 

Gordon is correct. You need to multiply the volume by 100 since you seem to be getting "volume in lots of 100" and not "volume in shares".

TWS API has the feature HDMS_MARKET_DATA_IN_SHARES since TWS API version 185 (that is TWS/IBGW version 10.18.02). In order to receive shares:
  • your client needs to use TWS API 185 or higher
  • your TWS/IBGW needs to be 10.18.2 or higher
  • in the API Global Configuration you need to deselect the option "Send market data in lots for US stocks ..."
  • TWS/IBGW need to restart after deselecting the option
That will move the decimal point to the right, but you will still see a (significant) discrepancy between the full day historical volume and other total volume reports. We had a thread about this in June with the title "How can you explain the substantially lower trading volume shown in IBKR?" Among the posts is one that I made where I compared the historical volume with the tick-by-tick volume for one AAPL day. Here the highlights:
  • Historical volume basically contains only reportable trades from the regular and extended trading sessions. There are lot's of trades you can see in tick-by-tick data that are not reportatble and therefore not included in the historical volume.
  • A big chunk of non-reportable volume comes from the auctions at market open and close, which are technically not part of the trading session. For AAPL today, 1Mio shares were traded with MOO orders and 4.8Mio shares for MOC.
  • Odd lots (a little simplified trades with a volume that is not a multiple of 100) are not reportable either

Take a look at my post since it has a detailed tally for one AAPL day.

闯ü谤驳别苍
?
PS. If you don't mind, I'll rename this thread when posts have settled down a little and take the "duplicate lines" out of the title.
?
On Wed, Oct 16, 2024 at 03:30 PM, comicpilsen wrote:

Hi Gordon thank you for taking an interest. so right now AAPL volume is Volume 33,719,016 yet IB has AAPL 255589.4 so does that map onto your thinking about the lower numbers please? cp


Re: reqHistoricalData returns low volumes

 

Hi Gordon thank you for taking an interest. so right now AAPL volume is Volume 33,719,016 yet IB has AAPL 255589.4 so does that map onto your thinking about the lower numbers please? cp


Re: reqHistoricalData returns low volumes

 

IBKR report volume in full lot (100's), I would even say "of full lots", so in general IBKR volumes is lower than other feeds
?
Note from my experience:
It may also impact IBKR VWAP computation on low volume tickers.(more odd lots)
IBKR VWAP is slightly different than what others feeds do 'suggest' (impact is cumulative, hence more difference on VWAP by the end of a day)?
?


Re: reqHistoricalData returns low volumes

 

sorry to be so dense but I'm missing something basic here. I'm still getting low volumes irrespective of useRTH=0 or useRTH=1 . I can run it at 3am or 9pm and I get the same results. What am I missing???? I just want the OHLC for stocks. I know it's my code but I'm not seeing it.

Symbol	Date	Open	High	Low	Close	Volume
AAPL	20241011	228.5	229.55	227.2	227.4	234818.72
AAPL	20241014	228	231.73	227.12	231.3	255318.34
AAPL	20241015	231.34	237.49	230.3	234.1	427936.92
AAPL	20241016	234.34	234.44	229.84	231.65	184033.79

the contract looks like this

0,AAPL,STK,,0,,,SMART,,USD,,,False,,,,combo:

and the function looks like this

def request_historical_data(self):
        if self.tickers:
            ticker_info = self.tickers.pop(0)  
            ticker = ticker_info['Ticker']  
            logger.info(f"Requesting historical data for {ticker}...")

            contract = Contract()
            contract.symbol = ticker
            contract.secType = ticker_info['Type']  
            contract.currency = "USD"
            contract.exchange = ticker_info['Exchange']  
            print(contract)

            if ticker and ticker_info['Type'] and ticker_info['Exchange']:
                self.req_id_to_symbol[self.req_id] = ticker
          

                try:
                    self.reqHistoricalData(  
                        reqId=self.req_id,   
                        contract=contract,
                        endDateTime='',
                        durationStr='3 Y',
                        barSizeSetting='1 day',
                        whatToShow='TRADES',
                        useRTH=0,
                        formatDate=1,
                        keepUpToDate=False,
                        chartOptions=[]
                    )
                    self.req_id += 1 
                except Exception as e:
                    logger.error(f"Request failed for {ticker}: {e}")
                    self.request_historical_data()  # Retry with the next ticker
            else:
                logger.error(f"Invalid ticker information for {ticker}. Skipping...")
                self.request_historical_data()  # Move on to the next ticker
        else:
            logger.error("All tickers processed.")
            self.disconnect()


Re: Procedure Declaration does not match Error

 

开云体育

You can message me directly if you wish.? They usually don't like programming specific questions, just TWS general issues.
I have done a lot of VBA and you can set a break point i? the code and then step through using F8.

Mel




-------- Original message --------
From: "vicente via groups.io" <vicente@...>
Date: 2024-10-15 3:37 p.m. (GMT-08:00)
Subject: [TWS API] Procedure Declaration does not match Error

Hello everyone,??
?
Recently I inherited a spreadsheet that connects to the TWS API and have been trying to get it to work as intended I keep getting the error message pictured below and am at a loss on how to fix it.? I am completely new to dealing with VBA and wouldn't know how to fix / address this issue.?
?
Thanks
?
?


Procedure Declaration does not match Error

 

Hello everyone,??
?
Recently I inherited a spreadsheet that connects to the TWS API and have been trying to get it to work as intended I keep getting the error message pictured below and am at a loss on how to fix it.? I am completely new to dealing with VBA and wouldn't know how to fix / address this issue.?
?
Thanks
?
?


Re: Close Orders Rejected

 

I think bespalex is on the right path. Do you know what your net beta exposure is? There's a Risk Navigator tool that will show you how your positions add up. Managing margin for multiple instruments is not trivial as it is not additive.
?
I would recommend giving this a go: https://www.interactivebrokers.com/campus/trading-course/ibkrs-risk-navigator/


Re: Close Orders Rejected

 

This can happen due to portfolio margin calculation, if for example you hold long QQQ and short NVDA, that will have a lower margin requirement than naked short NVDA, as the positions partly cancel each other's risk.
Also, as noted above, when you sell NVDA short, you got your cash position increase (you just sold shares you borrowed), and if later you somehow tied this cash or used it, you can't close your short as it requires buying shares back with cash.


Re: Close Orders Rejected

 

Sorry: 10-11 AM Session Hours,?? 5-6 in my timezone


Re: Close Orders Rejected

 

It is my intuition as well, that closing a position reduces risk and something isn't right. I only trade during Regular Hours, this was between 5-6 but I have daily examples
like this.


Re: duplicate rows from a simple api request reqHistoricalData

 

Hi both Thanks for the replies and going the extra yard. I checked both the raw ( pre merge) csv's and the duplicates are there. As Jurgen hasn't seen this behavior I am CERTAIN that the problem is my end. I'll turn OFF after market and look at this again ( thanks patrick) . I strongly suspect a programming error on my part. Thank you for confirming that this is aberrant behavior.

cp


Re: Close Orders Rejected

 

Normally you should always be able to reduce risk (including closing short positions). What time of day were the trades at? Maybe you were outside RTH (Regular Trading Hours) - there's a flag for that?


Re: duplicate rows from a simple api request reqHistoricalData

 

"Implied_Volatility" is not part of the "TRADES" historical data Bar object, so you must be doing some data merging. And the values in that column are too large for NVDA VWAPs for those days. Any chance the second record each day gets generated within your data processing logic?

I grabbed the same data, got only one record per day, and the records I received are identical to your #71612, #71614, and #71616 (after rounding to two digits).

Also, what exactly does the contract look like ("Exchange", "Primary Exchange", others).

闯ü谤驳别苍

?
On Sat, Oct 12, 2024 at 07:31 PM, comicpilsen wrote:

    Symbol     Date     Implied_Volatility     Open     High     Low     Close     Volume
71611     NVDA     2021-10-13     33.684118     20.94     20.99     20.72     20.94     66359.50
71612     NVDA     2021-10-13     33.684118     20.92     20.99     20.71     20.94     1070657.50
71613     NVDA     2021-10-14     32.698311     21.29     21.75     21.13     21.75     78571.30
71614     NVDA     2021-10-14     32.698311     21.27     21.76     21.12     21.75     1575756.20
71615     NVDA     2021-10-15     30.701298     21.81     21.93     21.66     21.86     80737.50
71616     NVDA     2021-10-15     30.701298     21.78     21.93     21.66     21.86     1486919.50


Re: duplicate rows from a simple api request reqHistoricalData

 

Comic, I'm just guessing but it looks to me like the rows aren't exactly identical. The highs and lows are different and the volumes. I am thinking the lower volume rows are after-market trading as you have specified "Trades".


On Sat, Oct 12, 2024 at 5:32?PM comicpilsen via <comicpilsen=[email protected]> wrote:

I'm sending

 self.reqHistoricalData(  # 
                        reqId=self.req_id,   # Use a unique request ID
                        contract=contract,
                        endDateTime='',
                        durationStr='3 Y',
                        barSizeSetting='1 day',
                        whatToShow='TRADES',
                        useRTH=1,
                        formatDate=1,
                        keepUpToDate=False,
                        chartOptions=[]

and getting two rows per day all the time but don't know why?

    Symbol     Date     Implied_Volatility     Open     High     Low     Close     Volume
71611     NVDA     2021-10-13     33.684118     20.94     20.99     20.72     20.94     66359.50
71612     NVDA     2021-10-13     33.684118     20.92     20.99     20.71     20.94     1070657.50
71613     NVDA     2021-10-14     32.698311     21.29     21.75     21.13     21.75     78571.30
71614     NVDA     2021-10-14     32.698311     21.27     21.76     21.12     21.75     1575756.20
71615     NVDA     2021-10-15     30.701298     21.81     21.93     21.66     21.86     80737.50
71616     NVDA     2021-10-15     30.701298     21.78     21.93     21.66     21.86     1486919.50

Am I missing something in the call please?


duplicate rows from a simple api request reqHistoricalData

 

I'm sending

 self.reqHistoricalData(  # https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#requesting-historical-bars
                        reqId=self.req_id,   # Use a unique request ID
                        contract=contract,
                        endDateTime='',
                        durationStr='3 Y',
                        barSizeSetting='1 day',
                        whatToShow='TRADES',
                        useRTH=1,
                        formatDate=1,
                        keepUpToDate=False,
                        chartOptions=[]

and getting two rows per day all the time but don't know why?

    Symbol     Date     Implied_Volatility     Open     High     Low     Close     Volume
71611     NVDA     2021-10-13     33.684118     20.94     20.99     20.72     20.94     66359.50
71612     NVDA     2021-10-13     33.684118     20.92     20.99     20.71     20.94     1070657.50
71613     NVDA     2021-10-14     32.698311     21.29     21.75     21.13     21.75     78571.30
71614     NVDA     2021-10-14     32.698311     21.27     21.76     21.12     21.75     1575756.20
71615     NVDA     2021-10-15     30.701298     21.81     21.93     21.66     21.86     80737.50
71616     NVDA     2021-10-15     30.701298     21.78     21.93     21.66     21.86     1486919.50

Am I missing something in the call please?


Re: Close Orders Rejected

 

开云体育

To check the impact of an order on margin, you can do a what-if in TWS or via the API before you enter the order and see the impact on margin requirement. Pls check the IBKR docs. It's not that easy to give a formula since it will depend on a few factors incl margin mode of the account and??IBKR's margin calculation at the time (they've made some changes after the vol spike on August 5 for portfolio margin to make it more stringent due to wider bid/ask spreads).?

You're right the cash is reserved as collateral for a short position but the impact on margin may not be 0 by closing the position. Best is to always keep sufficient 'margin cushion'. I believe they show a warning in TWS when that goes below 10% but better to keep significantly higher than that especially when holding volatile products (options, tech stocks etc)

rgds, Bart
On Oct 12, 2024 at 11:44?AM -0700, Flaviu Paul via groups.io <flaviupaul@...>, wrote:

I will use the what-if order, thank you, but I would want to use it when I try to buy a new stock so that I don't get into a situation where I am no longer able
to exit the current position on other stock.?
?
Is the formula something like this:
equityWithLoanAfter - initMarginAfter > buffer
?
"I thought you were trying to close out a short position. So for that you would need to buy back the stock so spend cash. "
?
It is a short position and I have to buy back the stock, but isn't the cash already taken when I open the trade and used as collateral?
?
?


Re: Close Orders Rejected

 

I will use the what-if order, thank you, but I would want to use it when I try to buy a new stock so that I don't get into a situation where I am no longer able
to exit the current position on other stock.?
?
Is the formula something like this:
equityWithLoanAfter - initMarginAfter > buffer
?
"I thought you were trying to close out a short position. So for that you would need to buy back the stock so spend cash. "
?
It is a short position and I have to buy back the stock, but isn't the cash already taken when I open the trade and used as collateral?
?
?


Re: Close Orders Rejected

 

开云体育

I don’t think you can retrieve historical margin or excess liquidity. You can only see it for the current time in TWS in the account window. You can do a what-if order to see impact on margin situation at the present time, in TWS or via API.?
I thought you were trying to close out a short position. So for that you would need to buy back the stock so spend cash.?

rgds, Bart
On Oct 12, 2024 at 1:36?AM -0700, Flaviu Paul via groups.io <flaviupaul@...>, wrote:

Thank you Bart.
I don't know how to get the historical excess liquidity or initial margin.
The Daily PNL was positive when I tried to close the position and had unused cash.?
You mentioned that "since your cash position will reduce", my cash position would increase as I close the position as I exit the trade I transform the
shares back into cash.


Re: Close Orders Rejected

 

Thank you Bart.
I don't know how to get the historical excess liquidity or initial margin.
The Daily PNL was positive when I tried to close the position and had unused cash.?
You mentioned that "since your cash position will reduce", my cash position would increase as I close the position as I exit the trade I transform the
shares back into cash.