简体   繁体   中英

Getting order Id error when running Ibapi / TWS API script

I'm running a test code that is supposed to place a simple order only, but every time I run it I'm getting a weird error that I've never experienced before. Here is a screenshot https://imgur.com/a/vuPfsys . And this is my script: Help would be really appreciated...

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

import threading
import time

class IBapi(EWrapper, EClient):
    
    def __init__(self):
        EClient.__init__(self, self)
    
    def nextValidId(self, orderId: int):
        super().nextValidId(orderId)
        self.nextorderId = orderId
        print('The next valid order id is: ', self.nextorderId)

    def execDetails(self, reqId, contract, execution):
        print('Order Executed: ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId, execution.orderId, execution.shares)


def run_loop():
    app.run()

def makeContract():
    contract = Contract()
    contract.symbol = 'AAPL'
    contract.secType = 'STK'
    contract.exchange = 'SMART'
    contract.currency = 'USD'
    return contract

app = IBapi()
app.connect('127.0.0.1', 7497, 999)

app.nextorderId = None

#Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()

#Check if the API is connected via orderid
while True:
    if isinstance(app.nextorderId, int):
        print('connected')
        print()
        break
    else:
        print('waiting for connection')
        time.sleep(1)

#Create order object
order = Order()
order.action = 'BUY'
order.totalQuantity = 2000
order.orderType = 'MKT'
order.orderId = app.nextorderId
app.nextorderId += 1



#Place order
app.placeOrder(order.orderId, makeContract, order)

#wait for callbacks
time.sleep(20)

app.disconnect()

Thank you so much

You probably want to call the function makeContract. You're passing the function object instead of the result.

app.placeOrder(order.orderId, makeContract(), order)

Note the parentheses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM