Keyboard Shortcuts
Likes
- Twsapi
- Messages
Search
Re: Main app in new process vs new thread (Python)
Thank you, buddy!
So this piece (TWS API reader) runs in child process and does effectively nothing since it does not see request for data p = Process(target = app.run)
p.start()
p.join() while this piece runs in parent process? app.reqMktData(101, contract , '', False, False, []) ?
time.sleep(2)
app.disconnect()
I expect parent process to terminate without any problem and this should kill child process as well, but it does not happen. It just hangs.? Good point about OS resources, but my knowledge of OS are so limited that it looks like Python path is a bit easier. Thank you again for clarifications!? |
How is auction price calculated?
On the page with the list of tick types (), I see "Auction Price" which is the "The price at which the auction would occur if no new orders were received and the auction were held now- the indicative price for the auction. Typically received after Auction imbalance (tick type 36)".
1) How does IB calculate this? 2) What data feed do I need for this data? Order imbalance? Thank you. |
Re: Request Historical Data with Keep up to date set to true
I am using C++. The last arg, the TagValue list exist too, as I can see it in the C# doc
but I never filled it with anything.? Seems more a provision for future use. The? keepUpToDate flags does still exist as it seems in C# too, and work as expected I always set endDateTime as "" to get latest date. Could it be a factor that influence your behavior ? you need to show more of the way you fill reqHistoricalData request to get an opinion. |
Re: Request Historical Data with Keep up to date set to true
Which TWS API version are you looking at, Shaun? I just checked 10.19 "stable" and 10.20 "latest" and the reqHistoricalData request signature contains the boolean keepUpToDate and the chartOptions List;TagValue> for all languages. Check out "source/CSharpClient/client/EClient.cs" The documentation at also shows both, the keepUpToDate boolean and chartOptions? list of TagValues. ´³¨¹°ù²µ±ð²Ô |
Re: Main app in new process vs new thread (Python)
On Thu, Jan 5, 2023 at 08:17 PM, GreenGreen wrote:
If you read the man page for fork carefully you'll see it says... The child process and the parent process run in separate memory spaces. And, ´³¨¹°ù²µ±ð²Ô kindly pointed out that the API works well when all threads run within the same process and have access to the same memory and global variables. So... when you call:
and then, subsequently:
You just might not be calling the functions in the process you think you are. In other words, you need to make sure you're referring to the same "app", because now there are two (one in the child and one in the parent). So, the problem just might be that you're having the parent operate on things that are actually inside the child (or vice versa) and without doing some special IPC like shared memory etc this won't work. And even then, be careful because the API may not be designed to operate that way necessarily. If you want the parent to "control" the lives of the children (count them, spawn a few, etc) that's one thing. But you can't just assume they share everything off the bat.. they share very little. A fork basically just starts a copy of the parent process in separate address space. Think of it like running the same program twice from the command-line. If you did that you'd need a different client ID for each and two different tcp/ip sockets and probably call reqMktData on different contracts, right? This is why I suggested you get more familiar with what fork does, since under the hood that's likely what a new Python Anyway, at the end of the day when ´³¨¹°ù²µ±ð²Ô asks What is it that you are trying to achieve? he's being practical. However, when I suggested you read the man page for fork it was for you to learn something in an academic sense. For the life of me IDK why you'd actually want to complicate things in the way you're trying to, lol. There's nothing wrong with using multiprocessing, I actually prefer it. But it's always easier to first use the facilities built into the OS before rewriting things in Python... you know, top, kill, pgrep, Unix pipes, the shell's ampersand operator etc. Then, if you're doing the same thing over and over whip something up in Python. Just my 2cents. Good luck! |
Request Historical Data with Keep up to date set to true
I am working in C#. I don't know if this feature works the sake in other languages or not.
I want to make a Historical Data Request with Keep Up to Date set to true. However, the signature for reqHistoricalData no longer has an option for that. It seems to have been replaced with a list of tag values. Is there a way, perhaps with a tag value, to keep a request up to date? I know I could request real time 5 second bars and construct whatever timeframe I need from them, but I would prefer it if I could just keep receiving the bars in the timeframe I am working with. Thanks in advance for any help. Shaun |
Re: Main app in new process vs new thread (Python)
You can use a class for each process:
And in main: Que'ing in and out of a class/process makes it easy to add more processes when you need more expediant data processing. You can push a fairly large np array onto a que if you have alot of data to pass around. |
Re: Main app in new process vs new thread (Python)
Thank you, ´³¨¹°ù²µ±ð²Ô!
In python most TWS API example show how to create new thread and run TWS API reader in there. As far as I know in Python threads cannot run in parallel due to GIL (general interpreter lock).? I would like to have two separate processes (running in parallel) 1) One is for TWS API reader 2) One for processing data (I want to make sure that processing data does not hold back data stream) Thank you again for clarification. It looks like I will have to use shared variables to communicate between processes. Even after reading buddy response, I am still puzzled why my code did not work. Child process (TWS API reader) was supposed to terminate after parent process finished.? I simply copied time.sleep() from examples that I have seen. Dropping it now... |
Re: Main app in new process vs new thread (Python)
What is it that you are trying to achieve? Why is the "standard" way of using TWS API insufficient? TWS API runs a reader thread that receives all communication data packets from TWS/IBGW, decodes them, and calls the appropriate callback function to alert you about the response. That works well when all threads run within the same process and have access to the same memory and global variables. But that will not work when the TWS API reader runs in a separate process. The nature of processes is that they cannot simply access each other's memory or directly call some method in a separate process. So you would need to implement your own API for your "client process" to communicate with your "TWS API process". That will be a lot of work even if you start with a library or module that provides a basic infrastructure. And the communication with TWS/IBGW will be slower since requests have to get from your client process first to your API process before they are sent to TWS/IBG and the responses will have to take the same way back. ´³¨¹°ù²µ±ð²Ô PS. Before I forget.? Do yourselves a favor and lose all the? time.sleep() calls. TWS API is an asynchronous protocol where it is undetermined how long it will take for responses to your requests to arrive. If your client runs out of things to do between the request and the arrival of the response(s), the thread has to wait but never sleep. The only acceptable use of sleep in relation with TWS API is when you need to? pace requests (for example to observe the 50 requests/second limit for older TWS/IBGW or the historical data download maximum requests rates). But in these cases, you delay the next request (the sleep is before the request) instead of sleeping after the request in the hope that some response came back while your thread slept. Every language has some form of support for these asynchronous communications and for Python you may want to look at asyncio or take a peek at ib_insync for an excellent implementation. And yes, even IBKR samples and those best selling "Get rich? fast with Python" books are just full of them. It's still wrong, dangerous, and borderline malpractice. On Thu, Jan 5, 2023 at 11:21 AM, GreenGreen wrote:All Python examples on the Internet show to run main app (class extending?EClient and?EWrapper) in new thread, something like this: |
IBKR API: ERROR -1 1100 Connectivity between IB and Trader Workstation has been lost.
Hi,
i am testing the bracket order with ibkr api, it works fine but during the test there was a connection problem then it cancels all orders. I have to restart my python code manually. How can we restart connection when an error 1100 occurs? Thank you oto |
Main app in new process vs new thread (Python)
All Python examples on the Internet show to run main app (class extending?EClient and?EWrapper) in new thread, something like this:
app = TestApp()
app.connect("127.0.0.1", 7497, 1)
time.sleep(1)
api_thread = threading.Thread(target=app.run)
api_thread.start()
?But I wonder, if can run it in new process. Here is what I tried: from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
import time
import datetime
import threading
import psutil
from multiprocessing import Process
?
?
class TestApp(EWrapper, EClient):
? ? def __init__(self):
? ? ? ? EClient.__init__(self, self)
? ? ? ? self.nextValidOrderId = None
? ? ? ? self.price_list = []
? ? ? ??
? ? def error(self, reqId, errorCode, errorString, advancedOrderRejectJson):
? ? ? ? print("Error: ", reqId, " ", errorCode, " ", errorString, "", advancedOrderRejectJson)
? ??
? ? def tickPrice(self, reqId, tickType, price, attrib):
? ? ? ? super().tickPrice(reqId, tickType, price, attrib)
? ? ? ? self.price_list.append(price)
? ? ? ?
contract = Contract()
contract.symbol = "SPY"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "ARCA"
app = TestApp()
app.connect("127.0.0.1", 7497, 1)
time.sleep(1)
?
p = Process(target = app.run)
p.start()
p.join()
time.sleep(2)
?
app.reqMktData(101, contract , '', False, False, [])
?
time.sleep(2)
app.disconnect()
Unfortunately, it just hangs and does not terminate. I wonder what might be the reason? Does it matter that I run it in Jupyter Notebook? ? |
Re: API error - Your API version does not support fractional size rules (during forex price extraction)
As a C++ user what do I need to do to clear this issue?
On Thursday, January 5, 2023, 04:40:01 AM EST, twoprint via groups.io <twoprint@...> wrote:
I would like to let you know the latest Beta version I just tried today seems to fix the issue : version 10.21.0w.
They added a tick box in the API settings (at the very bottom) to ? Send Forex market data in compatibility mode in integer units ?.
I remind that the integer to decimal variable type change will extend to other currency pairs than AUD.USD. |
Re: API error - Your API version does not support fractional size rules (during forex price extraction)
I would like to let you know the latest Beta version I just tried today seems to fix the issue : version 10.21.0w.
They added a tick box in the API settings (at the very bottom) to ? Send Forex market data in compatibility mode in integer units ?.
I remind that the integer to decimal variable type change will extend to other currency pairs than AUD.USD. |
Re: stupid ib authentication
I frankly understand your frustration.
However, lots of pain can be taken off the process. Most recent TWS include a "restart" in the menu, which makes it unnecessary to enter your pin on the IB app on a daily basis. Just be aware that IB will invalidate the login once a week, on Sunday at 1am. Next login you will have to provide the pin again. This can make things a lot smoother. Have a look at IBC, an excellent tool maintained by a dedicated and professional Richard who is active on this forum and the ibc one. Good luck |
Re: API error - Your API version does not support fractional size rules (during forex price extraction)
?
toggle quoted message
Show quoted text
hymagik is correct about the somewhat messy C++ Decimal support and the fact that IBKR has selected a decimal size of 8 bytes (64 bits). To be more precise, IBKR decimal uses a "MathContext with a precision setting matching the IEEE 754R Decimal64 format, 16 digits, and a rounding mode of HALF_EVEN.". I also fully support when hymagik says 'Once in "Decimal" you should stay in Decimal'. The other three language have native language support for decimal math so that no overloading of 8 bytes integer values is required. The use of TWS API V10 decimals is much smoother and type safe in these languages:
On Tue, Jan 3, 2023 at 03:20 PM, <hymagik@...> wrote:
? |
Re: API error - Your API version does not support fractional size rules (during forex price extraction)
There are really no settings you can change. You would have to use a TWS API Version of at least "163" for any of the fractional size rules to work. rossh_yh mentioned that "163" is available in TWS API V10.10 and later. And it sounds like IBKR now requires fractional size support for AUD.USD real-time data. The way this works internally is that IBKR introduces new features and increments the internal API version number. When your client connects to TWS/IGW, the two determine the highest common API version. That way, a client with a very old API version can work just fine with brand new TWS/IBGW as long as the client only utilizes features from it's version level and below. Feature 163 is called MIN_SERVER_VER_FRACTIONAL_SIZE_SUPPORT. You can find the list of the features in the API source code
Your client can also be aware of the maximum API version it understands and, after connection with TWS/IBGW, the highest common version. This is somewhat language dependent again, but in python, for example, these two lines below should be able to print the API version for you and it should print a number of 163 or higher for fractional siye support. from ibapi.server_versions import * ´³¨¹°ù²µ±ð²Ô PS. Excuse my PSL level python skills (Python-as-a-Second-Languge) but we speak Java at home On Tue, Jan 3, 2023 at 03:31 PM, Mark Murari wrote: Thanks Jurgen for the very comprehensive answer which is much appreciated. I need to dig around and figure out why this change is preventing specifically the download of Audusd Forex contract. It seems not to affect any other currency pair except Aud. I am using the python api so not clear what setting needs to be changed to allow the download to work. |