interactive brokers api python - fire order during "pre market"

722 views Asked by At

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

There are 2 answers

1
Guilhem On

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.

0
khiraide On

Use

order.outsideRth = true;

Please see the documentation below to learn more.

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