API IB TWS Python Bracket问题
创始人
2024-09-07 02:00:21
0

以下是一个使用IB TWS API和Python解决Bracket问题的示例代码:

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

class BracketTrader(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
        self.nextOrderId = None
    
    def nextValidId(self, orderId: int):
        super().nextValidId(orderId)
        self.nextOrderId = orderId

    def placeBracketOrder(self, symbol, quantity, limitPrice, stopLossPrice, takeProfitPrice):
        # 创建合约对象
        contract = Contract()
        contract.symbol = symbol
        contract.secType = "STK"
        contract.exchange = "SMART"
        contract.currency = "USD"
        
        # 创建主订单
        mainOrder = Order()
        mainOrder.action = "BUY"
        mainOrder.orderType = "LMT"
        mainOrder.totalQuantity = quantity
        mainOrder.lmtPrice = limitPrice
        
        # 创建止损订单
        stopLossOrder = Order()
        stopLossOrder.action = "SELL"
        stopLossOrder.orderType = "STP"
        stopLossOrder.totalQuantity = quantity
        stopLossOrder.auxPrice = stopLossPrice
        
        # 创建止盈订单
        takeProfitOrder = Order()
        takeProfitOrder.action = "SELL"
        takeProfitOrder.orderType = "LMT"
        takeProfitOrder.totalQuantity = quantity
        takeProfitOrder.lmtPrice = takeProfitPrice
        
        # 发送订单
        self.placeOrder(self.nextOrderId, contract, mainOrder)
        self.nextOrderId += 1
        self.placeOrder(self.nextOrderId, contract, stopLossOrder)
        self.nextOrderId += 1
        self.placeOrder(self.nextOrderId, contract, takeProfitOrder)
        self.nextOrderId += 1

def main():
    app = BracketTrader()
    app.connect("127.0.0.1", 7497, 0)

    # 等待连接成功
    while not app.isConnected():
        pass

    # 发送Bracket订单
    app.placeBracketOrder("AAPL", 100, 150, 140, 160)

    # 等待订单完成
    while app.nextOrderId > 0:
        pass

    # 断开连接
    app.disconnect()

if __name__ == "__main__":
    main()

上述代码中,BracketTrader类继承了EWrapperEClient类,并重写了nextValidId方法,用于获取下一个有效的订单ID。placeBracketOrder方法用于创建并发送Bracket订单,包括主订单、止损订单和止盈订单。main函数创建了一个BracketTrader对象,连接到TWS API,并发送Bracket订单。最后,在订单完成之前,程序会一直等待。

相关内容

热门资讯

关于!微信微乐自建房辅助工具,... 关于!微信微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)方法1)微乐家乡麻将自建房辅...
大家学习交流!微信卡五星辅助器... 大家学习交流!微信卡五星辅助器,微乐小程序免费黑科技(开挂自建房)方法1、很好的工具软件,可以解锁游...
三分钟了解!微乐微信自建房辅助... 三分钟了解!微乐微信自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具1、三分钟了解...
一分钟教会你!微乐家乡麻将自建... 一分钟教会你!微乐家乡麻将自建房辅助app,微乐自建房脚本免费入口(开挂自建房)方法1、用户打开应用...
分享实测!微信微乐自建房辅助工... 分享实测!微信微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)器1、起透看视 微信微乐自建房...
重大通报!微乐自建房辅助工具脚... 重大通报!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)app微乐自建房辅助工具脚本破解...
如何分辨真伪!微乐微信自建房辅... 如何分辨真伪!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)技巧1、微乐微信自建房辅...
我来教教你!微乐自建房辅助工具... 我来教教你!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)工具1、玩家可以在微乐家...
重大来袭!微乐自建房辅助工具神... 重大来袭!微乐自建房辅助工具神器,微乐小程序免费黑科技(开挂自建房)器1、游戏颠覆性的策略玩法,独创...
带你了解!微信卡五星辅助器,微... 带你了解!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)方法1、带你了解!微信卡五星辅助器,...