Reputation: 1082
First, to begin with, I was successfully able to place an order using TWS API. However, for that, as I understood, I need to run the TWS desktop version in the background. But I need to run this on my remote server. So I used a 3rd party API called IBeam and created a gateway using it, in the remote server. Now it is working well and serving the GET requests that I request from the Interactive Brokers.
Now, I want to place an order in Interactive Broker, using an API request and found this doc by IB. However, for me it is not clear what they meant by each argument, so as of now I am stuck. I.e, from docs, I need to send a POST request to https://localhost:5000/v1/api/iserver/account/{accountId}/orders (with IB gateway running in localhost:5000) with the request body
{
"orders": [
{
"acctId": "string",
"conid": 0,
"secType": "secType = 265598:STK",
"cOID": "string",
"parentId": "string",
"orderType": "string",
"listingExchange": "string",
"isSingleGroup": true,
"outsideRTH": true,
"price": 0,
"auxPrice": null,
"side": "string",
"ticker": "string",
"tif": "string",
"referrer": "QuickTrade",
"quantity": 0,
"fxQty": 0,
"useAdaptive": true,
"isCcyConv": true,
"allocationMethod": "string",
"strategy": "string",
"strategyParameters": {}
}
]
}
From what I learn from the TWS API, this was all the information needed to place an order:
contract = Contract()
contract.symbol = "AAPL"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "NASDAQ"
order = Order()
order.action = "BUY"
order.totalQuantity = 10
order.orderType = "MKT"
It would be great if you could help me with a sample code to place a similar order using the REST API of Ineteractive Broker
Upvotes: 8
Views: 3863
Reputation: 6264
It seems that you're mixing up the IBKR Web API and IBKR TWS API.
IBeam helps you set up the Web Gateway for the Web API. But the code you shared suggests that you attempt to communicate with the TWS API. The two are not compatible.
You either need to set up the TWS Gateway - in this case replace IBeam with IBC - or use the REST API requests.
To use the REST API, you need to send a POST request to the place_order
endpoint. Use the documentation to submit the correct signature:
https://ibkrcampus.com/ibkr-api-page/cpapi-v1/#place-order
request_url = f"{baseUrl}/iserver/account/U1234567/orders"
json_content = {
"orders": [
{
"acctId": "U1234567",
"conid": 265598,
"conidex": "265598@SMART",
"secType": "265598@STK",
"cOID": "AAPL-BUY-100",
"parentId": None,
"orderType": "TRAILLMT",
"listingExchange": "NASDAQ",
"isSingleGroup": False,
"outsideRTH": True,
"price": 185.50,
"auxPrice": 183,
"side": "BUY",
"ticker": "AAPL",
"tif": "GTC",
"trailingAmt": 1.00,
"trailingType": "amt",
"referrer": "QuickTrade",
"quantity": 100,
# Can not be used in tandem with quantity value.
# "cashQty": {{ cashQty }},
# "fxQty": {{ fxQty }},
"useAdaptive": False,
"isCcyConv": False,
# May specify an allocation method such as Equal or NetLiq for Financial Advisors.
# "allocationMethod": {{ allocationMethod }},
"strategy": "Vwap",
"strategyParameters": {
"MaxPctVol":"0.1",
"StartTime":"14:00:00 EST",
"EndTime":"15:00:00 EST",
"AllowPastEndTime":true
}
}
]
}
requests.post(url=request_url, json=json_content)
Alternatively, you can use IBind (a sister project to IBeam) to do that programmatically:
import os
from ibind import IbkrClient, make_order_request, QuestionType
account_id = os.getenv('IBIND_ACCOUNT_ID', '[YOUR_ACCOUNT_ID]')
client = IbkrClient()
order_request = make_order_request(
conid='265598',
side='BUY'
quantity=10,
order_type='MARKET',
acct_id=account_id,
coid='my_order'
)
answers = {
QuestionType.PRICE_PERCENTAGE_CONSTRAINT: True,
QuestionType.ORDER_VALUE_LIMIT: True
}
response = client.place_order(order_request, answers, account_id).data
print(response)
(See full example here)
Note: I'm the author of both IBeam and IBind
Upvotes: 1
Reputation: 1082
I found this article helpful in the process of placing an order.
I.e, this is a sample request that you can use to place an order
{
"orders": [
{
"acctId": "DU4299134",
"conid": 8314,
"secType": "8314:STK",
"cOId": "testAlgoOrder",
"orderType": "LMT",
"price": 142,
"side": "BUY",
"tif": "DAY",
"quantity": 1,
"strategy": "Adaptive",
"strategyParameters": {"adaptivePriority": "Normal" }
}
]
}
You can use these URLs to find more info about the strategies,
url = f"https://localhost:5000/v1/api/iserver/contract/{conid}/algos"
url_more_info = f"https://localhost:5000/v1/api/iserver/contract/{conid}/algos?addDescription=1&addParams=1&algos={algos}"
Further, when you place an order like above, IBKR will ask you to confirm the order, which you can do by
url = f"https://localhost:5000/v1/api/iserver/reply/{replyid}"
data = '''{
"confirmed": true
}'''
response = requests.post(url, data=data, headers=headers, verify='path to .pem file')
Note that you have to use the correct header when you are sending a POST requests to IBKR as mentioned here.
Upvotes: 4