获取列出的选项和参数Interactive Brokers API中的期货 [英] Getting parameters of listed options & futures in Interactive Brokers API

查看:73
本文介绍了获取列出的选项和参数Interactive Brokers API中的期货的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多示例说明了如何从盈透证券获取特定资产的价格.但是,当我想获取一项资产的整个期权链时,我不知道列出了哪些特定的行权.期货也一样,我不知道目前有哪些到期日.因此,即对于选项,我只遍历所有可能的警告,然后对每个警告进行遍历,每100条消息也进行一次sleep(1)以避免达到每秒请求数量的限制.显然,这些消息中有许多返回的错误为未为请求找到安全定义".

There are a lot of examples showing how to get particular asset's price from Interactive Brokers. However, when I want to get the whole chain of options for one asset, I don't know which particular strikes are listed. Same for futures, I don't know which expirations are available at the moment. So, i.e., for options, I just loop through all possible strikes and reqMktData for each, also making a sleep(1) every 100 messages to avoid hitting the limit for number of requests per second. Obviously, many of these messages return with error "No security definition has been found for the request".

这似乎是错误的方法,因为它浪费了很多时间在不存在的资产上.有没有更干净的方法可以做到这一点,或者有专门的功能可以实现此目的?

This looks like the wrong approach as it wastes lots of time on non-existing assets. Is there any more clean way to do this, or a special function for such purpose?

推荐答案

contractDetailsEnd的实现处理程序,如Donn Lee所建议.感谢shashkello和Donn Lee.

Implementing handler for contractDetailsEnd as suggested by Donn Lee. Thanks to both shashkello and Donn Lee.

from ib.ext.Contract import Contract
from ib.ext.ContractDetails import ContractDetails
from ib.opt import ibConnection, message
import time

def watcher(msg):
    print msg

def contractDetailsHandler(msg):
    contracts.append(msg.contractDetails.m_summary)

def contractDetailsEndHandler(msg):
    global DataWait
    DataWait =  False

con = ibConnection()
con.registerAll(watcher)
con.register(contractDetailsHandler, 'ContractDetails')
con.register(contractDetailsEndHandler, 'ContractDetailsEnd')

con.connect()

contract = Contract()
contract.m_exchange     = "SMART"
contract.m_secType      =  "OPT"
contract.m_symbol       = "VTR"
#contract.m_multiplier   = "100"
contract.m_currency     = "USD"


con.reqContractDetails(1, contract)

contracts = [] # to store all the contracts

DataWait = True  ;  i = 0
while DataWait and i < 90:
    i += 1 ; print i,
    time.sleep(1)

con.disconnect()
con.close()

print contracts

这篇关于获取列出的选项和参数Interactive Brokers API中的期货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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