ibind icon indicating copy to clipboard operation
ibind copied to clipboard

Bad Request: accountId is not valid Only when creating orders

Open shaft3796 opened this issue 5 months ago • 14 comments

Hello, first of all thanks you for this library, I know my problem might not be related to the library directly but as I can't find any answer anywhere, I will try here.

When I try to create an order on a demo account using the following code:

conid = 265598
side = 'BUY'
size = 1
order_type = 'MKT'
order_tag = f'my_order-{datetime.datetime.now().strftime("%Y%m%d%H%M%S")}'

order_request = OrderRequest(conid=conid, side=side, quantity=size, order_type=order_type, acct_id="DUH724521", coid=order_tag)

answers = {
    QuestionType.PRICE_PERCENTAGE_CONSTRAINT: True,
    QuestionType.ORDER_VALUE_LIMIT: True,
    'Unforeseen new question': True,
}

order_response = client.place_order(order_request, answers=answers).data

print(order_response)

I get the following error:

ibind.support.errors.ExternalBrokerError: IbkrClient: response error Result(data=None, request={'url': 'https://api.ibkr.com/v1/api/iserver/account/DUH724521/orders', 'json': {'orders': [{'conid': 265598, 'side': 'BUY', 'quantity': 1, 'orderType': 'MKT', 'acctId': 'DUH724521', 'cOID': 'my_order-20250711074428', 'tif': 'GTC'}]}}) :: 400 :: Bad Request :: {"error":"Bad Request: accountId is not valid: DUH724521","statusCode":400}

However, while placing order is not working, I can successfully fetch my account balance without problem.

I got my account ID in the "Account Parameters" tab, since I can fetch my balance, I assume the problem doesn't come from here, but I may be wrong.

Image

Note that I am using a DEMO account, however as I mentioned, other features such as balance fetching are working and I am able to trade on the live interface.

Any help is much appreciated, have a good day!

shaft3796 avatar Jul 11 '25 07:07 shaft3796

hey @shaft3796 thanks for the detailed description of your issue 👍

How do you authenticate? IBeam? OAuth? In either case, what account name do you use there - live or paper?

I suspect you're logged in as another user, possibly with your live credentials?

Use receive_brokerage_accounts() to verify the currently logged in account.

Voyz avatar Jul 12 '25 07:07 Voyz

@Voyz Thanks you for your quick reply! I am logged in using OAuth. I am using a demo (paper) account, I generated the OAuth credentials through the demo account page.

I don't think I am logged with my live credentials as I don't even have access to the live account (someone else have access to it, I only have demo account creds). Moreover as I mentioned, the fetch ledger endpoint returns the balance of my demo account.

Another issue I have is that I can't use most of the account endpoints, I get an error saying that I should query /accounts first. I still get the error even after calling portfolio_accounts() which work successfully. The account ID that is returned by this method is the same as the one displayed on my demo account on the web UI.

shaft3796 avatar Jul 12 '25 16:07 shaft3796

@shaft3796 thanks for clarifying. Can you confirm if you initialise the brokerage session? You can read more about it here:

https://github.com/Voyz/ibind/wiki/Advanced-OAuth-1.0a#initializing-brokerage-session

Voyz avatar Jul 12 '25 22:07 Voyz

Thanks for your answer @Voyz.

I added the initialize_brokerage_session call to my code which returns {'authenticated': True, 'competing': False, 'connected': True, 'message': '', 'MAC': '**************', 'serverInfo': {'serverName': None, 'serverVersion': None}, 'hardware_info': '******|****************'}

I also added the receive_brokerage_accounts() call which returns {}

The problem is still there though, and I still can fetch my demo account balance.

Hi hopes this will helps you to put me in the correct direction to resolve my problem. Thanks you again for investigating it.

shaft3796 avatar Jul 13 '25 03:07 shaft3796

I ran into this issue and I noticed that it only happens if you call the portfolio_accounts() endpoint and then immediately try to place an order. I fixed it by waiting for 5 seconds between calling the portfolio_accounts() endpoint and placing an order.

roboluc avatar Jul 14 '25 20:07 roboluc

Thanks you so much @roboluc this worked perfectly ! @Voyz maybe this would need more investigating to figure out the root cause of the issue and document this if it is inherent to InteractivesBroker. Thanks you again for your help !

shaft3796 avatar Jul 15 '25 17:07 shaft3796

@shaft3796 did you manage to reproduce this error using the method @roboluc outlined?

Voyz avatar Jul 16 '25 07:07 Voyz

@Voyz Strangely, I just removed the time.sleep to check that this was the solution to the problem, but the order worked fine !

So either this is an issue that happens from time to time on IB side and that is resolved by itself. Or maybe the issue happens on the first time you try to place an order on an account ? It is quite strange honestly.

Edit: That is even stranger because I tried again without time.sleep, this time my order failed with the same error as before. Then I added the time.sleep, and it failed, I rerun and it succeed. So it seems the issue is not really deterministic, maybe the time sleep helps to solve it because of a side effect but this is not the direct cause.

shaft3796 avatar Jul 16 '25 10:07 shaft3796

@shaft3796 understood, very strange indeed. Did you try to talk to the IBKR support about this?

Voyz avatar Jul 16 '25 11:07 Voyz

@Voyz No I didn't, will continue my integration during the next weeks and if the issue happens again I will.

shaft3796 avatar Jul 16 '25 16:07 shaft3796

I came across the same problem. Seems to be resolved now. My steps were:

  • include the account_id param on IbkrClient instantiation
  • call client.receive_brokerage_accounts() even though the response is empty
  • wait 5 seconds before attempting to place order

bug-or-feature avatar Jul 18 '25 15:07 bug-or-feature

I came across the same problem. Seems to be resolved now. My steps were:

  • include the account_id param on IbkrClient instantiation
  • call client.receive_brokerage_accounts() even though the response is empty
  • wait 5 seconds before attempting to place order

Yup that seems to work tks !

shaft3796 avatar Jul 21 '25 07:07 shaft3796

hey guys, I saw this behaviour happen too. @bug-or-feature's suggestion seems to work. I changed the 5 seconds to 0.25 and it works just as reliably.

Voyz avatar Jul 28 '25 15:07 Voyz

@Voyz FYI it has also been working fine for me with the 5s delay.

shaft3796 avatar Aug 10 '25 06:08 shaft3796