[Python] ib_async TWS API only first order gets transmitted, remaining orders doesnt show on...

Discussão em 'Python' iniciado por Stack, Maio 19, 2025.

  1. Stack

    Stack Membro Participativo

    Currently, I have a function that creates 3 orders:

    1. Market order to enter the stock
    2. Limit order for taking profit
    3. Stop order for stop loss

    However, on TWS only the first order is shown and auto transmitted to TWS through API. Furthermore, for each instance, only the very first order gets transmitted.

    from ib_async import *

    ib = IB()
    ib.connect('127.0.0.1', 7497, clientId=0)

    def place_order(tradeDirection: str, ticker: str, riskSize: float, entryPrice: float, exitPrice: float):
    logger.info(f"====== Placing order for {ticker} ======")

    positionSize = get_position_size(get_risk(riskSize), entryPrice, exitPrice, tradeDirection)
    contract = Stock(ticker, 'SMART', 'USD')

    # Parent Market Order
    parent_order = MarketOrder(tradeDirection, positionSize)

    # Calculate TP and SL
    if tradeDirection == "BUY":
    takeProfitPrice = entryPrice + 2 * (entryPrice - exitPrice)
    stopLossPrice = exitPrice
    tp_order = LimitOrder("SELL", positionSize, takeProfitPrice)
    sl_order = StopOrder("SELL", positionSize, stopLossPrice)

    elif tradeDirection == "SELL":
    takeProfitPrice = entryPrice - 2 * (exitPrice - entryPrice)
    stopLossPrice = exitPrice
    tp_order = LimitOrder("BUY", positionSize, takeProfitPrice)
    sl_order = StopOrder("BUY", positionSize, stopLossPrice)


    # Place all orders
    ib.placeOrder(contract, parent_order)
    ib.sleep(0)
    ib.placeOrder(contract, tp_order)
    ib.sleep(0)
    ib.placeOrder(contract, sl_order)

    logger.info(f"Main Order placed: {parent_order}")
    logger.info(f"TP order placed: {tp_order}")
    logger.info(f"SL order placed: {sl_order}")


    From the logs, you can see there are 3 orders inside Open Orders, but on TWS only the 1st Market Order gets transmitted and filled.

    2025-05-20 00:20:27.525 | INFO | __main__:place_order:49 - ====== Placing order for TSLA ======
    2025-05-20 00:20:27.526 | INFO | __main__:place_order:77 - Main Order placed: MarketOrder(orderId=63, action='BUY', totalQuantity=65)
    2025-05-20 00:20:27.527 | INFO | __main__:place_order:78 - TP order placed: LimitOrder(orderId=64, action='SELL', totalQuantity=65, lmtPrice=359.5)
    2025-05-20 00:20:27.527 | INFO | __main__:place_order:79 - SL order placed: StopOrder(orderId=65, action='SELL', totalQuantity=65, auxPrice=325.0)
    2025-05-20 00:20:27.527 | INFO | __main__:webhook:158 - Open orders: [MarketOrder(orderId=63, action='BUY', totalQuantity=65), LimitOrder(orderId=64, action='SELL', totalQuantity=65, lmtPrice=359.5), StopOrder(orderId=65, action='SELL', totalQuantity=65, auxPrice=325.0)]


    How can I make all 3 orders be transmitted to TWS?

    Continue reading...

Compartilhe esta Página