Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

IKBR API: Steps to Find Open Positions and Close Them

Last updated on 4 days ago
K
KevinMember
Posted 4 days ago
Steps to Find Open Positions and Close Them

Set Up IBKR API:
Start the IB Gateway or Trader Workstation (TWS) in API mode.
Ensure that API access is enabled and set up the port (default: 7497 for live trading or 7496 for paper trading).

Authenticate via IBKR REST API:
Authenticate using the IBKR Client Portal API or directly through your gateway.

Find Open Positions: Use the /portfolio/accounts and /portfolio/{accountId}/positions endpoints to retrieve the list of open positions.

Close Positions:
Use the /iserver/account/{accountId}/orders endpoint to place a sell order for the open position.

fetch code from https://github.com/CAAutomation/IKBR-api/blob/main/find-open-positions-and-close
Key API Endpoints

Authenticate:
POST /portal/auth
Get Accounts:
GET /portfolio/accounts
Get Open Positions:
GET /portfolio/{accountId}/positions
Close Position (Place Order):
POST /iserver/account/{accountId}/orders
K
KevinMember
Posted 4 days ago
Notes

Authentication: Replace YOUR_ACCESS_TOKEN with a valid token obtained during authentication.
Contract ID (ConId): Retrieve the conid of the position from the /positions API.
Order Type: Use MKT for market orders or LMT for limit orders.
Testing Environment: Test in the IBKR Paper Trading account to avoid live trading risks.
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 3
Members Online 0

Total Members: 11
Newest Member: Jhilam