简体   繁体   中英

IB API: Checking if in trade or have open order

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM