Skip to Content
While25SDK docs
Trading

Futures

One futures contract from its root, such as ES or NQ, and how rolls are handled.

c = while25.future("MNQ")              # the contract the chart's continuous MNQ is on now
while25.buy(c["instrument_key"], 1, stop_loss=20950)
while25.future("MNQ", "next")          # the one after it, for a roll
while25.future("MNQ", "2027-03")       # a named contract month

The chart's continuous line (MNQ1!) is no contract and takes no order: an order on it is refused on every broker. future() is how a strategy gets from a root to the dated contract to trade, worked out now from the contracts the Futures panel lists. It turns nothing on and keeps nothing.

ArgumentTakes
rootthe root as the Futures panel names it ("MNQ", "NIFTY", "CRUDEOIL"); "ROOT@MIC" where two exchanges of one market list the same root
expiry"front" (the default), "next", or a contract month ("2027-03", "202703")

"front" is the contract the chart's continuous line for the root is on now, so it rolls when that line rolls: on IBKR and Upstox after the first full session in which the next contract traded more than it (at the latest after its last trading day), on Zerodha at expiry. Where the venue draws no continuous line there is no front to follow: "front" and "next" are refused, and a named month works. "next" is the first contract of a later month than the front. A month is that month's contract, as its key names it: MNQ's "2027-03" is FUT:US:MNQ:202703, MNQH7.

Nothing is rolled for you. It is worked out on every call; when the front has moved on, the strategy closes the old contract and opens the new one with orders of its own (a bracket's close is square_off(), under Orders):

c = while25.future("MNQ")
if self.bracket_id and c["instrument_key"] != self.contract:
    while25.orders.square_off(self.bracket_id)          # out of the old contract
    res = while25.buy(c["instrument_key"], 1, stop_loss=self.stop_for(c))
    self.contract, self.bracket_id = c["instrument_key"], res["order_id"]

It returns a dict: instrument_key, symbol, root, month ("YYYY-MM"), last_trade (ISO date, or None when the venue states none), lot_size and multiplier (Decimal, or None when the venue states none), currency, tradeable and untradeable_reason (an untradeable contract's orders are refused, the reason says why), and front (True when it is the contract the chart's line is on now; None for a named month, which does not look). The contract it names is on the venue's list, so its first order is not refused as unknown. The first lookup of a root on IBKR, and the line's first look at its contracts, wait while the venue answers, up to 30 seconds.

While25Error.codewhen
NO_ROOTnot a root: a contract's key, or nothing
NO_FUTURESthe venue lists no live futures for the root
AMBIGUOUS_ROOTtwo exchanges list the root; the message names ROOT@MIC for each
NO_FRONT"front" or "next" where the venue draws no continuous line for the root: name a month (the message lists them)
NO_NEXTno contract after the front is listed yet
NOT_LISTEDthe month is not listed (the message names the listed ones)
AMBIGUOUS_MONTHthe month lists more than one contract (a weekly beside the monthly): order the one you mean by its key
TIMEOUTthe venue was still looking it up after 30 seconds — ask again