0

这是我的代码:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.utils import iswrapper
from ibapi.common import *
from ibapi.contract import *
from ibapi.order import Order
from ibapi.order import OrderComboLeg
from threading import Thread

class OrderApp(EWrapper, EClient):
    
    def __init__(self):
        EClient.__init__(self,self)

    def error(self,reqId:TickerId, errorCode:int, errorString:str):
        print("Error:",reqId," ", errorCode, " ", errorString)

    @iswrapper   
     
    def nextValidId(self, orderId: int):
        super().nextValidId(orderId)
        print("setting nextValidOrderId: %d", orderId)
        self.nextValidOrderId = orderId
        
    def accountSummary(self, reqId: int, account: str, tag: str, value: str,currency: str):
        super().accountSummary(reqId, account, tag, value, currency)
        #print("AccountSummary. ReqId:", reqId, "Account:", account,"Tag: ", tag, "Value:", value, "Currency:", currency)
        print(value)
        
    def accountSummaryEnd(self, reqId: int):
        super().accountSummaryEnd(reqId)
        print("AccountSummaryEnd. ReqId:", reqId)

    def orderStatus(self, orderId: OrderId, status: str, filled: float, remaining: float, avgFillPrice: float, permId: int,parentId: int, lastFillPrice: float, clientId: int,whyHeld: str):
        super().orderStatus(orderId, status, filled, remaining,
        avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld)
        print("OrderStatus. Id:", orderId, "Status:", status, "Filled:", filled,
        "Remaining:", remaining, "AvgFillPrice:", avgFillPrice,
        "PermId:", permId, "ParentId:", parentId, "LastFillPrice:",
        lastFillPrice, "ClientId:", clientId, "WhyHeld:",
        whyHeld)

def run_loop():
    app.run()

def main():
    app.connect("127.0.0.1",7497,5)
    contract = Contract()
    contract.secType = "STK"
    contract.exchange = "SMART"
    contract.currency = 'USD'
    contract.symbol = 'AAPL'
        
    leg1 = ComboLeg()
    leg1.conId = 1
    leg1.ratio = 1    

    leg2 = ComboLeg()
    leg2.conId = 2
    leg2.ratio = 1

    contract.comboLegs = []
    contract.comboLegs.append(leg1)
    contract.comboLegs.append(leg2)
    order = Order()
    order.orderComboLegs = []
    order.action = 'BUY'
    order.orderType = "LMT"
    order.totalQuantity = 10
    order.orderComboLegs = []
    legPrices = 10,5
    for price in legPrices:
        comboLeg = OrderComboLeg()
        comboLeg.price = price
        order.orderComboLegs.append(comboLeg)
    app.placeOrder(69,contract,order)
    ibapi_thread = Thread(target=run_loop,daemon=True)
    ibapi_thread.start()

if __name__ == "__main__":
    app = OrderApp()
    main()

这不起作用,因为 IB API 正在寻找 order.lmtprice,但是根据 IB API 的文档,这是不正确的方法。今天我花了 4 又 1/2 小时才成功下达一个组合订单,而最后两个我一直在努力解决这个问题。谢谢你的帮助!

马多克

4

0 回答 0