¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 ¿ªÔÆÌåÓý
Date

Timestamp is missing milliseconds in tickByTickAllLast

 

I am trying to request real-time tick by tick data, but data arrives with timestamp missing milliseconds. Here is my code:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
import time
from datetime import datetime
import threading
?
?
class TestApp(EWrapper, EClient):
? ? def __init__(self):
? ? ? ? EClient.__init__(self, self)
? ? ? ? self.last_price_list = []
?
? ? def error(self, reqId, errorCode, errorString):
? ? ? ? print("Error: ", reqId, " ", errorCode, " ", errorString)
?
? ? def tickByTickAllLast(self, reqId, tickType, time, price, size, tickAtrribLast, exchange, specialConditions):
? ? ? ? print("Tick Price. Ticker Id:", reqId, "tickType:", TickTypeEnum.to_str(tickType),?
? ? ? ? ? ? ? "Price:", price, "\n",
? ? ? ? ? ? ? "market_time_unix:", time, "\n",
? ? ? ? ? ? ? "market_time:", datetime.fromtimestamp(time).strftime("%Y-%m-%d %H:%M:%S.%f'"), "\n",
? ? ? ? ? ? ? "serve_time :", datetime.now())
? ? ? ? print("_______________________")
? ??
?
? ? app = TestApp()
?
app.connect("127.0.0.1", 4002, 0)
? ??
? ? # allow time to connect to server
time.sleep(1)
? ??
contract = Contract()
contract.symbol = "SPY"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "ARCA"
?
#app.reqMarketDataType(4)? # switch to delayed-frozen data if live is not available
app.reqTickByTickData(1 ,contract, "Last", 0, True)
#app.reqMktData(1, contract, "", False, False, [])
?
api_thread = threading.Thread(target=app.run)
api_thread.start()



Here is output:

Error:? -1? ?2104? ?Market data farm connection is OK:usfarm
Error:? -1? ?2106? ?HMDS data farm connection is OK:ushmds
Error:? -1? ?2158? ?Sec-def data farm connection is OK:secdefil
Tick Price. Ticker Id: 1 tickType: BID Price: 414.37?
?market_time_unix: 1659545243?
?market_time: 2022-08-03 16:47:23.000000'?
?server_time : 2022-08-03 16:47:23.880814
_______________________
Tick Price. Ticker Id: 1 tickType: BID Price: 414.37?
?market_time_unix: 1659545243?
?market_time: 2022-08-03 16:47:23.000000'?
?server_time : 2022-08-03 16:47:23.881391
_______________________
Tick Price. Ticker Id: 1 tickType: BID Price: 414.37?
?market_time_unix: 1659545243?
?market_time: 2022-08-03 16:47:23.000000'?
?server_time : 2022-08-03 16:47:23.881610


Is there way to get milliseconds/microseconds?


Re: No historical market data for AAL/STK@VALUE Last 1d

 

You caught yourself a good one here, David.

I am sure our environments are different enough that we get (at least) slightly different results. I am running tests in a paper account, TWS 10.16.1n and a pretty odd Java API 9.85.02 (that is version 161). In that context, requesting historical data for AAL/STK/SMART/NASDAQ works as expected and without any issues.

BUT ...

Before requesting historical data, our down-loader grabs the head timestamp as described in . And that throws an error similar to what you get.

The interesting part is that the error tracks with the value I use for the "exchange" field in the contract:

Error when exchange="SMART"

19:43:51:211 <- 87-10000002-139673266-AAL-STK--0.0---SMART-NASDAQ-USD-AAL-NMS-0-1-TRADES-2-
19:43:51:234 -> ---q4-2-10000002-162-Historical Market Data Service error message:No historical market data for LCC/STK@VALUE Last 0-
19:43:51:265 <- 87-10000003-139673266-AAL-STK--0.0---SMART-NASDAQ-USD-AAL-NMS-0-0-TRADES-2-
19:43:51:288 -> ---q4-2-10000003-162-Historical Market Data Service error message:No historical market data for LCC/STK@VALUE Last 0-

No error when "exchange" is one of the legitimate real exchanges for AAL, here for example "NYSE":

19:56:05:306 <- 87-10000002-139673266-AAL-STK--0.0---NYSE-NASDAQ-USD-AAL-NMS-0-1-TRADES-2-
19:56:05:317 -> ---88-10000002-1127827800-
19:56:05:430 <- 87-10000003-139673266-AAL-STK--0.0---NYSE-NASDAQ-USD-AAL-NMS-0-0-TRADES-2-
19:56:05:442 -> ---88-10000003-1127827800-

Maybe you can try to grab historical data for NYSE/NASDAQ yourself (instead of SMART/NASDAQ). If that works it would probably be something you want to raise with IBKR support.

´³¨¹°ù²µ±ð²Ô



On Tue, Aug 2, 2022 at 05:42 PM, David Walker wrote:

Hi ´³¨¹°ù²µ±ð²Ô,

?

Thanks I had a similar thought, but I traced the contract into the IB client class function and it is definitely submitting the AAL NASDAQ contract.? Here is the contract object as it is inside the reqHistoricalData function in the client class:

?

{'comboLegs': None,

'comboLegsDescrip': '',

'conId': 139673266,

'currency': 'USD',

'deltaNeutralContract': None,

'exchange': 'SMART',

'includeExpired': False,

'lastTradeDateOrContractMonth': '',

'localSymbol': 'AAL',

'multiplier': '',

'primaryExchange': 'NASDAQ',

'right': '',

'secId': '',

'secIdType': '',

'secType': 'STK',

'strike': 0.0,

'symbol': 'AAL',

'tradingClass': 'NMS'}

?

The conId matches the correct NASDAQ AAL conId on IB¡¯s website.? The conId of the VALUE hosted instrument is 285940697, so it¡¯s hard to see the crossover happening on IB¡¯s side.

?

In response to the request with the contract above, the same error is produced: ¡°162 Historical Market Data Service error message:No historical market data for AAL/STK@VALUE Last 1d¡±

?

Could you to make an identical request with the exact contract above and see if you receive the same error??

?

Dave

?

?

?

From: [email protected] <[email protected]> On Behalf Of ´³¨¹°ù²µ±ð²Ô Reinold via groups.io
Sent: Tuesday, 2 August 2022 4:35 PM
To: [email protected]
Subject: Re: [TWS API] No historical market data for AAL/STK@VALUE Last 1d

?

When you say "I'm retrieving the contract object fresh" are you talking about reqContractDetails? If so, how many contracts does the request return?

When I ask for AAL:STOCK (no other fields initialized in the query contract) I get 31 contracts for three instruments:

  • 25 contracts relate to "AMERICAN AIRLINES GROUP INC" (conid 139673266) for the various exchanges it is listed on
  • 5 contracts relate to "ANGLO AMERICAN PLC" (conid 45024954)
  • 1 contract is for "AVIAAM LEASING AB" (conid 285940697)

Only that last contract for "AVIAAM LEASING AB" trades on the VALUE exchange. Any chance wires might get crossed and the historical data request sees that contract?

Attached a quick CSV with the contracts I get.

´³¨¹°ù²µ±ð²Ô

On Tue, Aug 2, 2022 at 12:02 AM, David Walker wrote:

Hi Everyone,

I'm receiving this error callback when making an historical data request for AAL:

"No historical market data for AAL/STK@VALUE Last 1d"? (error code 162)

I can't work out what's going on!? I'm retrieving the contract object fresh before sending the reqHistoricalData request.? The exchange in the contract object is 'SMART' and primaryExchange is 'NASDAQ', but the error message seems to indicate AAL is looking at the VALUE exchange.

TWS shows historical data for AAL in the last 1 D, so it's not clear why the API would say there is no data.

Most of my historical data requests are working fine (with all the same request parameters, except the contract object) - though there are a handful of others (say 1 in 100) that are also giving the same error above.

Can anyone help solve this?

?


Re: No historical market data for AAL/STK@VALUE Last 1d

 

¿ªÔÆÌåÓý

Hi ´³¨¹°ù²µ±ð²Ô,

?

Thanks I had a similar thought, but I traced the contract into the IB client class function and it is definitely submitting the AAL NASDAQ contract.? Here is the contract object as it is inside the reqHistoricalData function in the client class:

?

{'comboLegs': None,

'comboLegsDescrip': '',

'conId': 139673266,

'currency': 'USD',

'deltaNeutralContract': None,

'exchange': 'SMART',

'includeExpired': False,

'lastTradeDateOrContractMonth': '',

'localSymbol': 'AAL',

'multiplier': '',

'primaryExchange': 'NASDAQ',

'right': '',

'secId': '',

'secIdType': '',

'secType': 'STK',

'strike': 0.0,

'symbol': 'AAL',

'tradingClass': 'NMS'}

?

The conId matches the correct NASDAQ AAL conId on IB¡¯s website.? The conId of the VALUE hosted instrument is 285940697, so it¡¯s hard to see the crossover happening on IB¡¯s side.

?

In response to the request with the contract above, the same error is produced: ¡°162 Historical Market Data Service error message:No historical market data for AAL/STK@VALUE Last 1d¡±

?

Could you to make an identical request with the exact contract above and see if you receive the same error??

?

Dave

?

?

?

From: [email protected] <[email protected]> On Behalf Of ´³¨¹°ù²µ±ð²Ô Reinold via groups.io
Sent: Tuesday, 2 August 2022 4:35 PM
To: [email protected]
Subject: Re: [TWS API] No historical market data for AAL/STK@VALUE Last 1d

?

When you say "I'm retrieving the contract object fresh" are you talking about reqContractDetails? If so, how many contracts does the request return?

When I ask for AAL:STOCK (no other fields initialized in the query contract) I get 31 contracts for three instruments:

  • 25 contracts relate to "AMERICAN AIRLINES GROUP INC" (conid 139673266) for the various exchanges it is listed on
  • 5 contracts relate to "ANGLO AMERICAN PLC" (conid 45024954)
  • 1 contract is for "AVIAAM LEASING AB" (conid 285940697)

Only that last contract for "AVIAAM LEASING AB" trades on the VALUE exchange. Any chance wires might get crossed and the historical data request sees that contract?

Attached a quick CSV with the contracts I get.

´³¨¹°ù²µ±ð²Ô

On Tue, Aug 2, 2022 at 12:02 AM, David Walker wrote:

Hi Everyone,

I'm receiving this error callback when making an historical data request for AAL:

"No historical market data for AAL/STK@VALUE Last 1d"? (error code 162)

I can't work out what's going on!? I'm retrieving the contract object fresh before sending the reqHistoricalData request.? The exchange in the contract object is 'SMART' and primaryExchange is 'NASDAQ', but the error message seems to indicate AAL is looking at the VALUE exchange.

TWS shows historical data for AAL in the last 1 D, so it's not clear why the API would say there is no data.

Most of my historical data requests are working fine (with all the same request parameters, except the contract object) - though there are a handful of others (say 1 in 100) that are also giving the same error above.

Can anyone help solve this?


Re: About commissionReport.realizedPNL

 

¿ªÔÆÌåÓý

I think it¡¯s just as simple as monitoring position changes, and then using the reported realizedPNL for that position change directly. It gets reported to you, no need to calculate.?

Rgds, Bart

On Aug 2, 2022, at 6:50 AM, david03kimo via groups.io <david03kimo@...> wrote:

?Hi,Bart,

Thanks for your help.
It's a way to calculate realized PnL by adding dailyPnL.
Do you use this method by calculating yourself?


--


------
Forex trader
David Liao


Re: reqPositionsMulti does not work as intended

 

´³¨¹°ù²µ±ð²Ô,?
Thanks I am aware of it and handle order submissions and fills/executions correctly. I will just allocate the fills to pertaining positions on my end.

Thanks for explaining, it makes it very clear.?


Re: reqPositionsMulti does not work as intended

 

The has a field called that you can use as you wish. The string you set is also carried over to objects

´³¨¹°ù²µ±ð²Ô


On Tue, Aug 2, 2022 at 09:59 AM, Dojo321 wrote:
So, this pertains to specific model portfolios that were set up in advisor accounts in seems??

How do you then keep positions apart for specific strategies? Does this all have to be handled on the user side? I have written an OMS, would I need to keep my positions apart myself inside the OMS? I was hoping to be able to distinguish positions that pertain to specific strategies similar to orders/fills. I am not looking to create hedge accounts but for example, I often trade baskets that are made up of individual assets and at the same time trade individual assets that make up some of the baskets yet I want to see positions and position pnl in a segregated fashion for the baskets and the individual assets. I can easily manage it on my end but it is harder to synchronize and reconcile with broker positions if I am unable to obtain the average prices of positions that represent a slice of the entire net position in my IB account...?


Re: reqPositionsMulti does not work as intended

 

So, this pertains to specific model portfolios that were set up in advisor accounts in seems??

How do you then keep positions apart for specific strategies? Does this all have to be handled on the user side? I have written an OMS, would I need to keep my positions apart myself inside the OMS? I was hoping to be able to distinguish positions that pertain to specific strategies similar to orders/fills. I am not looking to create hedge accounts but for example, I often trade baskets that are made up of individual assets and at the same time trade individual assets that make up some of the baskets yet I want to see positions and position pnl in a segregated fashion for the baskets and the individual assets. I can easily manage it on my end but it is harder to synchronize and reconcile with broker positions if I am unable to obtain the average prices of positions that represent a slice of the entire net position in my IB account...?


Re: reqPositionsMulti does not work as intended

 

Thanks, will take a look


Re: reqPositionsMulti does not work as intended

 

Portfolio models and related to that model codes are for advisors to automate trading of instrument combinations. If you have not done so, take a look at and

´³¨¹°ù²µ±ð²Ô


Re: reqPositionsMulti does not work as intended

 

Fair enough if that was the case but then what is modelCode good for? I don't seem to be able to make use of it. Orders can be supplied with a model code and that modelCode can be entered into reqPositionsMulti. I don't see the benefit of that if it does not work...?


Re: reqPositionsMulti does not work as intended

 

Dojo321 wrote: ".. net position in TWS would be 0 but when I request positions I want to see two positions, .. "
This is not possible with an IB account. Your IB account is a netting account, meaning that only the net result of all trading activities in a ticker symbol is available as position size. IB does not provide a so-called "hedging account", which is what you would need for your purpose.


Re: Suggestions on how to deal with blocking pop up from TWS (like last week)

 

Great question. I didn't get a popup at all. But all orders stopped. And I had to a web interface to actually see to popup and agree.?

More interesting question is: how one could learn in advance about those? I.e. I didn't get any emails from IB that I have x days to review and agree/disagree. The message just popped up. And this is not somethin an algo trading can deal with.? It is actually second time in 2 years where IB stops my trading with no warning (last time it was brexit, and because of jurisdiction change a company needed a LEI number. took 4 days to get. But ib knew about that 6 months in advance. Could have put it in the migration plan.). Either I am (we are) missing some info or IB just not particularly careful with user experience. I.e. I bet those legal changes were known in advance and they could have warned people and gave them some time in advance to react.??


Re: About commissionReport.realizedPNL

 

Hi,Bart,

Thanks for your help.
It's a way to calculate realized PnL by adding dailyPnL.
Do you use this method by calculating yourself?


--


------
Forex trader
David Liao


Re: How to automatically accept (or disable) the paper trading notification?

 

I don't know enough about dockers, so I'll try Despair's suggestion for now. Thanks for your help anyway!


Re: How to automatically accept (or disable) the paper trading notification?

 

Great, thank you very much for your help, Despair!


Re: Best method for getting level 2 market depth snapshots on a large number of equities

 

Thanks for the response Jurgen, really appreciate it. Everything you said about the cancellation requests makes sense to me.

How long does it take to grab a snapshot for 30 instruments?

On average, I find I'm able to get about 1 per second if I limit myself to one stream maximum. With a two stream maximum I can get somewhere between 1.5 and 1.8 per second. With a three stream maximum I was getting a bit over 2 per second, but that's my maximum number of streams alas. This is all trading on my own account, so unfortunately I'm a wee bit short of the account values needed to get awarded bonus streams :-)??? and I'm not currently paying for any booster packs.

Cheers,

Colin


On Tue, 2 Aug 2022 at 17:34, ´³¨¹°ù²µ±ð²Ô Reinold via <TwsApiOnGroupsIo=[email protected]> wrote:

I would have used the same approach and cannot see a way to do it differently. How long does it take to grab a snapshot for 30 instruments?

When you are done with a snapshot it will take a while for your request to be finalized. TWS sees it right away, but I am sure they won't add the feed back to the "available" pool until TWS receives a cancellation confirmation from the IBKR back-end. So I am not surprised that the next might get denied until TWS has received a cancellation confirmation from IBKR. Unfortunately, has no callback that indicates to you when it is safe to make the next request. This could potentially be an ongoing area of instability.

The hard limit for the number of simultaneous Level 2 streams must have been increased, since now says you can have up to 60. But you can only get 10 of them by purchasing booster packs. To get to 60 you must have enough equity or commission to "earn" them.

According to the fine print in

  • "There is no cap on the quantity of market data lines allocated per customer."
  • You get one market line per $8 in monthly commissions
  • Or for each $10,000 in equity

If my math is correct, $48,000 in monthly commission or an account value of $60Mio gets you to the 6,000 market line allocations you'd need for 60 simultaneous market depth feeds. Or half of that for 30 feeds :-)

´³¨¹°ù²µ±ð²Ô


Re: About commissionReport.realizedPNL

 

?The various options for PNL feeds are described in

´³¨¹°ù²µ±ð²Ô

On Tue, Aug 2, 2022 at 02:17 AM, <david03kimo@...> wrote:

Thanks,I think your quick example is exact what I want.
I'd like to know what callback function do you use to get realizedPNL without?commission reports?


Re: Best method for getting level 2 market depth snapshots on a large number of equities

 

I would have used the same approach and cannot see a way to do it differently. How long does it take to grab a snapshot for 30 instruments?

When you are done with a snapshot it will take a while for your request to be finalized. TWS sees it right away, but I am sure they won't add the feed back to the "available" pool until TWS receives a cancellation confirmation from the IBKR back-end. So I am not surprised that the next might get denied until TWS has received a cancellation confirmation from IBKR. Unfortunately, has no callback that indicates to you when it is safe to make the next request. This could potentially be an ongoing area of instability.

The hard limit for the number of simultaneous Level 2 streams must have been increased, since now says you can have up to 60. But you can only get 10 of them by purchasing booster packs. To get to 60 you must have enough equity or commission to "earn" them.

According to the fine print in

  • "There is no cap on the quantity of market data lines allocated per customer."
  • You get one market line per $8 in monthly commissions
  • Or for each $10,000 in equity

If my math is correct, $48,000 in monthly commission or an account value of $60Mio gets you to the 6,000 market line allocations you'd need for 60 simultaneous market depth feeds. Or half of that for 30 feeds :-)

´³¨¹°ù²µ±ð²Ô


Re: About commissionReport.realizedPNL

 

Thanks,I think your quick example is exact what I want.
I'd like to know what callback function do you use to get realizedPNL without?commission reports?


--


------
Forex trader
David Liao


Re: No historical market data for AAL/STK@VALUE Last 1d

 

When you say "I'm retrieving the contract object fresh" are you talking about reqContractDetails? If so, how many contracts does the request return?

When I ask for AAL:STOCK (no other fields initialized in the query contract) I get 31 contracts for three instruments:

  • 25 contracts relate to "AMERICAN AIRLINES GROUP INC" (conid 139673266) for the various exchanges it is listed on
  • 5 contracts relate to "ANGLO AMERICAN PLC" (conid 45024954)
  • 1 contract is for "AVIAAM LEASING AB" (conid 285940697)

Only that last contract for "AVIAAM LEASING AB" trades on the VALUE exchange. Any chance wires might get crossed and the historical data request sees that contract?

Attached a quick CSV with the contracts I get.

´³¨¹°ù²µ±ð²Ô

On Tue, Aug 2, 2022 at 12:02 AM, David Walker wrote:

Hi Everyone,

I'm receiving this error callback when making an historical data request for AAL:

"No historical market data for AAL/STK@VALUE Last 1d"? (error code 162)

I can't work out what's going on!? I'm retrieving the contract object fresh before sending the reqHistoricalData request.? The exchange in the contract object is 'SMART' and primaryExchange is 'NASDAQ', but the error message seems to indicate AAL is looking at the VALUE exchange.

TWS shows historical data for AAL in the last 1 D, so it's not clear why the API would say there is no data.

Most of my historical data requests are working fine (with all the same request parameters, except the contract object) - though there are a handful of others (say 1 in 100) that are also giving the same error above.

Can anyone help solve this?