Keyboard Shortcuts
Likes
- Twsapi
- Messages
Search
API data not the same as TWS data?
Hi there. I'm retrieving data from stocks using a Python script and the methods?tickPrice, tickSize and updateMktDepthL2
?
It looks like it is working, but checking the data I get from the API is not?similar at all to the data shown in TWS (or other apps). I'm wondering if I'm doing things right. I have been?reading about this on the internet and it happens to be a common?subject. I know I should count with some deviation, the bars I could build with fetched?data are not going to be exactly?the same as the TWS bars, but the samples I have checked are far away from being close.?
?
I'm trying to build?a system that takes data from the order book and trades (prices) to make a study, but with the provided?accuracy is impossible. I don't need a 100% accuracy, let's say a 98% will be fine.?
?
By the way, I haven't found any documentation about the retrieved data, is there someone who can give me a link or confirm my understanding is right:
?
05/09/2024 15:29:59.427 HBAN tickPrice reqId: 3 tickType: BID price: 14.85 attrib: CanAutoExecute: 1 PastLimit: 0 PreOpen: 0
05/09/2024 15:29:59.427 HBAN tickSize reqId: 3 tickType: BID_SIZE size: 2300 ?
The first line means an order has been filled in the BID side at 14.85? ?
The second line means the order had a size of 2300
So the last crossed price will be 14.85?
The same is true for ASK side orders.
?
05/09/2024 15:30:00.275 HBAN tickPrice reqId: 3 tickType: LAST price: 14.85 attrib: CanAutoExecute: 0 PastLimit: 0 PreOpen: 0
05/09/2024 15:30:00.276 HBAN tickSize reqId: 3 tickType: LAST_SIZE size: 2300? ?
This is much more tricky, it could be: 1) a confirmation of the previous trade, so they are always redundant, 2) Market orders crossed with other market orders
Could anyone confirm?
?
Thank you,
|
Re: How are built the Continuous future data retrieved with secType = CONTFUT ?
To create a continuous future, you must apply the back adjustment method with the various futures contracts that you downloaded.
The most recent future is used to calculate a coefficient that is used to adjust (going back) the closest expired future, etc. Here is an explanation: ?
[Edited by Moderator: this link shows one way to calculate continuous future data: it may not be the way IBKR does it.] |
Re: What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)
scott_hopgood@hotmail.com
For the benefit of those members that do use Python with the TWS API (and 闯ü谤驳别苍 surely with over 6,000 members I cannot be the only one, and therefore this has relevance to those members), I used the following method which works nicely.
?
# create the OrderCancel object
order_cancel = OrderCancel()
?
# add an empty string for manualOrderCancelTime to the object
order_cancel.manualOrderCancelTime = ""
# pass the object as the second parameter
self.cancelOrder(orderId, order_cancel)
?
Also DS, to clarify, I did indeed use square brackets as a symbolic notation of passing a single integer value as the OrderID, rather than this being the syntax of the code that would represent a list in Python.
?
As this was the last issue in my 6000+ line program that I so naively developed using a copy-and-paste approach, I am most pleased that the program now works perfectly and is rendering excellent daily results.?
?
Thank you all for your input.
|
How are built the Continuous future data retrieved with secType = CONTFUT ?
I submit the question to IB assistance 2 months ago, but no answer received until today.
So I ask the group:
As wrote in the subject:
How are built the Continuous future data retrieved with secType = CONTFUT ? The data are a simple concatenation of the data from the different contracts?
Or are the data from previous contracts rescaled avoiding gaps? How is the rollover date chosen? By volume change or expiry date or anything else? I did not find any info about on the web. Anyone know the method used by IB to build the Continuous version of futures?
|
Re: Expired contract & TimeZoneId
I asked IB assistance, their answer was so:
-------------
Now, when using `reqContractDetails()` to get the contract details of expired Future contracts, the timeZoneIds are always Null.
This is by design. However, there is always a timeZoneId get returned when you `reqContractDetails()` to get the unexpired Future Contracts.
For example, the `ES FUT USD CME 20240920` returns the timeZoneId: US/Central. Unexpired Future Contract timeZoneId is always same as the expired Future Contract timeZoneId.
You can get the unexpired Future Contract timeZoneId to know the expired Future Contract timeZoneId.
-------------
Let me notice that it is not a very good design idea, a field "isExpired" should be present instead!
?
|
Re: Rolling futures to the next expiry
I have been looking into calendar spreads for a few weeks and it looks to me that what you experience is intended this way. By default and documented as such in the tool tips, TWS seems to
If you BUY 1 spread of RTYU4-Z4 you will get positions: -1 RTYU4 and +1 RTYZ4 If you SELL 1 spread of RTYU4-Z4 you will get positions: +1 RTYU4 and -1 RTYZ4 With the "Spread Trader" in TWS or via the API you can define this the other way around, but I got myself turned around a few time when buying and selling the various combinations. So I decided to stay with that "front -1, back +1" convention for my TWS API implementation calendar spread order placement. 闯ü谤驳别苍 ?
On Mon, Jun 24, 2024 at 03:52 AM, Brian wrote:
|
Re: What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)
In client API 10.19 the method's signature suggests an object of type OrderId for the first operand, which appears to be equivalent to a single int, i.e. a primitive integer type. Since square brackets indicate a list of objects in Python, it goes against the signature, and although typing annotations in Python are purely suggestive, in fact the source of the method itself indicates this shouldn't really work.
?
I frankly didn't pay much attention to this artefact assuming it could be some type of symbolic notation but if it's a direct quote then it definitely doesn't look right to me unless newer API versions introduce changes to the method allowing to pass it a list of order ids.
?
--
Best, DS |
Re: What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)
Just to wrap this up (since we are moving from an TWS API issue that applies to all 6000++ members to basic Python programming skills). Shouldn't it be self.cancelOrder(orderId, OrderCancel()) instead of self.cancelOrder([orderId], OrderCancel()) ? cancelOrder() defines orderId as an integer value and doesn't [orderId] make it a list of one integer value? 闯ü谤驳别苍 ? ?
On Wed, Sep 11, 2024 at 10:44 AM, ds-avatar wrote:
|
Re: What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)
scott_hopgood@hotmail.com
Hello DS,
?
Thank you for your response to my query. So if I understand correctly, should my call for cancellation of an order look like this instead?
?
self.cancelOrder([orderId], OrderCancel())
?
Kind regards,
Scott. |
Re: What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)
You should use OrderCancel() for the operand in question. This is how Juergen's suggestion of instantiating an object is translated to Python.
?
Seeing that you are highly dependent on the documentation you might consider installing the "stable" client API 10.19 which I believe most of docs are centered around.
?
Also while this group is dedicated mostly to the original IBKR's client API, when it comes to Python the excellent independent alternative ib_async package is arguably much easier to use. But even then limitations of a naive copy-and-paste approach without learning key concepts and practices of the underlying programming language will betray themselves pretty soon. (Note that beside Python, the original client API is also available in C, C# and Java, so there's plenty of choices that could be more suitable to your experience).
?
--
Best, DS |
Re: What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)
scott_hopgood@hotmail.com
Hello 闯ü谤驳别苍,
?
Thank you for this additional information. I have done as you suggested and changed the call for order cancellation to the following:
?
self.cancelOrder([orderId], OrderCancel)
?
Unfortunately this results in the following error, which terminates the program:
?
type object 'OrderCancel' has no attribute 'manualOrderCancelTime'
?
I have taken a look at the source code for Python for order cancellation (order_cancel.py)? and it looks like this:
?
"""
Copyright (C) 2024 Interactive Brokers LLC. All rights reserved. This code is subject to the terms ?and conditions of the IB API Non-Commercial License or the IB API Commercial License, as applicable. """ from ibapi.const import UNSET_INTEGER
from ibapi.object_implem import Object from ibapi.utils import intMaxString ?
class OrderCancel(Object):
? ? def __init__(self): ? ? ? ? self.manualOrderCancelTime = "" ? ? ? ? self.extOperator = "" ? ? ? ? self.externalUserId = "" ? ? ? ? self.manualOrderIndicator = UNSET_INTEGER ?
? ? def __str__(self):
? ? ? ? s = "manualOrderCancelTime: %s, extOperator: %s, externalUserId: %s, manualOrderIndicator: %s" % ( ? ? ? ? ? ? self.manualOrderCancelTime, ? ? ? ? ? ? self.extOperator, ? ? ? ? ? ? self.externalUserId, ? ? ? ? ? ? intMaxString(self.manualOrderIndicator), ? ? ? ? ) ?
? ? ? ? return s
?
Unfortunately I am not well versed in Python beyond the basics from what I have learned from the sample code provided in the IBKR tutorials, so I am just unsure what I am doing wrong here and why this error is occurring. It is disappointing that the API documentation has not been updated by IBKR to reflect the changes made to this function from version 10.30+ so I at a loss as to how I can move past this error.
?
I am most grateful for your assistance.
?
Regards,
Scott.
?
|
Re: What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)
If in doubt, the source code is still the best documentation, Scott. I am not a Python practitioner, but I assume the OrderCancel class has a no-argument constructor that initializes the four fields with meaningful "empty" values. At least the Java implementation has one. Assuming you cancel orders infrequently, I'd replace the empty string "" parameter with something like new OrderCancel()?or the equivalent in Python. My framework already has a convenience method void cancelOrder( int orderId ) that hides the extra parameters from the business logic (since I don't need the manualCancelTime or OrderCancel logic). In that case, a single shared and constant OrderCancel object will work, too. 闯ü谤驳别苍 ? ? On Tue, Sep 10, 2024 at 06:32 PM, <scott_hopgood@...> wrote:
|
Re: What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)
scott_hopgood@hotmail.com
Hello 闯ü谤驳别苍,
?
Thank you for your reply and apologies for not providing the version number I am using. I can confirm that I am using version 10.30.01 and so it seems that passing an empty string as per the original (and latest) documentation is no longer correct.?
?
In this case, would you know what should be passed for OrderCancel instead of an empty string? I searched the latest API documentation in IBKR Campus but I was unable to locate any information regarding the fields of the new OrderCancel class, so I am unable to adjust my call to include the default fields of this class in order to test it.
?
I appreciate your assistance with this.
?
Regards,
Scott. |
Re: What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)
Scott did not tell us, which TWS API version caused the issue. But I suspect it is 10.30 or higher. Looks like 10.30.01 changed the cancelOrder() request signature:
That is true for all languages, not just Python. The new class OrderCancel has several fields, among them a string field called manualOrderCancelTime. Programing languages other than Python should give you compilation errors since the second argument changed from type String to type OrderCancel.?Also, cancelOrder requests will likely fail unless and instance of OrderCancel is passed. An object with default fields might work, but I have not checked that. The shiny new official TWS API documentation on IBKR Campus does not reflect that change yet and still suggests passing "" will work.
?
闯ü谤驳别苍
?
On Tue, Sep 10, 2024 at 01:39 PM, ds-avatar wrote:
|
Re: What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)
This looks so weird that I felt curious enough to sift through the source further.
?
The Python IBAPI source does not contain text 'manualOrderCancelTime' at all so it totally escapes me how it could even throw an error message that you quoted, featuring this text. Beside all this, the error message itself does not follow from anything I see in the API source code.
?
That said, in Java and C# API that same argument is named 'manualOrderCancelTime'. But Python API is not dependent on either.
?
Pretty mind-boggling to say the least, and I still have only questions, no answers...
?
--
Best, DS |
Re: What is manualOrderCancelTime param of Eclient::cancelOrder( int id, String manualOrderCancelTime)
The error is pretty strange but, FWIW, judging by the API source, the correct spelling must be 'manualCancelOrderTime' instead of 'manualOrderCancelTime':
?
?
?
--
Best, DS |
Using Excel
I appreciate this may have been answered (please point me to the thread - or a vba code example).
?
I have the TWS_ActiveX.xls working?
?
But I am unable to automate pulling down Account, Portfolio and Open order data using one button - I have to go to each tab and request data.
?
I am sure its a scoping/hierarchy within the code but I have been unable to make it work .
?
Any pointers - just want to press one button (preferably not on either of the account portfolio or open order tabs) and all data is refreshed
?
Many thanks
?
P
? |