简体   繁体   English

IB Python API - 下订单

[英]IB Python API - Placing an Order

I would like to place an order using the native IB Python API.我想使用本机 IB Python API 下订单。

The code I am using for this is the following:我为此使用的代码如下:

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


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

    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, parentId, 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('Order Executed: ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId,
              execution.orderId, execution.shares, execution.lastLiquidity)

    def start(self):
        contract = Contract()
        contract.symbol = "DAI"
        contract.secType = 'STK'
        contract.exchange = 'SMART'
        contract.currency = "EUR"
        contract.primaryExchange = "SMART"

        order = Order()
        order.action = "Buy"
        order.totalQuantity = 1 
        order.orderType =  "MKT"
        order.lmtPrice = 99

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

def  main():
    app = TestApp()
    app.nextOrderId = 1
    app.connect('127.0.0.1', 7497, 123)

    Timer(3, app.stop).start()
    app.run()

if __name__ == "__main__":
    main()

The output I am getting is:我得到的输出是:

Error:  -1   2104   Market data farm connection is OK:usfarm.nj
Error:  -1   2104   Market data farm connection is OK:eufarm
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:secdefeu

But no order is placed.但是没有下订单。

I have placed the ibapi-folder in my working directory and logged into my TWS paper trading account.我已将 ibapi 文件夹放在我的工作目录中并登录到我的 TWS 模拟交易账户。

Why is no order being placed?为什么没有下单?

You don't place the order.你不下订单。 https://interactivebrokers.github.io/tws-api/order_submission.html https://interactivebrokers.github.io/tws-api/order_submission.html

Add self.placeOrder(self.nextOrderId, contract, order) after making the order.下单后添加self.placeOrder(self.nextOrderId, contract, order)。

Don't forget a nextOrderId += 1.不要忘记 nextOrderId += 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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