3

This is my code:

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

class TestApp(EWrapper, EClient):

    def __init__(self):
        EClient.__init__(self, self)

    def Error(self, reqID, errorCode, errorString):
        print('Error :', reqID, '', errorCode,'', errorString)

    def contractDetails(self, reqID, contractDetails):
        print('Contract Details :', reqID, '', contractDetails)

    def nextValidId(self, orderId):
        self.nextOrderID = orderId
        self.run()

    def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permID, lastFillprice, cliendId, whyHeld, mktCapPrice):
        print('Orderstatus Id. ', orderId, 'Status: ', status, 'Filled: ', 'Remaining: ', remaining, 'Last Fill Price: ', lastFillprice)

    def openOrderEnd(self, orderId, contract, order, orderState):
        print('Open Order ID. ', orderId, contract.symbol, contract.secType, '@', contract.exchange, ': ', order.action, order.orderType, order.totalQuantity, orderState.status)

    def execDetails(self, reqId, contract, execution):
        print('Exec Details. ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId, execution.orderId, execution.shares, execution.lastLiquidity)

    def accountSummary(self, reqId, account, tag, value, currency):
        print('Account Summary. ', reqId, account, tag, value, currency)

    def start(self):
        contract = Contract()
        contract.symbol = 'NFLX'
        contract.secType = 'STK'
        contract.exchange = 'SMART'
        contract.currency = 'USD'
        contract.primaryExchange = 'NASDAQ'

        order = Order()
        order.action = 'BUY'
        order.totalQuantity = 2
        order.orderType = 'LMT'
        order.lmtPrice = 539.50

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

I am just getting the basic messages when I execute this code:

ERROR -1 2104 Market data farm connection is OK:hfarm ERROR -1 2104 Market data farm connection is OK:usfarm.nj ERROR -1 2104 Market data farm connection is OK:usfuture ERROR -1 2104 Market data farm connection is OK:jfarm ERROR -1 2104 Market data farm connection is OK:eufarm ERROR -1 2104 Market data farm connection is OK:cashfarm ERROR -1 2104 Market data farm connection is OK:usfarm ERROR -1 2106 HMDS data farm connection is OK:euhmds ERROR -1 2106 HMDS data farm connection is OK:fundfarm ERROR -1 2106 HMDS data farm connection is OK:ushmds ERROR -1 2158 Sec-def data farm connection is OK:secdefnj

I copied the code from the IBKR online video. I don't know what I'm doing wrong. I would higly appreciate any help.

ComputerBoi
  • 37
  • 1
  • 1
  • 7

1 Answers1

0

You are connected, you just never placed the order in your start method. Consider renaming your start method since maybe calling Timer.start is confusing.

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

class TestApp(EWrapper, EClient):

    def __init__(self):
        EClient.__init__(self, self)
        self.nextOrderID = 0 # if this is init code put it in init

    def Error(self, reqID, errorCode, errorString):
        print('Error :', reqID, '', errorCode,'', errorString)

    def contractDetails(self, reqID, contractDetails):
        print('Contract Details :', reqID, '', contractDetails)

    def nextValidId(self, orderId):
        self.nextOrderID = orderId
        #self.run() you already called app.run
        self.start() # call your start function, you never did so order wasn't placed.
    
    # you missed parentId, just copy these big defs from the source
    def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice):
        print('Orderstatus Id. ', orderId, 'Status: ', status, 'Filled: ', 'Remaining: ', remaining, 'Last Fill Price: ', lastFillPrice)
        #maybe disconnect when order is filled
        if remaining == 0.0:
            self.stop()

    def openOrderEnd(self, orderId, contract, order, orderState):
        print('Open Order ID. ', orderId, contract.symbol, contract.secType, '@', contract.exchange, ': ', order.action, order.orderType, order.totalQuantity, orderState.status)

    def execDetails(self, reqId, contract, execution):
        print('Exec Details. ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId, execution.orderId, execution.shares, execution.lastLiquidity)
        
    def accountSummary(self, reqId, account, tag, value, currency):
        print('Account Summary. ', reqId, account, tag, value, currency)

    def start(self):
        contract = Contract()
        contract.symbol = 'NFLX'
        contract.secType = 'STK'
        contract.exchange = 'SMART'
        contract.currency = 'USD'
        #contract.primaryExchange = 'NASDAQ'#??may be ISLAND, but not needed for nflx

        order = Order()
        order.action = 'BUY'
        order.totalQuantity = 2
        order.orderType = 'LMT'
        order.lmtPrice = 539.50
        
        self.placeOrder(self.nextOrderID, contract, order)
        self.nextOrderID +=1 # always increment after use

    def stop(self):
        #self.done = True # unnecessary 
        if self.isConnected() : 
            print("disconnecting")
            self.disconnect()
        
def main():
    app = TestApp()
    app.connect('127.0.0.1', 7497, 123)

    #Timer(3, app.stop).start() #means you want to stop in 3 seconds no matter what
    app.run()

if __name__ == '__main__':
    main()
brian
  • 10,619
  • 4
  • 21
  • 79
  • Thank you for that. However, I am unable to get the function accountSummary and openOrderEnd to run. How can I fix this? – ComputerBoi Jan 05 '21 at 19:47
  • You don't ask for them. Check the docs for how to request them. eg. http://interactivebrokers.github.io/tws-api/account_summary.html#acct_summary_req – brian Jan 05 '21 at 20:18
  • This is function is under the class TestApp – ComputerBoi Jan 06 '21 at 16:26
  • def accountSummary(self, reqId, account, tag, value, currency): self.reqAccountSummary(reqId,account, tag, value ) print('Account Summary. ', reqId, account, tag, value, currency) – ComputerBoi Jan 06 '21 at 16:27
  • It still doesn't show that in the python console. How should I fix it? – ComputerBoi Jan 06 '21 at 16:27
  • You have to reads the docs or ask another question. A response will come after a request, the docs explain how. – brian Jan 06 '21 at 17:42
  • Sorry to disturb you but I am new to python. – ComputerBoi Jan 07 '21 at 16:19
  • This function is in the TestApp class – ComputerBoi Jan 07 '21 at 16:19
  • def accountSummary(self, reqId, account, tag, value, currency): self.reqAccountSummary(9002, 'All', '$LEDGER') super().accountSummary(reqId, account, tag, value, currency) print('Account Summary. ReqID: ',reqId, 'Account: ', account, 'Tag: ', tag, 'Value:', value, 'currency: ', currency) – ComputerBoi Jan 07 '21 at 16:19
  • that isn't right either and I don't understand where I am going wrong – ComputerBoi Jan 07 '21 at 16:19