In the interactive brokers API, How do I check if there is already an open order or if I'm in a trade for a specific Contract in python. Can't seem to find an answer online
Thanks a lot
In the interactive brokers API, How do I check if there is already an open order or if I'm in a trade for a specific Contract in python. Can't seem to find an answer online
Thanks a lot
I believe I found an answer as long as you are trying to get the open orders be printed inside
The openOrder() function was taken straight from the documentation
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import *
import threading
import time
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def nextValidId(self, orderId):
self.nextValidId = orderId
self.start()
def start(self):
self.reqAllOpenOrders()
def openOrder(self, orderId, contract: Contract, order: Order,
orderState):
super().openOrder(orderId, contract, order, orderState)
print("OpenOrder. PermId: ", order.permId, "ClientId:", order.clientId, " OrderId:", orderId,
"Account:", order.account, "Symbol:", contract.symbol, "SecType:", contract.secType,
"Exchange:", contract.exchange, "Action:", order.action, "OrderType:", order.orderType,
"TotalQty:", order.totalQuantity, "CashQty:", order.cashQty,
"LmtPrice:", order.lmtPrice, "AuxPrice:", order.auxPrice, "Status:", orderState.status)
def run_loop():
app.run()
app = IBapi()
app.connect('127.0.0.1', 7497, 123)
# Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()
time.sleep(3)
app.disconnect()