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
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.