3

I'm using the IB API in order to automatically fire orders during pre-market time. I am unable to figure out why the order is waiting for the market opening time instead of buying during the pre-market period. After looking at the API documentation I added the Auction order but still I can't buy during pre-market period.

Here is my code sample:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import *
from threading import Timer

class TestApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)

    def error(self, reqId , errorCode, errorString):
        print("Error: ", reqId, " ", errorCode, " ", errorString)

    def nextValidId(self, orderId ):
        self.nextOrderId = orderId
        self.start()

    def orderStatus(self, orderId , status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice):
        print("OrderStatus. Id: ", orderId, ", Status: ", status, ", Filled: ", filled, ", Remaining: ", remaining, ", LastFillPrice: ", lastFillPrice)

    def openOrder(self, orderId, contract, order, orderState):
        print("OpenOrder. ID:", orderId, contract.symbol, contract.secType, "@", contract.exchange, ":", order.action, order.orderType, order.totalQuantity, orderState.status)

    def execDetails(self, reqId, contract, execution):
        print("ExecDetails. ", reqId, contract.symbol, contract.secType, contract.currency, execution.execId,
              execution.orderId, execution.shares, execution.lastLiquidity)

    def start(self):
        contract = Contract()
        contract.symbol = "AAPL"
        contract.secType = "STK"
        contract.exchange = "IDEALPRO"
        contract.currency = "USD"
        contract.primaryExchange = "NASDAQ"

        order = Order()
        order.action = "BUY"
        order.tif = "AUC"
        order.totalQuantity = 10
        order.orderType = "MTL"
        order.lmtPrice = 1000
       # order = Order()
       # order.action = "BUY"
       # order.totalQuantity = 10
       # order.orderType = "LMT"
       # order.lmtPrice = 1000



        self.placeOrder(self.nextOrderId, contract, order)

    def stop(self):
        self.done = True
        self.disconnect()

def main():
    app = TestApp()
    app.nextOrderId = 0
    app.connect("127.0.0.1", 7497, 0)

    Timer(3, app.stop).start()
    app.run()

if __name__ == "__main__":
    main()
Shivam Roy
  • 1,961
  • 3
  • 10
  • 23
  • I don't trade like that but my guess is you need to set OutsideRTH true. http://interactivebrokers.github.io/tws-api/classIBApi_1_1Order.html#a60dcca6e3c3ae6ae6e0c5f3fdffc5a4a You should also check the exchange to see if auction orders are available. – brian May 18 '21 at 14:26
  • Rth is not the same for premarket. Not working for me at least. From tws you can see that premarket and rth is working only for non market orders. Found TIF option in order, that can be set to OPG, but still not working. – aleXela Jun 06 '22 at 12:54
  • Found this one, checking https://ibkr.info/node/576 – aleXela Jun 06 '22 at 12:58

2 Answers2

1

Use

order.outsideRth = true;

Please see the documentation below to learn more.

https://interactivebrokers.github.io/tws-api/classIBApi_1_1Order.html#a60dcca6e3c3ae6ae6e0c5f3fdffc5a4a

khiraide
  • 81
  • 1
  • 5
-1

Not sure to understand your point but you cannot execute orders during auctions until they close (you can only place orders). For opening auction, it means that your order will be executed once the markets open.

Guilhem
  • 1
  • 1
  • Its not true. There is pre market and after market. And using tws you can set these orders, but from api for some reason not. – aleXela Jun 06 '22 at 12:52