I googled 'typeerror: 'int' object is not callable' but still don't know how to do.Cannot find overlap name

423 views Asked by At

I googled the error 'typeerror: 'int' object is not callable' means same name in variables and method/function name. But I cannot find the same names. Can guys help me?many thanks.

In additional, the code is called by other program.The other program call sendorder(action,price,qty,limit,stop) to send an order.

thank, bros.

the error message: Davidde-MacBook-Pro:Github davidliao$ /usr/local/bin/python3 /Users/davidliao/Documents/code/Github/MyProject/_SB.py Traceback (most recent call last): File "/Users/davidliao/Documents/code/Github/MyProject/_SB.py", line 63, in sendorder(action,Limit,quantity,TakeProfit,StopLoss) File "/Users/davidliao/Documents/code/Github/MyProject/SendBracketOrder.py", line 137, in sendorder app.run() File "/Users/davidliao/Documents/code/Github/MyProject/ibapi/client.py", line 239, in run self.decoder.interpret(fields) File "/Users/davidliao/Documents/code/Github/MyProject/ibapi/decoder.py", line 1278, in interpret self.interpretWithSignature(fields, handleInfo) File "/Users/davidliao/Documents/code/Github/MyProject/ibapi/decoder.py", line 1259, in interpretWithSignature method(*args) File "/Users/davidliao/Documents/code/Github/MyProject/SendBracketOrder.py", line 27, in nextValidId self.start() File "/Users/davidliao/Documents/code/Github/MyProject/SendBracketOrder.py", line 68, in start bracket = self.BracketOrder(self.nextOrderId(), self.signal, self.qty,self.entryprice, self.tp, self.sl) TypeError: 'int' object is not callable Error: -1 504 Not connected Davidde-MacBook-Pro:Github davidliao$

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

class TestApp(EWrapper,EClient):
    def __init__(self,action,price,qty,limit,stop):
        EClient.__init__(self,self)
        self.action=action
        self.price=price
        self.qty=qty
        self.tp=limit
        self.sl=stop
        return

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

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

    def orderStatus(self,orderId,status,filled,remaining,avgFillPrice,permId,parenttId,lastFillPrice,clientId,whyHeld,mktCapPrice):
        print('OrderStatus. Id: ',orderId,', Status: ',status,', Filled: ',filled,', Remaining: ',remaining,', LastFillPrice: ',lastFillPrice)
        return
        
    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)
        return
    
    def execDetails(self,reqId,contract,execution):
        print('ExecDetails. ',reqId,contract.symbol,contract.secType,contract.currency,execution.execId,execution.orderId,execution.shares,execution.lastLiquidity)
        return

    def updatePortfolio(self,contract:Contract,position:float,marketPrice:float,marketValue:float,averageCost:float,unrealizedPNL:float,realizedPNL:float,accountName:str):
        print('UpdatePortfolio.','Symbol:',contract.symbol,'SecType:',contract.secType,'Exchange:',contract.exchange,'Position:',position,'MarketPrice:',marketPrice,'MarketValue:',marketValue,'AverageCost:',averageCost,'UnrealizedPNL:',unrealizedPNL,'RealizedPNL:',realizedPNL,'AccountName:',accountName)
        return

    def start(self):
        #EURUSD sell 10000 0
        #{{ticker}} {{strategy.order.action}} {{strategy.order.contracts}} {{strategy.position_size}}
        # print('data is ',self.data)

        # self.data=self.data.split()
        # self.data = [x.upper() for x in self.data]
        # action=self.data[1]
        # print('Action:',action)
        # quantity=int(self.data[2])
        # print('Quantity:',quantity)
        #position=int(self.data[3])
        # print('Position:',position)

        # Contract
        contract = Contract()
        contract.symbol = "EUR"
        contract.secType = "CFD" 
        contract.currency = "USD"
        contract.exchange = "SMART" 

        # Order
        bracket = self.BracketOrder(self.nextOrderId(), self.action, self.qty,self.price, self.tp, self.sl)
        for o in bracket:
            self.placeOrder(o.orderId, contract, o)
            self.nextOrderId() # need to advance this we’ll skip one extra oid, it’s fine

        #Update Portfolio
        self.reqAccountUpdates(True,"") 
        return
        
    def stop(self):
        self.reqAccountUpdates(False,"")
        self.done=True
        self.disconnect()
        return

    @staticmethod
    def BracketOrder(
        self,
        parentOrderId:int, 
        action:str, 
        quantity:float, 
        limitPrice:float, 
        takeProfitLimitPrice:float, 
        stopLossPrice:float
        ):

        #This will be our main or “parent” order
        parent = Order()
        parent.orderId = parentOrderId
        parent.action = action
        parent.orderType = 'LMT'
        parent.totalQuantity = quantity
        parent.lmtPrice = limitPrice
        #The parent and children orders will need this attribute set to False to prevent accidental executions.
        #The LAST CHILD will have it set to True, 
        parent.transmit = False

        takeProfit = Order()
        takeProfit.orderId = parent.orderId + 1
        takeProfit.action = 'SELL' if action == 'BUY' else 'BUY'
        takeProfit.orderType = 'LMT'
        takeProfit.totalQuantity = quantity
        takeProfit.lmtPrice = takeProfitLimitPrice
        takeProfit.parentId = parentOrderId
        takeProfit.transmit = False

        stopLoss = Order()
        stopLoss.orderId = parent.orderId + 2
        stopLoss.action = 'SELL' if action == 'BUY' else 'BUY'
        stopLoss.orderType = 'STP'
        #Stop trigger price
        stopLoss.auxPrice = stopLossPrice
        stopLoss.totalQuantity = quantity
        stopLoss.parentId = parentOrderId
        #In this case, the low side order will be the last child being sent. Therefore, it needs to set this attribute to True 
        #to activate all its predecessors
        stopLoss.transmit = True

        bracketOrder = [parent, takeProfit, stopLoss]
        return bracketOrder

def sendorder(action,price,qty,limit,stop):
    app=TestApp(action,price,qty,limit,stop)
    app.nextOrderId=0
    app.connect('127.0.0.1',4002,0)

    # Call stop() after 3 seconds to disconnect the program
    Timer(3,app.stop).start()
    
    app.run()

if __name__=="__main__":
    sendorder()

But my previous program works well.....

'''
This is for webhook listener to call to send order to IB.
'''

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

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

    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,parenttId,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 updatePortfolio(self,contract:Contract,position:float,marketPrice:float,marketValue:float,averageCost:float,unrealizedPNL:float,realizedPNL:float,accountName:str):
        print('UpdatePortfolio.','Symbol:',contract.symbol,'SecType:',contract.secType,'Exchange:',contract.exchange,'Position:',position,'MarketPrice:',marketPrice,'MarketValue:',marketValue,'AverageCost:',averageCost,'UnrealizedPNL:',unrealizedPNL,'RealizedPNL:',realizedPNL,'AccountName:',accountName)

    def start(self):
        
        #EURUSD sell 10000 0
        #{{ticker}} {{strategy.order.action}} {{strategy.order.contracts}} {{strategy.position_size}}
        print('data is ',self.data)

        self.data=self.data.split()
        self.data = [x.upper() for x in self.data]
        action=self.data[1]
        print('Action:',action)
        quantity=int(self.data[2])
        print('Quantity:',quantity)
        #position=int(self.data[3])
        # print('Position:',position)

        contract = Contract()
        contract.symbol = "EUR"
        contract.secType = "CFD" 
        contract.currency = "USD"
        contract.exchange = "SMART" 

        order=Order()
        order.action=action
        order.totalQuantity=quantity
        order.orderType="MKT"

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

        #Update Portfolio
        self.reqAccountUpdates(True,"") 
        
        

    def stop(self):
        self.reqAccountUpdates(False,"")
        self.done=True
        self.disconnect()

def sendorder(data):
    app=TestApp(data)
    app.nextOrderId=0
    app.connect('127.0.0.1',4002,0)

    # Call stop() after 3 seconds to disconnect the program
    Timer(3,app.stop).start()
    
    app.run()

if __name__=="__main__":
    main()

1

There are 1 answers

3
greg_data On

When defining the TestApp class, you define nextOrderId as a method on the class.

Within TestApp.start you call this method:

    for o in bracket:
        self.placeOrder(o.orderId, contract, o)
        self.nextOrderId()

However

Within sendorder you set app.nextOrderId = 0

So you are overwriting the method with an integer, and when the code tries to run self.nextOrderId() it's trying to call that integer as a method, which doesn't make sense, hence the error typeerror: 'int' object is not callable.