6.3. TWS API v9.72 在线文档 ——订单管理/替换订单【翻译】

Order Management(订单管理)

·      Placing Orders

·      Modifying Orders

·      Cancelling Orders

·      Retrieving currently active orders

·      Executions and Commissions

·      Order Limitations

·      MiFIR Transaction Reporting Fields

Placing Orders(换订单)

The Next Valid Identifier

Perhaps the most important event received after successfully connecting to the TWS is the IBApi.EWrapper.nextValidId, which is also triggered after invoking the IBApi.EClient.reqIds method. As its name indicates, the nextValidId event provides the next valid identifier needed to place an order. This identifier is nothing more than the next number in the sequence. This means that if there is a single client application submitting orders to an account, it does not have to obtain a new valid identifier every time it needs to submit a new order. It is enough to increase the last value received from the nextValidId method by one. For example, if the valid identifier for your first API order is 1, the next valid identifier would be 2 and so on.

【译】对于成功地连接到TWS之后,可能最重要的消息就是接收到“IBApi.EWrapper.nextValidId”,也可以使用IBApi.EClient.reqIds 方法获得。正如它的名字,nextValidId 事件提供了下一个订单的可用标识符。它非常重要,如果在单客户端模式下,不必每次提交订单时都请求,只需要给“IBApi.EWrapper.nextValidId”加1。举例来说,如果你一开始的nextValidId 是1,那么接下来第二份订单的值就是2,以此类推。

However if there are multiple client applications connected to one account, it is necessary to use an order ID with new orders which is greater than all previous order IDs returned to the client application in openOrder or orderStatus callbacks. For instance, if the client is set as the Master client, it will automatically receive order status and trade callbacks from orders placed from other clients. In such a case, any orderID used in placeOrder must be greater than the orderIDs returned in these status callbacks. Alternatively if the function reqAllOpenOrders is used by a client, subsequent orders placed by that client must have order IDs greater than the order IDs of all orders returned because of that function call. You can always use the IBApi.EClient.reqIds method in the event that your client application loses track of the sequence.

【译】如果有多个客户端连接到了一个账户,那么就需要使用给每个订单一个订单ID,这个ID必须要比之前所有的从openOrder 或者orderStatus 回调函数获得的ID要大。举例来说,如果客户端设置为主客户端,它将自动的接收订单状态以及从其他从客户端发送的交易回调返回。在这样的情况下,任何一个在placeOrder 的orderID 都必须比这些返回的orderIDs 要大。如果某个客户端调用了reqAllOpenOrders 函数,新的ID需要比通过这个函数返回的全部的ID的最大值要大(译者注:id > max(reqAllOpenOrders))。因此,你可以使用IBApi.EClient.reqIds 获取到所有已用订单。

        //The parameter is always ignored.

        client.reqIds(-1);

The above will result in IBApi.EWrapper.nextValidId callback being invoked:

【译】这个方法会导致IBApi.EWrapper.nextValidId函数被激活

    publicclassEWrapperImpl implementsEWrapper {

    {

    @Override

    publicvoidnextValidId(intorderId){

        System.out.println("Next Valid Id: ["+orderId+"]");

        currentOrderId =orderId;

    }

The next valid identifier is persistent between TWS sessions.

If necessary, you can reset the order ID sequence within the API Settings dialogue. Note however that the order sequence Id can only be reset if there are no active API orders.

【译】如果需要,你可以在API设置对话框中重制订单ID序列。主意:只有在没有激活订单的情况下,才可以重制订单序列。

Placing Orders

Orders are submitted via the IBApi.EClient.placeOrder method. From the snippet below, note how a variable holding the nextValidId is incremented automatically:

【译】订单由IBApi.EClient.placeOrder 函数提交。注意nextValidId 是需要自增长的。

            client.placeOrder(nextOrderId++,

                ContractSamples.USStock(),

                OrderSamples.TrailingStopLimit("BUY",1,5,5,110));

Immediately after the order was submitted correctly, the TWS will start sending events concerning the order’s activity via IBApi.EWrapper.openOrder and IBApi.EWrapper.orderStatus

【译】如果提交的订单正确,TWS将立即通过IBApi.EWrapper.openOrder 和IBApi.EWrapper.orderStatus 返回关于订单活动状态的信息。

An order can be sent to TWS but not transmitted to the IB server by setting the IBApi.Order.Transmit flag in the order class to False. Untransmitted orders will only be available within that TWS session (not for other usernames) and will be cleared on restart. Also, they can be cancelled or transmitted from the API but not viewed while they remain in the untransmitted state.

【译】可以在订单类中,设置IBApi.Order.Transmit 为False, 阻止订单经由TWS直接提交到IB的服务器。没有提交到服务器的订单,只驻留在TWS中,并在下次重启时失效。尽管可以对这些订单进行取消或传送,但是仍然没有传送的订单,将不可见。

The openOrder callback

The IBApi.EWrapper.openOrder method delivers an IBApi.Order object representing the open order within the system. Additionally, an IBApi.OrderState object containing margin and commission information about this particular order.

【译】IBApi.EWrapper.openOrder 函数,在系统中传递代表开放订单对象的IBApi.Order。另外的,IBApi.OrderState 对象包含该订单的头寸与佣金信息。

 

publicclassEWrapperImpl implementsEWrapper {

    @Override

    publicvoidopenOrder(intorderId,

        Contract contract,Order order,

        OrderState orderState){

        System.out.println(

            EWrapperMsgGenerator.openOrder(orderId,

                contract,order,orderState));

    }

The orderStatus callback

The IBApi.EWrapper.orderStatus method contains all relevant information on the current status of the order execution-wise (i.e. amount filled and pending, filling price, etc.).

【译】IBApi.EWrapper.orderStatus 函数包含订单执行的全部关联信息(例如:成交,挂起,成交价格等)。

publicclassEWrapperImpl implementsEWrapper {

    @Override

    publicvoidorderStatus(intorderId,

        String status,doublefilled,

        doubleremaining,doubleavgFillPrice,

        intpermId,intparentId,

        doublelastFillPrice,

        intclientId,String whyHeld,doublemktCapPrice){

        

        System.out.println(

            "OrderStatus. Id: "+orderId+

            ", Status: "+status+

            ", Filled"+filled+

            ", Remaining: "+remaining+

            ", AvgFillPrice: "+avgFillPrice+

            ", PermId: "+permId+

            ", ParentId: "+parentId+

            ", LastFillPrice: "+lastFillPrice+

            ", ClientId: "+clientId+

            ", WhyHeld: "+whyHeld+

            ", MktCapPrice: "+mktCapPrice);

    }

Automatic Order Status Messages (without invoking reqOpenOrders or reqAllOpenOrders)

自动化订单状态(不激活reqOpenOrders 或reqAllOpenOrders)

·      Clients with the ID of the client submitting the order will receive order status messages indicating changes in the order status.

·      The client with Master Client ID (set in TWS/IBG) will receive order status messages for all clients.

·       Client ID 0 will receive order status messages for its own (client ID 0) orders and also for orders submitted manually from TWS.

·       包含从ID的客端提交订单后会受到订单消息,指示订单的改

·       包含主ID的客端(在TWS/IBG置)会接受到全部从客端的订单消息。

·       ID 0 会收到只关于自己的的订单信息。

Possible Order States

可能的订单状态

·      ApiPending - indicates order has not yet been sent to IB server, for instance if there is a delay in receiving the security definition. Uncommonly received.

·      ApiPending指示订单没有送到IB的服器上,如:接受安全资产有延。不通常收到。

·      PendingSubmit - indicates the order was sent from TWS, but confirmation has not been received that it has been received by the destination. Most commonly because exchange is closed.

·      PendingSubmit指示订单经发TWS,但是确消息未收到。通常因交易所休市,会常收到。

·      PendingCancel - indicates that a request has been sent to cancel an order but confirmation has not been received of its cancellation.

·      PendingCancel指示取消订单请求已经发送但是确消息未收到。

·      PreSubmitted - indicates that a simulated order type has been accepted by the IB system and that this order has yet to be elected. The order is held in the IB system until the election criteria are met. At that time the order is transmitted to the order destination as specified.

·      PreSubmitted指示模拟订单型已IB,但中。该订单IB保留,直到条件符合。订单送到对应的交易所。

·      Submitted - indicates that your order has been accepted at the order destination and is working.

·      Submitted指示你的订单已被接受,并开始行。

·      ApiCancelled - after an order has been submitted and before it has been acknowledged, an API client client can request its cancellation, producing this state.

·      ApiCancelled订单提交后或提交前,订单到,API端可以求取消它,将个状

·      Cancelled - indicates that the balance of your order has been confirmed cancelled by the IB system. This could occur unexpectedly when IB or the destination has rejected your order.

·      Cancelled指示由于订单资产负债情况由IB的取消,当IB或目的地取消

·      Filled - indicates that the order has been completely filled.

·      Filled指示订单完全成交。

·      Inactive - indicates an order is not working, possible reasons include:

·      【不活指示订单没有工作,可能原因如下:

o   it is invalid or triggered an error. A corresponding error code is expected to the error() function.

o   它是不合法的,或者激了一个错误对应错误error()中。

o   the order is to short shares but the order is being held while shares are being located.

o   做空池,不能做空股票。

o   an order is placed manually in TWS while the exchange is closed.

o   在交易所关闭时订单TWS修改了。

o   an order is blocked by TWS due to a precautionary setting and appears there in an untransmitted state

o   订单TWS性策略,阻止了交易,将致未送状

Important notes concerning IBApi.EWrapper.orderStatus :

·      Typically there are duplicate orderStatus messages with the same information that will be received by a client. This corresponds to messages sent back from TWS, the IB server, or the exchange.

·      通常的,一个客端会同收到多条重复信息。些消息由TWSIB器,交易所返回。

·      There are not guaranteed to be orderStatus callbacks for every change in order status. For example with market orders when the order is accepted and executes immediately, there commonly will not be any corresponding orderStatus callbacks. For that reason it is recommended to monitor the IBApi.EWrapper.execDetails function in addition to IBApi.EWrapper.orderStatus.

·      不保orderStatus会返回所有的订单例来订单被接受并立即行,就通常情况就不会返回订单。因此,建议监IBApi.EWrapper.execDetails函数作为订单IBApi.EWrapper.orderStatus 充手段。

·      Beginning in API v973.04, a parameter mktCapPrice is included in the orderStatus callback. If an order has been price-capped, mktCapPrice will indicate the price at which it has been capped.

·      v973.04开始,mktCapPrice 参数被加入到了orderStatus 函数中。如果订单已被限制价格,mktCapPrice将指示其上限的价格。

Attaching Orders

Advanced orders such as Bracket Orders or Hedging involve attaching child orders to a parent. This can be easily done via the IBApi.Order.ParentId attribute by assigning a child order’s IBApi.Order.ParentId to an existing order’s IBApi.Order.OrderId. When an order is attached to another, the system will keep the child order ‘on hold’ until its parent fills. Once the parent order is completely filled, its children will automatically become active.

【译】高级订单,包含交叉交易、对冲订单涉及到将子订单附加到父级。通过将子订单的IBApi.Order.ParentId分配给现有订单的IBApi.Order.OrderId轻松完成。当订单附加到另一个订单时,系统将保留子订单,直到其父订单成交。父订单完成后,其子将自动变为活动状态。

Important: When attaching orders and to prevent accidental executions it is a very good idea to make use of the IBApi.Order.Transmit flag as demonstrated in Bracket Orders

当附加订单时,为了避免执行过程中产生意外,在交叉订单中使用IBApi.Order.Transmit 标记是个很好的习惯。

 

猜你喜欢

转载自blog.csdn.net/poisonchry/article/details/80435209
今日推荐