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订单。最后,在订单完成之前,程序会一直等待。

相关内容

热门资讯

透视苹果版!智星德州有挂吗!的... 透视苹果版!智星德州有挂吗!的确是真的有挂((2020已更新))(哔哩哔哩)1、每一步都需要思考,不...
透明规律!广东雀神麻雀调胜率(... 1、透明规律!广东雀神麻雀调胜率(辅助挂)确实是真的有挂(有挂解惑)-哔哩哔哩;详细教程。2、广东雀...
关于!易棋游戏外挂(辅助挂)确... 关于!易棋游戏外挂(辅助挂)确实真的有挂(有挂神器)-哔哩哔哩;《WPK辅助透视》‌:支持手机实时对...
九分钟普及!wpk外挂(透视)... 九分钟普及!wpk外挂(透视)透视辅助((2022已更新))(哔哩哔哩)1)wpk外挂辅助挂:进一步...
专业讨论!pokerworld... 专业讨论!pokerworld下载外挂!其实真的有挂((2020已更新))(哔哩哔哩);最新版202...
热点推荐!微信小程序雀神提高胜... 热点推荐!微信小程序雀神提高胜率(辅助挂)其实真的有挂(真的有挂)-哔哩哔哩;揭秘教程安装方法样式多...
六分钟了解!gg扑克发牌(透视... 您好,gg扑克这款游戏可以开挂的,确实是有挂的,需要了解加微【485275054】很多玩家在这款游戏...
一分钟了解!功夫川麻辅助工具大... 1、一分钟了解!功夫川麻辅助工具大全(辅助挂)其实是真的有挂(有挂教程)-哔哩哔哩;该软件可以轻松地...
一分钟了解!wepokeai代... 一分钟了解!wepokeai代打!确实真的有挂((2020已更新))(哔哩哔哩);超受欢迎的wepo...
透明插件!广东雀神小程序打才能... 透明插件!广东雀神小程序打才能赢(辅助挂)果真是真的有挂(揭秘有挂)-哔哩哔哩关于广东雀神小程序打才...