Read this first

Educational example code — not investment advice or a recommended strategy. Test on Sandbox (UAT / Test — NOT live) first. Options selling carries unlimited risk.

SEBI retail algo framework

Under the SEBI framework for retail algorithmic trading, a strategy you run through the API may need exchange approval or registration through your broker before you use it on a live account. See SEBI retail algo rules and write to [email protected] before going live.

Prefer a form?

The Strategy Builder asks simple questions and gives you this same program with the settings filled in.

On this page

What it does

  • Login with the SDK: reuses today’s saved session, or opens the browser login (User ID in CAPITALS + password + TOTP) and asks you to paste the full redirect URL.
  • Finds ATM: reads the index spot price (NIFTY, BANKNIFTY or SENSEX) — or, for CRUDEOIL and CRUDEOILM on MCX, the near-month futures price — and rounds it to the nearest strike.
  • Builds the legs from the option chain (option_chain()): ATM CE + ATM PE for a straddle, or CE at ATM + X and PE at ATM − X for a strangle. Lot size and tick size come from the API, never hard-coded.
  • Places both legs — SELL by default, BUY also supported — as marketable LIMIT orders, and waits for each fill. If one leg does not fill fully, the other is squared off: it never leaves you with half a position.
  • Monitors prices over the WebSocket (REST polling as a fallback) and computes live MTM.
  • Exits on per-leg stoploss, combined stoploss, combined target, trailing stop, time exit, daily max loss, a lost price feed, an error, or Ctrl+C — and squares off every open leg with retries.

Download & install

Bash
pip install ./infinn-0.1.2-py3-none-any.whl "websocket-client>=1.6"
# then download infinn_straddle_strangle.py into the same folder

Keys are read from the environment only. On Windows (Command Prompt):

bat
set INFINN_API_KEY=your_app_key
set INFINN_API_SECRET=your_api_secret

Never paste keys or secrets into the program file.

The config block

Everything you can change sits at the top of infinn_straddle_strangle.py. Each line has a comment. You can also override the common ones from the command line (--underlying, --mode, --otm, --lots, --expiry, --entry, --exit). The listing below is that file, from the config through the builder settings block.

infinn_straddle_strangle.py
Loading infinn_straddle_strangle.py…

Stoploss & profit booking

ExitSettingWhat happens
Per-leg stoplossLEG_SL_PCT, LEG_SL_ACTIONA SELL leg exits when its premium rises by this %, a BUY leg when it falls by this %. Close only that leg (exit_leg) or everything (exit_all). Optional MOVE_SL_TO_COST moves the other leg’s stoploss to its entry price.
Combined stoplossCOMBINED_SL, MTM_UNITExit all when the combined MTM falls to −COMBINED_SL, in rupees or premium points.
TargetCOMBINED_TARGETExit all when the combined MTM reaches the target.
Trailing stopTRAIL_START, TRAIL_GAPOnce MTM reaches TRAIL_START, exit if it falls TRAIL_GAP below its best level.
Per-leg stoploss in pointsLEG_SL_POINTSSame as above, but a fixed number of premium points instead of a %. Use one or the other.
Combined % of premiumMTM_UNIT = "pct"Combined stoploss, target, trailing and lock values are read as a % of the combined entry premium.
Profit lockLOCK_TRIGGER, LOCK_MINOnce MTM reaches LOCK_TRIGGER, exit if it falls back to LOCK_MIN.
Daily max profitMAX_PROFIT_PER_DAY_RSExit all when the whole account’s day P&L reaches this profit.
Re-entry after stoplossREENTRY_ON_SL, MAX_REENTRIES, REENTRY_COOLDOWN_MINSAfter a stoploss closes every leg, wait the cooldown and enter again at the new ATM (max 5). Never after a target, time exit or daily limit.
Entry window & filterLAST_ENTRY_TIME, ENTRY_MIN_PREMIUM, ENTRY_MAX_PREMIUMEnter only while the combined CE + PE premium is inside the range, and not after LAST_ENTRY_TIME.
Time exitEXIT_TIMEHard exit at this IST time.
Daily max lossMAX_LOSS_PER_DAY_RSUses the whole account’s day P&L from the position book. The program will not even enter if the limit is already reached.
Lost price feedSTALE_PRICE_EXIT_SECSNo fresh price for this long: exit all.
Error or Ctrl+C—Square off every open leg, then stop.

MTM for a SELL leg is (entry − LTP) × quantity; for a BUY leg it is (LTP − entry) × quantity. Closed legs keep their booked P&L in the total.

Commodity: CRUDEOIL and CRUDEOILM

  • Set UNDERLYING to "CRUDEOIL" or "CRUDEOILM" (or --underlying). There is no spot index, so the reference price is the near-month future on MCX.
  • Options are taken from the MCX option chain for the nearest option expiry. Strike step, lot size and tick size come from the API.
  • PRODUCT = "auto" uses M (NRML) on MCX and I (intraday) on NFO / BFO.
  • The MCX session runs from 09:00 to 23:30 IST, and can run until 23:55 IST when the US is on daylight time. The program caps new entries and the hard exit before that close. On expiry day it rolls to the next expiry about 15 minutes before the session close.
MCX on Sandbox

As tested on 11 Nov 2026, Sandbox lists the CRUDEOIL futures and options (search, security info and option chain work), but it sends no MCX prices through Get Quotes or the WebSocket feed. The program then stops before entry with “no price” and places nothing. Use NIFTY, BANKNIFTY or SENSEX to test the full flow on Sandbox.

Run it on Sandbox

  • Dry run (default — no orders): python infinn_straddle_strangle.py --entry now --exit 15:15. Check the log shows the ATM strike, both legs, lot size, MTM lines and the exit.
  • Sandbox orders: python infinn_straddle_strangle.py --entry now --place-orders. Orders go to Sandbox (UAT / Test — NOT live) only.
  • Try a strangle: --mode strangle --otm 200. Try BUY: --side BUY. After the equity close, try --underlying CRUDEOIL or --underlying CRUDEOILM.
  • Press Ctrl+C once during monitoring and confirm both legs are squared off.
  • Read straddle_strangle.log. Every order, fill, MTM line and exit reason is written there (keys and tokens never are).
Sandbox notes

Sandbox prices and fills follow the test environment, not the real market. Limits on Sandbox can time out; the program logs a warning and carries on, because the broker’s risk system still checks margin on every order.

Safety built in

  • Sandbox and DRY_RUN by default. Live needs --env live --confirm-live and typing I UNDERSTAND.
  • Lot size and freeze quantity: quantity must be a multiple of the lot size; it is checked against the freeze quantity returned by the API (or your FREEZE_QTY_OVERRIDE) and capped by MAX_LOTS.
  • LIMIT orders priced a small buffer through the LTP and rounded to the tick size. Square-off retries widen the buffer, and the last attempt can use MARKET (MARKET_FALLBACK_ON_EXIT).
  • No duplicate orders: each order carries a unique tag. If a PlaceOrder request times out, the program looks for that tag in the order book before doing anything else.
  • Timeouts and retries on read calls only; orders are never retried blindly.
  • If a square-off cannot complete, the program prints MANUAL ACTION NEEDED with the symbol and quantity, and exits with code 4.
  • Stays under the API order rate limit through the SDK’s local throttle.

Before you go live

  • Run the dry run and Sandbox orders on several days, including a stoploss exit and a Ctrl+C exit.
  • Check the SEBI retail algo requirements for your strategy with [email protected].
  • Make sure your static IP is registered for API orders.
  • Start with one lot and keep MAX_LOSS_PER_DAY_RS set.

Full code

Same file as the download.

Show the full program (… lines)
infinn_straddle_strangle.py
Loading infinn_straddle_strangle.py…

Questions: [email protected]. INFINN does not give investment advice.

Esc
↑↓ navigate↵ openEsc close/ to search