首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用ibapi连接python和TWS

无法使用ibapi连接python和TWS
EN

Stack Overflow用户
提问于 2021-01-05 17:00:56
回答 1查看 166关注 0票数 1

这是我的代码:

代码语言:javascript
复制
from threading import Timer
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 contractDetails(self, reqID, contractDetails):
        print('Contract Details :', reqID, '', contractDetails)

    def nextValidId(self, orderId):
        self.nextOrderID = orderId
        self.run()

    def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permID, lastFillprice, cliendId, whyHeld, mktCapPrice):
        print('Orderstatus Id. ', orderId, 'Status: ', status, 'Filled: ', 'Remaining: ', remaining, 'Last Fill Price: ', lastFillprice)

    def openOrderEnd(self, orderId, contract, order, orderState):
        print('Open Order ID. ', orderId, contract.symbol, contract.secType, '@', contract.exchange, ': ', order.action, order.orderType, order.totalQuantity, orderState.status)

    def execDetails(self, reqId, contract, execution):
        print('Exec Details. ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId, execution.orderId, execution.shares, execution.lastLiquidity)

    def accountSummary(self, reqId, account, tag, value, currency):
        print('Account Summary. ', reqId, account, tag, value, currency)

    def start(self):
        contract = Contract()
        contract.symbol = 'NFLX'
        contract.secType = 'STK'
        contract.exchange = 'SMART'
        contract.currency = 'USD'
        contract.primaryExchange = 'NASDAQ'

        order = Order()
        order.action = 'BUY'
        order.totalQuantity = 2
        order.orderType = 'LMT'
        order.lmtPrice = 539.50

        self.placeOrder(self.nextOrderID, contract, order)

    def stop(self):
        self.done = True
        self.disconnect()


def main():
    app = TestApp()
    app.nextOrderID = 0
    app.connect('127.0.0.1', 7497, 0)

    Timer(3, app.stop).start()
    app.run()


if __name__ == '__main__':
    main()

当我执行这段代码时,我只是得到了基本的消息:

错误-1 2104市场数据场连接正常:hfarm错误-1 2104市场数据场连接正常: OK:usfarm.nj错误-1 2104市场数据场连接正常: HMDS未来错误-1 2104市场数据场连接正常:jfarm错误-1 2104市场数据场连接正常:eufarm错误-1 2104市场数据场连接正常:现金农场错误-1 2104市场数据场连接正常:usfarm错误-1 2106 HMDS数据场连接正常:euhmds错误-1 2106 HMDS数据场连接正常:fundfarm错误-1 2106 HMDS数据场连接正常:ushmds ERROR -1 2158 Sec-def data farm connection is OK:secdefnj

我从IBKR在线视频中复制了代码。我不知道我做错了什么。如果有任何帮助,我将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-06 00:06:48

你已经连接上了,你只是从来没有在你的start方法中下过订单。考虑重命名start方法,因为调用Timer.start可能会让人感到困惑。

代码语言:javascript
复制
from threading import Timer
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)
        self.nextOrderID = 0 # if this is init code put it in init

    def Error(self, reqID, errorCode, errorString):
        print('Error :', reqID, '', errorCode,'', errorString)

    def contractDetails(self, reqID, contractDetails):
        print('Contract Details :', reqID, '', contractDetails)

    def nextValidId(self, orderId):
        self.nextOrderID = orderId
        #self.run() you already called app.run
        self.start() # call your start function, you never did so order wasn't placed.
    
    # you missed parentId, just copy these big defs from the source
    def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice):
        print('Orderstatus Id. ', orderId, 'Status: ', status, 'Filled: ', 'Remaining: ', remaining, 'Last Fill Price: ', lastFillPrice)
        #maybe disconnect when order is filled
        if remaining == 0.0:
            self.stop()

    def openOrderEnd(self, orderId, contract, order, orderState):
        print('Open Order ID. ', orderId, contract.symbol, contract.secType, '@', contract.exchange, ': ', order.action, order.orderType, order.totalQuantity, orderState.status)

    def execDetails(self, reqId, contract, execution):
        print('Exec Details. ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId, execution.orderId, execution.shares, execution.lastLiquidity)
        
    def accountSummary(self, reqId, account, tag, value, currency):
        print('Account Summary. ', reqId, account, tag, value, currency)

    def start(self):
        contract = Contract()
        contract.symbol = 'NFLX'
        contract.secType = 'STK'
        contract.exchange = 'SMART'
        contract.currency = 'USD'
        #contract.primaryExchange = 'NASDAQ'#??may be ISLAND, but not needed for nflx

        order = Order()
        order.action = 'BUY'
        order.totalQuantity = 2
        order.orderType = 'LMT'
        order.lmtPrice = 539.50
        
        self.placeOrder(self.nextOrderID, contract, order)
        self.nextOrderID +=1 # always increment after use

    def stop(self):
        #self.done = True # unnecessary 
        if self.isConnected() : 
            print("disconnecting")
            self.disconnect()
        
def main():
    app = TestApp()
    app.connect('127.0.0.1', 7497, 123)

    #Timer(3, app.stop).start() #means you want to stop in 3 seconds no matter what
    app.run()

if __name__ == '__main__':
    main()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65575823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档