erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.81k stars 753 forks source link

Error validating request:-'bY' : cause - The API interface is currently in Read-Only mode. #1

Closed grandtiger closed 7 years ago

grandtiger commented 7 years ago

@erdewit Thanks for sharing this new Python framework. I just gave your example a try, but I got the following error. Does it require write access even for downloading historical data? The reason I set my gateway to read-only mode is to prevent any mistake because I am not ready to place any orders through the API yet. Thanks!

ERROR:ib_insync.wrapper:Error 321, reqId 2147483647: Error validating request:-'bY' : cause - The API interface is currently in Read-Only mode. ERROR:ib_insync.wrapper:Error 321, reqId 2147483647: Error validating request:-'a0' : cause - The account code is required for this operation.

from ib_insync import *

ib = IB()
ib.connect('127.0.0.1', 4003, clientId=1)

bars = ib.reqHistoricalData(
        contract=Stock('TSLA', 'SMART', 'USD'),
        endDateTime='',
        durationStr='30 D',
        barSizeSetting='1 hour',
        whatToShow='TRADES',
        useRTH=True)

print(bars)
grandtiger commented 7 years ago

@erdewit

BTW, after I unchecked read-only mode. I am getting the following error:

ERROR:ib_insync.wrapper:Error 321, reqId 2147483647: Error validating request:-'a0' : cause - The account code is required for this operation.

My account is a regular login, not a FA account. But it does have two accounts under the same login. I have used IBrokers in R without any issue.

Is there a way to pass the account code? Please advise what I need to do to fix this error. Thanks a lot!

erdewit commented 7 years ago

The read-only mode does not seem to work with reqOpenOrders(); this is needed to synchronize open orders at startup.

The second issue is reqAccountUpdates(); for multi accounts it seems to require an account code. I've fixed this in v0.6.1 by supplying a default account code. If you need account values of other accounts then there is IB.accountSummary(account).

The new version can be installed with

pip3 install -U ib_insync

grandtiger commented 7 years ago

Thanks a lot! @erdewit

Is it possible to add a readOnly argument to the constructor of IB class to not call reqOpenOrders? I changed it locally and did a quick test, but it doesn't seem to work. I will try again once I am free in two weeks.

It would be great if we could add support for multiple accounts and even FA account to this framework. Please advise if this is possible under your current design. Thanks again!

grandtiger commented 7 years ago

@erdewit Actually, after I restarted the ipython session, the readOnly change actually works! Following is my change. I didn't clone your repo and just used it directly. It's simple enough that you could just copy and paste. Thanks!

-    def __init__(self):
+    def __init__(self, readOnly=None):
+        self.readOnly = readOnly is not None and readOnly
         self.wrapper = Wrapper()
         self.client = Client(self.wrapper)
     async def connectAsync(self, host, port, clientId, timeout=2):
         self.wrapper.clientId = clientId
         await self.client.connectAsync(host, port, clientId, timeout)
-        await asyncio.gather(
-                self.reqOpenOrdersAsync(),
+        if self.readOnly:
+            await asyncio.gather(
                 self.reqAccountUpdatesAsync(),
                 self.reqPositionsAsync(),
                 self.reqExecutionsAsync())
+        else:
+            await asyncio.gather(
+                    self.reqOpenOrdersAsync(),
+                    self.reqAccountUpdatesAsync(),
+                    self.reqPositionsAsync(),
+                    self.reqExecutionsAsync())
         _logger.info('Synchronization complete')
erdewit commented 7 years ago

I will have to think about this but if a readOnly parameter is to be added it will be to the connect call and not the constructor.

grandtiger commented 7 years ago

Sure. I just started playing around with your framework and don't have a holistic view yet.

I think read-only mode would be a nice feature, since TWS and IB gateway both support this at the API level.

Support for multiple accounts is more important, but it's probably a much bigger undertaking.

On Jul 13, 2017, at 7:10 AM, Ewald de Wit notifications@github.com wrote:

I will have to think about this but if a readOnly parameter is to be added it will be to the connect call and not the constructor.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

erdewit commented 7 years ago

Support for multiple accounts was already there for positions() and accountSummary().

requestFA and replaceFA will be added soon, if I'm forgetting anything multi-account related let me know as I'm not familiar with it.

SomwareHR commented 9 months ago

Hi,

I can see this is an old thread but sam error appeared with ib_insync v.0.9.86

My solution was to change code from

ib.connect ( '127.0.0.1' , 4002 , clientId = 1 )

to

ib.connect ( '127.0.0.1' , 4002 , clientId = 1 , readonly = True )

BR SomwareHR

ghost commented 7 months ago

FWIW I think this is a serious issue. If readonly mode really doesn't work with req open orders, we should report it to IBKR, as their docs imply it should work. Otherwise this is just encouraging people to disable Readonly mode unnecessarily (and who knows that hijacked libraries people may be using...) Can you confirm the issue is on IBKR end?

SomwareHR commented 7 months ago

> ... who knows that hijacked libraries people may be using...

This is one of the reasons why I always use "paper-mode"!