You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python对接Interactive Brokers遇Error 200及无效行权价问题求助

问题描述

扫描期权时遇到大量Error 200错误,还出现不存在的行权价(例如TSLA行权价177.33)。相关代码及部分错误信息如下:

相关代码

def init_contract(symbol):
    contract = Contract()
    contract.symbol = symbol
    contract.currency = "USD"
    if symbol == 'SPX':
        contract.secType = "IND"
        contract.exchange = 'CBOE'
    else:
        contract.secType = "STK"
        contract.exchange = 'SMART'
    return contract
def get_symbols_nasdaq(include_company_data = False):
    ftp = ftplib.FTP("ftp.nasdaqtrader.com")
    ftp.login()
    ftp.cwd("SymbolDirectory")
    
    r = io.BytesIO()
    ftp.retrbinary('RETR nasdaqlisted.txt', r.write)
    
    if include_company_data:
        r.seek(0)
        data = pd.read_csv(r, sep = "|")
        return data

部分错误信息

Error 200, reqId 4683: The contract description specified for WCBR is ambiguous., contract: Contract(secType='STK', symbol='WCBR', exchange='SMART', currency='USD')
Error 200, reqId 4684: The contract description specified for WCLD is ambiguous., contract: Contract(secType='STK', symbol='WCLD', exchange='SMART', currency='USD')
Error 200, reqId 4692: No security definition has been found for the request, contract: Contract(secType='STK', symbol='WESTW', exchange='SMART', currency='USD')
Error 200, reqId 4699: No security definition has been found for the request, contract: Contract(secType='STK', symbol='WGSWW', exchange='SMART', currency='USD')
Error 200, reqId 4709: The contract description specified for WINA is ambiguous., contract: Contract(secType='STK', symbol='WINA', exchange='SMART', currency='USD')
Error 200, reqId 4711: The contract description specified for WING is ambiguous., contract: Contract(secType='STK', symbol='WING', exchange='SMART', currency='USD')
Error 200, reqId 4716: No security definition has been found for the request, contract: Contract(secType='STK', symbol='WINVW', exchange='SMART', currency='USD')
Error 200, reqId 4726: No security definition has been found for the request, contract: Contract(secType='STK', symbol='WKSPW', exchange='SMART', currency='USD')
Error 200, reqId 4729: No security definition has been found for the request, contract: Contract(secType='STK', symbol='WLDSW', exchange='SMART', currency='USD')
Error 200, reqId 4734: The contract description specified for WNDY is ambiguous., contract: Contract(secType='STK', symbol='WNDY', exchange='SMART', currency='USD')
Error 200, reqId 4737: The contract description specified for WOOD is ambiguous., contract: Contract(secType='STK', symbol='WOOD', exchange='SMART', currency='USD')
Error 200, reqId 4742: The contract description specified for WRLD is ambiguous., contract: Contract(secType='STK', symbol='WRLD', exchange='SMART', currency='USD')
Error 200, reqId 4770: No security definition has been found for the request, contract: Contract(secType='STK', symbol='XBIOW', exchange='SMART', currency='USD')
Error 200, reqId 4773: No security definition has been found for the request, contract: Contract(secType='STK', symbol='XBPEW', exchange='SMART', currency='USD')
Error 200, reqId 4783: No security definition has been found for the request, contract: Contract(secType='STK', symbol='XFINW', exchange='SMART', currency='USD')
Error 200, reqId 4795: No security definition has been found for the request, contract: Contract(secType='STK', symbol='XOSWW', exchange='SMART', currency='USD')
Error 200, reqId 4822: No security definition has been found for the request, contract: Contract(secType='STK', symbol='YOTAW', exchange='SMART', currency='USD')
Error 200, reqId 4825: No security definition has been found for the request, contract: Contract(secType='STK', symbol='YSBPW', exchange='SMART', currency='USD')
Error 200, reqId 4833: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZAPPW', exchange='SMART', currency='USD')
Error 200, reqId 4834: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZAZZT', exchange='SMART', currency='USD')
Error 200, reqId 4837: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZBZZT', exchange='SMART', currency='USD')
Error 200, reqId 4839: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZCARW', exchange='SMART', currency='USD')
Error 200, reqId 4841: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZCZZT', exchange='SMART', currency='USD')
Error 200, reqId 4845: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZEOWW', exchange='SMART', currency='USD')
Error 200, reqId 4848: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZFOXW', exchange='SMART', currency='USD')
Error 200, reqId 4857: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZJZZT', exchange='SMART', currency='USD')
Error 200, reqId 4862: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZLSWW', exchange='SMART', currency='USD')
Error 200, reqId 4866: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZOOZW', exchange='SMART', currency='USD')
Error 200, reqId 4868: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZPTAW', exchange='SMART', currency='USD')
Error 200, reqId 4873: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZURAW', exchange='SMART', currency='USD')
Error 200, reqId 4876: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZVZZT', exchange='SMART', currency='USD')
Error 200, reqId 4877: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZWZZT', exchange='SMART', currency='USD')
Error 200, reqId 4878: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZXYZ.A', exchange='SMART', currency='USD')
Error 200, reqId 4879: No security definition has been found for the request, contract: Contract(secType='STK', symbol='ZXZZT', exchange='SMART', currency='USD')
解决方法

一、处理Error 200错误

Error 200分两种情况:合约描述模糊(ambiguous)、找不到合约定义(No security definition),针对性处理:

1. 过滤无效/特殊后缀的标的

错误信息里的大量标的带W、ZT等后缀,这些通常是权证、优先股或已退市标的,不在IB常规股票合约范围内。修改get_symbols_nasdaq函数过滤这类标的:

def get_symbols_nasdaq(include_company_data = False):
    ftp = ftplib.FTP("ftp.nasdaqtrader.com")
    ftp.login()
    ftp.cwd("SymbolDirectory")
    
    r = io.BytesIO()
    ftp.retrbinary('RETR nasdaqlisted.txt', r.write)
    
    r.seek(0)
    data = pd.read_csv(r, sep = "|")
    # 过滤带特殊后缀的标的,保留普通普通股
    data = data[~data['Symbol'].str.contains(r'[WZ]\d*$|\.A$|ZT$', regex=True)]
    # 过滤已退市标的
    data = data[data['Market Category'] == 'Q']
    
    if include_company_data:
        return data
    else:
        return data['Symbol'].tolist()

2. 解决合约模糊问题

ambiguous错误说明同一个代码对应多个合约,需指定更精确的参数:

  • 在init_contract中加入primaryExchange字段,从nasdaqlisted.txt读取标的主交易所:
def init_contract(symbol, primary_exchange=None):
    contract = Contract()
    contract.symbol = symbol
    contract.currency = "USD"
    if symbol == 'SPX':
        contract.secType = "IND"
        contract.exchange = 'CBOE'
    else:
        contract.secType = "STK"
        contract.exchange = 'SMART'
        # 加入主交易所信息
        if primary_exchange:
            contract.primaryExchange = primary_exchange
    return contract

调用时从nasdaq数据中传入primaryExchange字段:

symbols_data = get_symbols_nasdaq(include_company_data=True)
for idx, row in symbols_data.iterrows():
    contract = init_contract(row['Symbol'], row['Listing Exchange'])
    # 后续期权扫描逻辑

3. 增加错误重试与跳过机制

单个标的请求失败时直接跳过或重试1-2次,避免中断整个扫描流程:

def scan_options(symbol, primary_exchange=None):
    contract = init_contract(symbol, primary_exchange)
    try:
        # 期权扫描逻辑
        req_id = get_new_req_id()
        ib.reqContractDetails(req_id, contract)
        # 等待回调处理...
    except Exception as e:
        print(f"扫描{symbol}失败: {e}")
        # 可选:重试一次
        try:
            ib.reqContractDetails(get_new_req_id(), contract)
        except:
            pass
        return None

二、处理不存在的行权价(如TSLA 177.33)

这类情况多是IB返回的行权价包含非标准小数,实际上市期权行权价多为整数或固定间隔小数(如0.5、1),需过滤无效行权价:

  1. 获取期权链后对行权价做合法性校验:
def filter_valid_strikes(strikes, underlying_price):
    # 保留与标的价格差距在合理范围(如±50%)的行权价
    valid_strikes = [s for s in strikes if 0.5 * underlying_price <= s <= 1.5 * underlying_price]
    # 过滤非标准小数行权价,仅保留整数或0.5倍数
    valid_strikes = [s for s in valid_strikes if s % 0.5 == 0]
    return valid_strikes
  1. 若IB数据返回异常,可切换到reqSecDefOptParams获取标准行权价参数,再生成合法行权价:
def get_standard_strikes(symbol):
    contract = init_contract(symbol)
    ib.reqSecDefOptParams(get_new_req_id(), contract.symbol, "", contract.secType, contract.conId)
    # 从回调中获取minStrike、maxStrike、strikeInterval,生成标准行权价

内容的提问来源于stack exchange,提问作者DrH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 04:32:02