如何从Interactive Brokers API获取合同详细信息? [英] How to obtain Contract Details from the Interactive Brokers API?

查看:111
本文介绍了如何从Interactive Brokers API获取合同详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照 Interactive Brokers文档,我试图获取合同详细信息使用以下代码:

Following the Interactive Brokers documentation I am trying to obtain the contract details using the below code:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper

class MyWrapper(EWrapper):

    def contractDetails(self, reqId, contractDetails):
        super().contractDetails(reqId, contractDetails)

        print("ContractDetails. ReqId:", reqId,
              contractDetails.summary.symbol,
              contractDetails.summary.secType,
              "ConId:", contractDetails.summary.conId,
              "@", contractDetails.summary.exchange)

    def contractDetailsEnd(self, reqId):
        super().contractDetailsEnd(reqId)
        print("ContractDetailsEnd. ", reqId, "\n")


wrapper = MyWrapper()
app = EClient(wrapper)
app.connect("127.0.0.1", 7497, clientId=0)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(), app.twsConnectionTime()))

from ibapi.contract import Contract
contract = Contract()
contract.symbol = "XAUUSD"
contract.secType = "CMDTY"
contract.exchange = "SMART"
contract.currency = "USD"

app.reqContractDetails(4444, contract)
app.run()

返回的输出是:

serverVersion:148 connectionTime:b'20190117 17:11:38 AEST'

An exception has occurred, use %tb to see the full traceback.

SystemExit


C:\Users\Greg\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2969: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

如何从Interactive Brokers API获取合同详细信息?我尝试使用%tb,但不认为我把它放在正确的行上.

How to obtain the contract details from the Interactive Brokers API? I tried using %tb but don't think I put it on the correct line.

推荐答案

from ibapi.client import EClient
from ibapi.wrapper import EWrapper


class MyWrapper(EWrapper):

    def nextValidId(self, orderId:int):
        print("setting nextValidOrderId: %d", orderId)
        self.nextValidOrderId = orderId
        # start program here or use threading
        app.reqContractDetails(4444, contract)

    def contractDetails(self, reqId, contractDetails):
        print(reqId, contractDetails.contract)# my version doesnt use summary

    def contractDetailsEnd(self, reqId):
        print("ContractDetailsEnd. ", reqId)
        # this is the logical end of your program
        app.disconnect() # delete if threading and you want to stay connected

    def error(self, reqId, errorCode, errorString):
        print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)


wrapper = MyWrapper()
app = EClient(wrapper)
app.connect("127.0.0.1", 7497, clientId=123)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(), app.twsConnectionTime()))

from ibapi.contract import Contract
contract = Contract()
contract.symbol = "XAUUSD"
contract.secType = "CMDTY"
contract.exchange = "SMART"
contract.currency = "USD"

app.run() # delete this line if threading

# def runMe():
#     app.run()

# import threading
# thread = threading.Thread(target = runMe)
# thread.start()

# input('enter to disconnect')
# app.disconnect()

在启动消息阅读器之前,您需要数据.也许您在开始数据之前就已将其获取.

You are asking for data before you start the message reader. Maybe you get the data before it starts.

IB建议您在收到nextValidId之后启动程序,以使您知道一切正常运行.由于python API在消息读取循环中阻塞,因此您需要实现线程化或结构化程序以异步运行.

IB recommends starting the program after you receive nextValidId so you know everything is running properly. Since the python API blocks in a message read loop you need to implement threading or structure your program to run asynchronously.

我已经展示了如何做到这一点,因此它将在没有用户输入的情况下运行,并且是事件驱动的或异步的.这意味着程序会等到应该执行某项操作后再执行.

I've shown how to do it so it will just run with no user input and it is event driven, or asynchronous. This means the program waits until it is supposed to do something and then it does it.

我包含了线程选项,只需更改注释即可.

I've including the threading option, just change the comments.

ContractDetails.summary已更改为合同.我不确定它是否是python中的摘要,不知道您从哪里得到的.

ContractDetails.summary has been changed to contract. I'm not sure it ever was summary in python, don't know where you got that from.

这篇关于如何从Interactive Brokers API获取合同详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆