ibind icon indicating copy to clipboard operation
ibind copied to clipboard

Any examples for placing options spread orders?

Open itcd opened this issue 7 months ago • 7 comments

The example rest_06_options_chain.py only shows how to retrieve option chains. Could you offer an example for creating options spread orders, including obtaining the CONID of options? Thanks a lot.

itcd avatar May 24 '25 11:05 itcd

Hey @itcd thanks for your request. Unfortunately, that example was contributed by another user, and I wouldn't at the momen have the time capacity to look into your request. Leaving this open in case someone else would like to contribute this example

Voyz avatar May 27 '25 14:05 Voyz

Hi @itcd, is your issue still current? I'm afraid I don't have a ready-made example for an option spread order, however I'm doing those daily. An order request should look similar to this (taken from my logfile):

{'side': 'BUY', 'quantity': 3, 'tif': 'DAY', 'cOID': 'OE S47 T493 ENTRY a06a3995c9ee0162', 'outsideRTH': False, 'orderType': 'LMT', 'price': 10.1, 'conidex': '28812380;;;783941066/-2,783634289/+1,783941086/+1'}

This was for a SPXW butterfly (June 3 expiration, 5950/5980/6010 strikes). Docs on how to put together the conidex field are here: https://www.interactivebrokers.com/campus/ibkr-api-page/cpapi-v1/#combo-orders

Is this what you need? Happy to help if you're stuck at any specific step.

salsasepp avatar Jun 04 '25 09:06 salsasepp

hey @itcd I found some time to look into this. Please see the updated 06 example with option spread order:

https://github.com/Voyz/ibind/blob/master/examples/rest_06_options_chain.py

Hope it helps, let me know 👍

Voyz avatar Jul 03 '25 17:07 Voyz

Hi @Voyz, It seems like ibkr_client.search_contract_by_symbol('SPX').data is only surfacing monthly expirations for SPX options (e.g., SEP25 through DEC30, plus some LEAPs).

I managed to retrieve the below:

SPX Contract ID: 416904 Option Sections Found: [{'secType': 'OPT', 'months': 'SEP25;OCT25;NOV25;DEC25;JAN26;FEB26;MAR26;APR26;MAY26;JUN26;JUL26;AUG26;SEP26;OCT26;DEC26;JAN27;JUN27;DEC27;DEC28;DEC29;DEC30', 'exchange': 'SMART;CBOE;IBUSOPT'}]

This looks like it's pulling from the standard monthly chain via the /iserver/secdef/search endpoint in the Web API (or a similar contract search method in the ibind Python client wrapper). Unfortunately, this endpoint appears to default to or exclusively return the monthly series without including weeklies or other non-standard expirations like SPXW (the trading class for SPX weekly options)? Is this expected?

I came across this: https://www.interactivebrokers.com/campus/trading-lessons/defining-contracts-in-the-tws-api/ but it appears this only works on tws?

kowkowhuang avatar Sep 21 '25 04:09 kowkowhuang

I managed to retrieve the below:

SPX Contract ID: 416904 Option Sections Found: [{'secType': 'OPT', 'months': 'SEP25;OCT25;NOV25;DEC25;JAN26;FEB26;MAR26;APR26;MAY26;JUN26;JUL26;AUG26;SEP26;OCT26;DEC26;JAN27;JUN27;DEC27;DEC28;DEC29;DEC30', 'exchange': 'SMART;CBOE;IBUSOPT'}]

This is normal. If you proceed and use the search_secdef_info_by_conid call with one of the months that are being returned (e.g. "SEP25"), you will get all options for that underlying that expire in September. Filtering for tradingClass should give the SPXW options you're looking for. Does that work for you?

salsasepp avatar Sep 21 '25 10:09 salsasepp

search_secdef_info_by_conid

Unfortunately that's not the case.

When I use search_secdef_info_by_conid call with one of the months that are being returned (e.g. "OCT25"), I'm only getting monthly SPX option which has AM expiry. There is no daily SPX weekly options.

DEBUG: Raw API Response Data: [{'conid': 730198605, 'symbol': 'SPX', 'secType': 'OPT', 'exchange': 'SMART', 'listingExchange': None, 'right': 'C', 'strike': 200.0, 'currency': 'USD', 'cusip': None, 'coupon': 'No Coupon', 'desc1': 'SPX', 'desc2': "OCT 16 '25 200 Call (AM)", 'maturityDate': '20251016', 'multiplier': '100', 'tradingClass': 'SPX', 'validExchanges': 'SMART,CBOE,IBUSOPT', 'showPrips': True}] conid symbol secType exchange listingExchange right strike currency cusip
coupon desc1 desc2 maturityDate multiplier tradingClass
validExchanges showPrips 730198605 SPX OPT SMART C 200.0 USD No Coupon SPX OCT 16 '25 200 Call (AM) 20251016 100 SPX SMART,CBOE,IBUSOPT True

I will wait for the author to comment if this is expected behaviour. @salsasepp do you have a working code that returns SPXW options?

kowkowhuang avatar Sep 21 '25 12:09 kowkowhuang

@salsasepp do you have a working code that returns SPXW options?

Here's working code, pruned down from the rest_06_options_chain.py example. Note I have hard-coded OCT25 as the expiration and 6500 as the strike:

import os
from pprint import pprint

from ibind import IbkrClient, ibind_logs_initialize, OrderRequest, QuestionType
from ibind.support.py_utils import print_table

ibind_logs_initialize()

cacert = os.getenv('IBIND_CACERT', False)  # insert your cacert path here
client = IbkrClient(cacert=cacert, use_session=False)

print('\n#### search for contract ####')
contracts = client.search_contract_by_symbol('SPX').data
spx_contract = contracts[0]
pprint(spx_contract)

print('\n#### validate contract ####')
info = client.search_secdef_info_by_conid(
    conid=spx_contract['conid'], sec_type='OPT', month='OCT25', strike=6500, right='C'
).data
print_table(info)

resulting in

 conid   symbol   secType   exchange   listingExchange   right   strike   currency   cusip      coupon   desc1                         desc2   maturityDate   multiplier   tradingClass       validExchanges   showPrips
809599899      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 01 '25 6500 Call       20251001          100           SPXW   SMART,CBOE,IBUSOPT        True
809892995      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 02 '25 6500 Call       20251002          100           SPXW   SMART,CBOE,IBUSOPT        True
804780220      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 03 '25 6500 Call       20251003          100           SPXW   SMART,CBOE,IBUSOPT        True
809088363      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 06 '25 6500 Call       20251006          100           SPXW   SMART,CBOE,IBUSOPT        True
810473501      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 07 '25 6500 Call       20251007          100           SPXW   SMART,CBOE,IBUSOPT        True
810827534      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 08 '25 6500 Call       20251008          100           SPXW   SMART,CBOE,IBUSOPT        True
811038693      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 09 '25 6500 Call       20251009          100           SPXW   SMART,CBOE,IBUSOPT        True
808651018      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 10 '25 6500 Call       20251010          100           SPXW   SMART,CBOE,IBUSOPT        True
811906865      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 13 '25 6500 Call       20251013          100           SPXW   SMART,CBOE,IBUSOPT        True
812385533      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 14 '25 6500 Call       20251014          100           SPXW   SMART,CBOE,IBUSOPT        True
812750186      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 15 '25 6500 Call       20251015          100           SPXW   SMART,CBOE,IBUSOPT        True
730201321      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX     OCT 16 '25 6500 Call (AM)       20251016          100            SPX   SMART,CBOE,IBUSOPT        True
812883696      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 16 '25 6500 Call       20251016          100           SPXW   SMART,CBOE,IBUSOPT        True
785025201      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 17 '25 6500 Call       20251017          100           SPXW   SMART,CBOE,IBUSOPT        True
813616034      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 20 '25 6500 Call       20251020          100           SPXW   SMART,CBOE,IBUSOPT        True
813868531      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 21 '25 6500 Call       20251021          100           SPXW   SMART,CBOE,IBUSOPT        True
814424049      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 22 '25 6500 Call       20251022          100           SPXW   SMART,CBOE,IBUSOPT        True
814614274      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 23 '25 6500 Call       20251023          100           SPXW   SMART,CBOE,IBUSOPT        True
811510858      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 24 '25 6500 Call       20251024          100           SPXW   SMART,CBOE,IBUSOPT        True
815741218      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 27 '25 6500 Call       20251027          100           SPXW   SMART,CBOE,IBUSOPT        True
815934296      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 28 '25 6500 Call       20251028          100           SPXW   SMART,CBOE,IBUSOPT        True
816316411      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 29 '25 6500 Call       20251029          100           SPXW   SMART,CBOE,IBUSOPT        True
781401834      SPX       OPT      SMART                         C   6500.0        USD           No Coupon     SPX   (SPXW) OCT 31 '25 6500 Call       20251031          100           SPXW   SMART,CBOE,IBUSOPT        True

The example code first asks for all the strikes available and then applies search_secdef_info_by_conid only to the first result from the list (i.e. a lonely call with strike 200). You'll have to loop through all months and all strikes to obtain the complete information.

salsasepp avatar Sep 21 '25 13:09 salsasepp