StopOrder and LimitOrders not going through using ib_insync

483 views Asked by At

I am working with a paper trading account using Interactive Brokers TWS Desktop Application and the python library ib_insync. My goal is to place a market order in GBP.USD with a position size of 25k, place a profit taking LimitOrder, and a StopOrder. The profit taking LimitOrder and StopOrder are at a predefined amount above/below the average fill price. When I run the code, the parent order (in this case long 25k GBP.USD) always goes through but the StopOrder and LimitOrder do not show up in my IB TWS Desktop Application.

Here is my code:

from ib_insync import *

# Create a connection to the IB gateway or TWS
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1) # adjust these as needed

# Define the contract and order
contract = Forex('GBPUSD')
ib.qualifyContracts(contract)
order = MarketOrder('BUY', 25000)

# Submit the order
trade = ib.placeOrder(contract, order)
print("Order submitted.")
ib.sleep(30)
# Wait for the order to fill
while trade.orderStatus.status != 'Filled':
    ib.waitOnUpdate()
print(f"Order status: {trade.orderStatus.status}")

if trade.orderStatus.status == 'Filled':
    print("Order filled, placing child orders.")

    # Define the prices for the child orders
    stop_loss_price = trade.orderStatus.avgFillPrice - 0.00150
    profit_target_price = trade.orderStatus.avgFillPrice + 0.00200
    print(f"Stop loss price: {stop_loss_price}, Profit target price: {profit_target_price}")
    ib.sleep(30)
    # If the order is filled, place a stop loss order
    stop_loss_order = StopOrder('SELL', 25000, stop_loss_price)
    stop_loss_order.orderId = ib.client.getReqId() # get a new order id
    stop_loss_trade = ib.placeOrder(contract, stop_loss_order)
    ib.sleep(30)
    print(f"Stop loss order placed, status: {stop_loss_trade.orderStatus.status}")
    ib.sleep(30)
    # And place a profit taking order
    profit_taking_order = LimitOrder('SELL', 25000, profit_target_price)
    profit_taking_order.orderId = ib.client.getReqId() # get a new order id
    profit_taking_trade = ib.placeOrder(contract, profit_taking_order)
    ib.sleep(30)
    print(f"Profit taking order placed, status: {profit_taking_trade.orderStatus.status}")
    ib.sleep(30)
ib.disconnect()
#ib.run()

I have tried increasing the delays and have been testing my code between 7pm EST - 10pm EST during weekdays so the market is live and volatility shouldn't be an issue. I noticed sometimes when I close my IB TWS Desktop Application, reopen it, and re-run my code all the orders go through (but not always). Again the parent order of long 25,000 GBPUSD always goes through, however, if the StopOrder doesn't go through, then the LimitOrder also doesn't go through. In this scenario the status of both the StopOrder and LimitOrder is "PendingSubmit". I have tried going through ib_insync and IB API documentation in addition to using ChatGPT to try and resolve the issue but none of these sources have helped me.

0

There are 0 answers