简体   繁体   English

在 Interactive Broker 的 API 中为订单添加时间条件

[英]Adding a Time Condition to an order in Interactive Broker's API

Good evening, I need big help and it will be greatly appreciated.晚上好,我需要很大的帮助,我们将不胜感激。 I am trying to add a time condition to my order but I don't understand IBKR's API.我正在尝试为我的订单添加时间条件,但我不了解 IBKR 的 API。

This is the sample code they provide:这是他们提供的示例代码:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import *
from threading import Timer
from ibapi.order_condition import OrderCondition, Create

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("ExecDetails. ", reqId, contract.symbol, contract.secType, contract.currency, execution.execId,
              execution.orderId, execution.shares, execution.lastLiquidity)
    
  

    

    def start(self):
        
        
        contract = Contract()
        contract.symbol = "AAPL"
        contract.secType = "STK"
        contract.exchange = "SMART"
        contract.currency = "USD"
        contract.primaryExchange = "NASDAQ"

        order = Order()
        order.action = "BUY"
        order.totalQuantity = 10
        order.orderType = "MKT"
        
    
        self.placeOrder(self.nextOrderId, contract, order)
        
        



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

def main():
    app = TestApp()
    app.nextOrderId = 0
    app.connect("127.0.0.1", 7497, 9)

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

if __name__ == "__main__":
    main()

The documentation for Order Conditioning is found at:订单条件的文档位于:

https://interactivebrokers.github.io/tws-api/order_conditions.html https://interactivebrokers.github.io/tws-api/order_conditions.html

How do I plug the time condition in the above code.如何在上面的代码中插入时间条件。 Let's say I am buying 100 stocks of AAPL on July 20th 2023 at 2:30pm?假设我在 2023 年 7 月 20 日下午 2:30 买入 100 股 AAPL 股票? Any help would be greatly appreciated.任何帮助将不胜感激。

Here is a sample bit of code that will do what you want.这是一个示例代码,可以满足您的需求。 If you pass an order to this function it will add a time based condition and return the order.如果您将订单传递给此函数,它将添加基于时间的条件并返回订单。

from ibapi.order_condition import OrderCondition, Create

def add_time_condition(order, start_datetime, orth=True):
    
    time_condition = Create(OrderCondition.Time)
    time_condition.time = datetime.strftime(start_datetime, '%Y%m%d %H:%M:%S')
    time_condition.isMore = True
    # isMore = True : specified time is the earliest acceptable time
    # isMore = False: specified time is the latest acceptable time
    time_condition.isConjunctionConnection = "AND"  # alternatively "OR"
    
    order.conditions.append(time_condition)
    order.conditionsIgnoreRth = orth

    return order

You can see that the basic idea is to create an OrderCondition.Time object, and populate it with the relevant parameters (time, isMore, and isConjunctionConnection if multiple conditions are being added).您可以看到基本思想是创建一个 OrderCondition.Time 对象,并使用相关参数(时间、isMore 和 isConjunctionConnection,如果正在添加多个条件)填充它。

You then append that Condition to the conditions parameter of the Order object.然后,您将该 Condition 附加到 Order 对象的 conditions 参数。 The conditions parameter of the Order object is just a list and you can append multiple Conditions to that list. Order 对象的条件参数只是一个列表,您可以将多个条件附加到该列表。 If you are adding multiple Conditions you will need to set the isConjunctionConnection parameter of each Condition.如果要添加多个条件,则需要设置每个条件的 isConjunctionConnection 参数。

A separate parameter on the Order object is the conditionsIgnoreRth - this tells the API whether the condition(s) should operate in extended trading hours or just regular trading hours. Order 对象上的一个单独参数是 conditionsIgnoreRth - 它告诉 API 条件是应该在延长交易时间还是仅在正常交易时间运行。

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

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