python - TWS API(盈透证券)-如何捕获事件并下订单

标签 python api interactive-brokers tws

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

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

最佳答案

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

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

关于python - TWS API(盈透证券)-如何捕获事件并下订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58114763/

相关文章:

python - 使用正则表达式避免在文本的开头和结尾出现字符

python - sklearn 中的 RFECV,来自 grid_scores_ 的分数

java - 监视突出显示的文本

python - 在云服务器中运行交互式经纪人客户端门户 API

c++ - 盈透证券 C++ 错误 : error: 'min' was not declared in this scope

c++ - 盈透证券为单个订单返回多个 "presubmitted"订单状态

python - OpenCV Python:cv2.findContours返回许多轮廓

python - 在 numpy 数组 ((x, y, z)...) 中搜索与最近的 x, y 匹配的 z

php - 使用 Linkedin OAuth api : keeps asking permissions authentication 登录

api - Golang Api 只匹配最后一条路线