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.

place a buy order using the Interactive Brokers (IBKR) API

Last updated on 4 days ago
K
KevinMember
Posted 4 days ago
An example of how you can place a **buy order** using the **Interactive Brokers (IBKR) API** in PHP. IBKR's API works over **TWS (Trader Workstation)** or **IB Gateway** and provides REST or socket-based APIs for automation.

We'll use the **IBKR Python Gateway API** approach here, but integrate with PHP using a REST client.

---

### **Steps to Place a Buy Order Using IBKR API**
1. **Setup IBKR TWS or Gateway**:
Ensure your IBKR TWS or Gateway is running in **API mode**. Enable API access by:
- Navigate to Global Configuration > API > Settings.
- Check **Enable ActiveX and Socket Clients**.
- Set **Host** and **Port** (default is 127.0.0.1:7497).

2. **Obtain Access via REST API**:
IBKR also provides a REST interface through the **IBKR Client Portal**. You’ll need to authenticate and get a session token.

3. **Install HTTP Client Library in PHP**:
Use cURL or a library like **Guzzle** for sending HTTP requests to IBKR REST API.

---
K
KevinMember
Posted 4 days ago
<?php
// Replace with your IBKR Gateway API URL
$base_url = "https://localhost:5000/v1/";
// Function to authenticate with IBKR Gateway API
function authenticateIBKR($base_url) {
$auth_url = $base_url . "portal/auth";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // For local dev environments
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo "Error: " . curl_error($ch);
curl_close($ch);
return false;
}
curl_close($ch);
return json_decode($response, true);
}
// Function to place a buy order
function placeBuyOrder($base_url, $account_id, $symbol, $quantity, $price) {
$order_url = $base_url . "iserver/account/" . $account_id . "/orders";
$order_payload = [
"acctId" => $account_id,
"conid" => $symbol, // Contract ID for the security
"secType" => "STK", // Stock type
"side" => "BUY",
"quantity" => $quantity,
"price" => $price,
"tif" => "GTC" // Good 'til Cancelled
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $order_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($order_payload));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Authorization: Bearer YOUR_ACCESS_TOKEN" // Replace with your access token
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo "Error: " . curl_error($ch);
curl_close($ch);
return false; }
curl_close($ch);
return json_decode($response, true);
}
// Authenticate
$auth_response = authenticateIBKR($base_url);
if ($auth_response) {
echo "Authenticated successfully.n";
// Example Buy Order
$account_id = "YOUR_ACCOUNT_ID"; // Replace with your IBKR account ID
$symbol = "265598"; // Replace with contract ID for the stock
$quantity = 10;
$price = 100.50; // Limit price
$order_response = placeBuyOrder($base_url, $account_id, $symbol, $quantity, $price);
if ($order_response) {
echo "Order placed successfully: " . json_encode($order_response);
} else {
echo "Failed to place order.";
}
} else {
echo "Authentication failed.";
}
?>
K
KevinMember
Posted 4 days ago
Key Notes:

Enable SSL: If you're running IB Gateway locally, you might need to disable SSL verification (CURLOPT_SSL_VERIFYPEER) for testing. For production, always use a valid SSL certificate.

Contract ID: Replace the symbol field with the Contract ID (ConId) for the instrument you wish to trade. You can use the /iserver/secdef/search endpoint to find it.

Order Types: Modify the payload to specify different order types (e.g., MKT for Market Orders or LMT for Limit Orders).

Authentication: Replace YOUR_ACCESS_TOKEN with a valid token from IBKR's authentication API.

Sandbox Testing: Use the IBKR Paper Trading account to test your code before running live trades.
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 2
Members Online 0

Total Members: 11
Newest Member: Jhilam