Search code examples
python-3.xinteractive-brokers

Place order with API IB


After deciding to walk away from Ibpy and learn the API IB I am having trouble even placing an order. What am I doing wrong?

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




#Def Market Order

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

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


    def start(self):

        contract = Contract()
        contract.symbol = "ES"
        contract.secType = "FUT"
        contract.exchange = "GLOBEX"
        contract.currency = "USD"
        contract.lastTradeDateOrContractMonth = "201903"


        order = Order()
        order.action = "BUY"
        order.orderType = "MKT"
        order.totalQuantity = 1
        order.outsideRth = True

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

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

def main():
    app=TestApp()
    app.connect("127.0.0.1", 7496,420)
    app.nextOrderId = 0
    app.run()



if __name__ == "__main__":
    main()

removed more Ibpy and replaced with ibapi

now getting

%Run basic.py 394 ERROR -1 2104 Market data farm connection is OK:usfuture ERROR -1 2104 Market data farm connection is OK:usfarm ERROR -1 2106 HMDS data farm connection is OK:ushmds ERROR 394 200 No security definition has been found for the request


Solution

  • from ib.ext.Contract import Contract from ib.ext.Order import Order

    Those are from IbPy, use only classes that start with ibapi.