【问题标题】:Getting order Id error when running Ibapi / TWS API script运行 Ibapi / TWS API 脚本时出现订单 ID 错误
【发布时间】:2020-12-20 23:28:13
【问题描述】:

我正在运行一个应该只下简单订单的测试代码,但每次我运行它时都会遇到一个我以前从未遇到过的奇怪错误。这是截图https://imgur.com/a/vuPfsys。 这是我的脚本: 非常感谢您的帮助...

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

非常感谢

【问题讨论】:

    标签: python interactive-brokers ibpy ib-api


    【解决方案1】:

    您可能想要调用函数 makeContract。您传递的是函数对象而不是结果。

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

    注意括号。

    【讨论】:

    • 太棒了!我试了一下,脚本下了订单。非常感谢,没有你我做不到。
    猜你喜欢
    • 1970-01-01
    • 2021-03-17
    • 2019-08-23
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多