【问题标题】:TWS API (Interactive Brokers) - how to catch event and place orderTWS API (Interactive Brokers) - 如何捕捉事件和下订单
【发布时间】:2020-01-26 14:41:39
【问题描述】:

有这样一个问题: 我学习 TWS API(Interactive Brokers),了解方法、类等。 我注册了下单的逻辑控制单元,在这个阶段我不明白 - 如何在终端收到的价格的不同条件下在终端下订单?可能这个问题是通过多线程解决的,但是,我不能完全理解如何实现这个。我求你帮忙。下面是从终端获取数据的代码,在 main () 块中 - 用于下订单的代码。我不明白如何附加到下订单 - 触发条件。 提前感谢您提供的任何帮助和信息。

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

class TestApp(EWrapper,EClient):
    def __init__(self):
        EClient.__init__(self,self)
    def error(self,reqId,errorCode,errorString):
        print("Error:  ",reqId,"  ",errorCode,"  ",errorString)
    def updateMktDepth(self, reqId, position: int, operation: int,side: int, price: float, size: int):
        print(price)
def main():
    app = TestApp()
    app.connect("127.0.0.1", 7497, 0)
    es = Contract()
    es.localSymbol = "ESZ9"
    es.symbol = "ES"
    es.secType = "FUT"
    es.exchange = "GLOBEX"
    es.currency = "USD"
    app.reqMarketDataType(4)
    app.reqMktDepth(1, es, 2, False, [])
    app.reqPositions()
    app.reqAllOpenOrders()
# QUESTION -  how  to place order, using any conditions with price? (for example: if price > 3000)
    order = Order()
    order.account = "DU1656058"
    order.action = "SELL"
    order.totalQuantity = 1
    order.orderType = "LMT"
    order.lmtPrice = 3055
    app.placeOrder(11000, es, order)
    app.run()
    app.disconnect()
if __name__ == "__main__":
    main()

【问题讨论】:

    标签: python api interactive-brokers tws


    【解决方案1】:

    TWS API 可以设置订单提交标准,例如价格条件和交易量条件。官方文档是here。例如,如果OrderTimeCondition,则只会在特定时间之前或之后提交。

    Algorithmic Trading with Interactive Brokers 这本书提供了演示如何设置订单条件的代码。

    【讨论】:

    • 我看过快速的官方文档,现在有另一个问题 - 如果我想创建自己的未结订单条件 - 例如 - 当 SP500 期货上涨 1% 时 - 我在 AAPL 开多头股票。或者信号不仅可以是合约价格,还可以是组合:SP500 / 道琼斯迷你期货和一些比较 - 将是我为开仓设置的。我可以通过使用官方的“条件方法”来解决这个问题吗?多谢一次!!!
    • 我不认为你可以通过订单条件做到这一点,但你可以编写代码来监控 SPX 并在 SPX 上涨 1% 时自动提交 AAPL (placeOrder) 订单。
    • 可能你知道如何构建任何自己的条件?我认为有一个简单的原则可以做到这一点。我可以从 TWS 获取所有数据,对其进行转换,进行任何计算,写入数据库等等,但在 order exec 的条件构建中不能灵活))可能是我很愚蠢,或者 TWS api 有点奇怪?! ))希望第一))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 2022-11-28
    • 2022-08-24
    相关资源
    最近更新 更多