Options
One option contract from a description: expiry, strike or delta, call or put.
c = while25.option("AAPL", expiry="26th", right="CALL", strike="ATM+2")
while25.buy(c["instrument_key"], c["lot_size"])
while25.subscribe(c["instrument_key"], on_tick=self.on_option)option() names ONE listed contract, worked out now from the same expiry and
strike lists the Option Chain panel reads — the whole list of an expiry, not
the panel's window around the money — so a strategy and the panel never name
different contracts, and nothing has to be open first.
| Argument | Takes |
|---|---|
underlying | a symbol ("AAPL", "NIFTY") or a full key ("EQ:US:AAPL") |
expiry | a date ("2026-11-20", a date), or a day of the month ("26", "26th", 26): the next date on or after today that falls on it |
right | "CALL" / "PUT", also "C" / "P" and "CE" / "PE" |
strike | a listed price (230, "24500"), or "ATM", "ATM+N", "ATM-N" |
settlement= | "pm" (the default) or "am": on a date that lists both, which one — SPX monthlies settle on that morning's opening prices, SPXW at the close |
atm_from= | "options" (the default) or "spot": what ATM is measured from — below |
ATM is counted as the Options Builder counts it. ATM is the listed strike
nearest the money, a tie going to the higher strike. ATM+N is N strikes
higher and ATM-N N strikes lower, for calls and puts alike — on a
put, ATM+2 is in the money. A step is one listed strike, not a fixed price
distance.
atm_from="options": the money is the expiry's forward, measured from the call and put prices near the money — the figure the Options Builder centres on. It keeps moving while an index stands still in its closing auction, and it carries the dividends and interest the expiry is priced on. Where no forward can be measured (no quoted call and put near the money: a shut market, a quiet board) the underlying's price stands, and the result says so inatm_from.atm_from="spot": the money is the underlying's own price — the strike the Option Chain panel marks by default. A source that sends no price for the underlying (SPX on Alpaca) is refused rather than measured another way.
What it turns on. Counting from the money turns on the underlying's live
price when nothing has (it stays on: it is the strategy's from then on). For
"options" it also turns on the call and put prices of the strikes nearest the
money for a moment to measure the forward, and turns off again every one the
strategy did not already hold. A listed price turns nothing on.
How long it takes. It answers from what the platform holds, so usually at once. The first lookup of an underlying, or of an expiry's strikes, on IBKR waits while IBKR answers — up to 30 seconds, the Option Chain panel's own wait; a first price waits up to 5 seconds. The strategy's code waits at that line meanwhile, so look a contract up when you need it — at start, or just before ordering — not on every tick.
It returns a dict:
| Key | |
|---|---|
instrument_key | the contract — what buy / sell / bracket / subscribe take |
symbol | the venue's own name for it |
underlying_key, expiry, settlement, strike (Decimal), right ("CALL" / "PUT") | what was picked |
lot_size | one lot, in units (Decimal): 1 on a US option, the lot on an Indian one. Order quantities are in units, so one lot is quantity=c["lot_size"] and two lots twice it |
multiplier, currency | multiplier is a Decimal, None where the venue states none |
atm_strike, offset, atm_from, atm_price | for an ATM strike: the strike counted from, N, what the money was measured from ("options" or "spot") and that figure |
It never names a neighbouring contract instead. A description that names no
listed contract raises While25Error, with nothing turned on or placed;
malformed arguments raise ValueError.
| code | meaning |
|---|---|
NO_UNDERLYING, NO_OPTIONS | the underlying is not an instrument name, or lists no options |
NO_EXPIRY | no expiry on that date (the message names the nearest before and after), or the date's only expiry settles at the other time (pass settlement=) |
NO_STRIKE | the price is not a listed strike (the message names the nearest listed) |
OFF_THE_LIST | ATM±N runs off either end of the strike list, or the money is beyond the listed strikes |
NO_PRICE | an ATM strike with no price to find the money on yet |
NOT_LISTED | the board lists no call (or no put) at that strike |
TIMEOUT | the venue was still looking it up after 30 seconds — ask again |
The underlying and the strike from the settings, the trigger on the index's own bars:
import while25
from while25 import Strategy, on
from while25.inputs import Enum, Float, Str
class EtfBreakout(Strategy):
id = "etf_breakout"
inputs = [Enum("etf", choices=["SPY", "QQQ"], default="SPY"),
Float("level", default=600.0),
Str("strike", default="ATM+1")]
@on.bar("EQ:US:SPY", "5m") # both written out: a bar line
@on.bar("EQ:US:QQQ", "5m") # takes a symbol from the file
def on_bar(self, ctx, bar):
if bar["symbol"] != "EQ:US:" + while25.params["etf"]:
return # the other ETF's bar
if bar["close"] > while25.params["level"]:
c = while25.option(while25.params["etf"], expiry="26th",
right="CALL", strike=while25.params["strike"])
while25.buy(c["instrument_key"], c["lot_size"])
while25.subscribe(c["instrument_key"], on_tick=self.on_option)
def on_option(self, ctx, tick):
... # every tick of the option boughtoption() works in a running strategy or app. A one-shot run is refused
NOT_IN_RUN, and a call while the file is being loaded (at import time)
IMPORT_TIME_DATA.