IBKR: 未找到与请求的合同相关的安全定义。

huangapple go评论68阅读模式
英文:

IBKR: No security definition has been found for the request, contract:

问题

我是新来的IBKR和其API,下面给出的代码在qualifyContracts方法上出现错误:

Error 200, reqId 308: No security definition has been found for the request, contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=808.33, right='C', exchange='CBOE', currency='USD')
Error 200, reqId 309: No security definition has been found for the request, contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=816.67, right='C', exchange='CBOE', currency='USD')
Error 200, reqId 310: No security definition has been found for the request, contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=825.0, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=1.67, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=3.33, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=5.0, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=6.67, right='C', exchange='CBOE', currency='USD')

下面是给出的代码:

from ib_insync import *
from random import getrandbits

if __name__ == '__main__':
    ib = IB()
    client_id = getrandbits(5)
    print('Connecting with the CLIENT ID = ', client_id)
    PORT = 4002
    ib.connect('127.0.0.1', PORT, clientId=client_id)
    contract = Stock('TSLA', 'SMART', 'USD')
    print('Setting Market Data Type')

    ib.reqMarketDataType(3)
    print('1')
    ib.qualifyContracts(contract)
    print('CON ID = ', contract.conId)
    chains = ib.reqSecDefOptParams(contract.symbol, '', contract.secType, contract.conId)
    cboe_chains = [c for c in chains if c.exchange == 'CBOE']

    opts = []
    for chain in cboe_chains:
        print(chain)
        for strike in chain.strikes:
            print(strike)
            option = Option(symbol=chain.tradingClass, lastTradeDateOrContractMonth=chain.expirations[0], strike=strike,
                            exchange='CBOE', currency='USD', right='C')
            opts.append(option)
    print(opts)
    qualified_contract = ib.qualifyContracts(*opts) # Error comes here

希望这对你有所帮助。

英文:

I am new in both IBKR and its API, the code given below is giving error on qualifyContracts method:

Error 200, reqId 308: No security definition has been found for the request, contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=808.33, right='C', exchange='CBOE', currency='USD')
Error 200, reqId 309: No security definition has been found for the request, contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=816.67, right='C', exchange='CBOE', currency='USD')
Error 200, reqId 310: No security definition has been found for the request, contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=825.0, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=1.67, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=3.33, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=5.0, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=6.67, right='C', exchange='CBOE', currency='USD')

The code given below:

from ib_insync import *
from random import getrandbits

if __name__ == '__main__':
    ib = IB()
    client_id = getrandbits(5)
    print('Connecting with the CLIENT ID = ', client_id)
    PORT = 4002
    ib.connect('127.0.0.1', PORT, clientId=client_id)
    # contract = Contract(conId=76792991, exchange='SMART',currency='USD')
    contract = Stock('TSLA', 'SMART', 'USD')
    print('Setting Market Data Type')

    ib.reqMarketDataType(3)
    print('1')
    ib.qualifyContracts(contract)
    print('CON ID = ', contract.conId)
    chains = ib.reqSecDefOptParams(contract.symbol, '', contract.secType, contract.conId)
    cboe_chains = [c for c in chains if c.exchange == 'CBOE']
    # # print(cboe_chains)
    # contracts = [Option(cboe_chains[0].underlyingConId, c.symbol, c.secType, c.exchange, c.currency, c.strike, c.right,
    #                     c.lastTradeDateOrContractMonth) for c in cboe_chains]

    opts = []
    for chain in cboe_chains:
        print(chain)
        for strike in chain.strikes:
            print(strike)
            option = Option(symbol=chain.tradingClass, lastTradeDateOrContractMonth=chain.expirations[0], strike=strike,
                            exchange='CBOE', currency='USD', right='C')
            opts.append(option)
    #
    #         qualified_contract = ib.qualifyContracts(option)
    #         print(qualified_contract)
    #     print('-----------------------------------')
    print(opts)
    qualified_contract = ib.qualifyContracts(*opts) # Error comes here

答案1

得分: 1

以下是您要翻译的内容:

我总是使用默认的主要交易所定义合同SMART并不总是按预期工作此外我不确定' CBOE' 是否是IBKR的有效交易所名称请检查

def contract(
    symbol, sec_type="STK", currency="USD", exchange="SMART", primaryExchange="ISLAND"
):
    contract = Contract()
    contract.symbol = symbol
    contract.secType = sec_type
    contract.currency = currency
    contract.exchange = exchange
    # 指定主要交易所属性以避免合同模糊
    contract.primaryExchange = primaryExchange
    return contract

contract = Stock('TSLA', 'SMART', 'USD', 'NASDAQ')
英文:

I always define the contract with a default primary exchange, SMART does not work as expected all the time. Also, I am not sure if 'CBOE' is a valid exchange name for IBKR. Check this:

def contract(
    symbol, sec_type="STK", currency="USD", exchange="SMART", primaryExchange="ISLAND"
):
    contract = Contract()
    contract.symbol = symbol
    contract.secType = sec_type
    contract.currency = currency
    contract.exchange = exchange
    # Specify the Primary Exchange attribute to avoid contract ambiguity
    contract.primaryExchange = primaryExchange
    return contract


contract = Stock('TSLA', 'SMART', 'USD', 'NASDAQ')

huangapple
  • 本文由 发表于 2023年3月4日 03:35:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定