Disclaimer

This is sample code for learning only. It is not advice, and it is not a recommendation to trade.

This code can have bugs. Read the code and check it yourself. Test it first on Sandbox (UAT / Test — NOT live).

You are fully responsible for any live trade and for any loss. Markets are risky. You can lose money.

INFINN (HKKR Finserv Pvt Ltd) is not responsible for losses from using this code.

Questions: [email protected]

SEBI retail algo framework

If you use a ready-made strategy on a live account, the exchange may need to approve it first. Do not go live until this is checked. See the SEBI retail algo rules and write to [email protected].

Private by design

The program is built in your browser from infinn_straddle_strangle.py. The choices you make are not saved on a server. The program has no keys or secrets. The toolkit reads them from environment variables (a place on your computer, not in the file).

On this page

5 easy steps

  1. Install Python. You need Python 3.9 or later. Get it from python.org. On Windows, tick the box that says “Add Python to PATH”.
  2. Install the INFINN toolkit. Download infinn-0.1.2-py3-none-any.whl, then run:
Bash
pip install ./infinn-0.1.2-py3-none-any.whl "websocket-client>=1.6"

The short command pip install infinn is not on PyPI yet. Use the wheel file.

  1. Set your key. Put your API key and secret in environment variables. Do not type them into the program. On Windows Command Prompt:
bat
set INFINN_API_KEY=your_app_key
set INFINN_API_SECRET=your_api_secret
  1. Get your code. On this page, pick an index, pick Straddle or Strangle, then set your stop loss, target and lots. You can start from a ready plan below. Tick I have read this, then click Copy code or Download. You get one .py file.
  2. Run it on Sandbox (UAT / Test — NOT live). This starts as a dry run, so no orders are sent.
Bash
python infinn_nifty_straddle_sell.py --entry now
python infinn_nifty_straddle_sell.py               # waits for your entry time
python infinn_nifty_straddle_sell.py --place-orders  # real orders, still on Sandbox (UAT / Test — NOT live)

On Windows, you can also download a .bat file and double-click it. Put it in the same folder as the .py file. The program opens the Sandbox login link. Type your User ID in CAPITALS, then your password and TOTP, then paste the full redirect URL. The login is reused for the day.

The trade rules are not made up for each person. Every program is the same tested file (infinn_straddle_strangle.py, engine sha256 4aeeb9647e3128b6…). Only the BUILDER_SETTINGS block changes.

Sandbox notes

Sandbox (UAT / Test — NOT live) currently sends no MCX prices, so a CRUDEOIL or CRUDEOILM test run may show no prices and stop before entry. Use NIFTY, BANKNIFTY or SENSEX to test the full flow. Sandbox prices and fills follow the test system, not the real market.

CRUDEOIL and CRUDEOILM trade on MCX, usually from 09:00 to 23:30 IST. When the US is on daylight time, the session can run until 23:55 IST. The program picks the nearest expiry for you (including on monthly crude options). On expiry day, once the session is nearly over, it moves to the next expiry. The reference price is the near-month MCX future, because crude has no spot index.

Build your program

Start from a ready plan, or fill the three steps. Open More options if you need extra settings. Copy and Download stay off until you tick I have read this. This page never turns --confirm-live on for you.

Step 1. Pick an index

Pick the market you want to use. NIFTY, BANKNIFTY and SENSEX are stock-market indexes. CRUDEOIL and CRUDEOILM (mini) are crude oil on MCX. Sandbox has no MCX prices yet, so a crude test run may show no prices.

Step 2. Pick Straddle or Strangle

A straddle uses a call and a put at the same price — the one nearest the index (called ATM, or at the money). A strangle uses a call and a put a little away from that price. A call is a CE option. A put is a PE option.

Type
Buy or sell?
For a strangle: the call is this many points above the middle price, and the put is this many below.
Step 3. Set stop loss, target and lots

A stop loss is the loss at which we close the trade. A target is the profit at which we close the trade. A lot is one bundle of the option. Start with 1 lot. These numbers are only examples — put in your own.

Start with 1.
More options
Strangle, expiry and size

You can leave these as they are.

1–20. The gap between strikes is read from the option list.
Picks the out-of-the-money call and put whose price is closest to this.
The program picks the nearest expiry for you. You never have to type a date. Choose Next only if you want the one after that. On expiry day, after the session is nearly over, it moves to the next expiry.
The program will not use more than this. The highest you can set here is 10.
When to enter and exit

All times are IST (India time), 24-hour.

No new entry or re-entry after this. Empty = until the exit time.
Every open side is closed at this time. For crude, the program caps the exit at 23:20 IST, or 23:45 IST when the US is on daylight time.
Per-leg stop loss

A leg is one side of the trade: the call or the put. You can also set a stop just for that side. At least one stop loss must be set: per leg, combined, or both.

SELL leg: exit when its price rises by this. BUY leg: when it falls by this.
Only with “Exit only that leg”.
Trailing stop and lock profit

All optional. A trailing stop follows the profit up, then exits if profit falls back. Lock profit means: once profit is high enough, do not let it fall below a floor.

Re-entry and daily limits

Daily limits use the whole account’s day P&L (profit and loss) from the position book.

New ATM / strikes are picked again. Not after a target, time exit or daily limit.
Orders and safety

The defaults are the safe choice. Leave them if you are not sure.

LIMIT price = last price ± this %, so it fills like a market order.
Where to run
Live sends real orders with real money. The program still needs --confirm-live and typing I UNDERSTAND. Check the SEBI retail algo rules with your broker first.
Disclaimer

This is sample code for learning only. It is not advice, and it is not a recommendation to trade.

This code can have bugs. Read the code and check it yourself. Test it first on Sandbox (UAT / Test — NOT live).

You are fully responsible for any live trade and for any loss. Markets are risky. You can lose money.

INFINN (HKKR Finserv Pvt Ltd) is not responsible for losses from using this code.

Questions: [email protected]

You must tick this before you can copy or download the program.

Settings block (this is the only part that changes):

Show the full program (0 lines)

What you can set

ConditionOptionsProgram setting
StrategyStraddle (ATM) or strangleMODE
Strangle strikesBy points from ATM, by number of strikes, or by premium closest to a valueSTRANGLE_BY, STRANGLE_OTM_POINTS, STRANGLE_OTM_STRIKES, STRANGLE_PREMIUM
SideSELL (short) or BUY (long)SIDE
UnderlyingNIFTY, BANKNIFTY, SENSEX, CRUDEOIL, CRUDEOILM (MCX). No GOLD.UNDERLYING
ExpiryPicked for you: the nearest date (current). Optional Next expiry is in More options. You never type a date. On expiry day it rolls to the next one after the session cutoff.EXPIRY
QuantityLots per leg with a safety cap (max 10 in the builder); freeze quantity checkedLOTS, MAX_LOTS
EntryFixed time, X minutes after open, or now; optional last entry timeENTRY_TIME, LAST_ENTRY_TIME
Entry filterCombined CE + PE premium inside a rangeENTRY_MIN_PREMIUM, ENTRY_MAX_PREMIUM
Per-leg stoploss% or points; exit that leg or both; move the other leg to costLEG_SL_PCT / LEG_SL_POINTS, LEG_SL_ACTION, MOVE_SL_TO_COST
Combined stoploss / targetIn ₹, premium points, or % of combined entry premiumMTM_UNIT, COMBINED_SL, COMBINED_TARGET
Trailing stopStart at profit X, exit if profit falls Y from its bestTRAIL_START, TRAIL_GAP
Lock profitOnce profit reaches X, exit if it falls back to YLOCK_TRIGGER, LOCK_MIN
Re-entryAfter a stoploss: max 1–5 times, cooldown in minutesREENTRY_ON_SL, MAX_REENTRIES, REENTRY_COOLDOWN_MINS
Time exitHard exit timeEXIT_TIME
Daily limitsMax loss and max profit for the whole accountMAX_LOSS_PER_DAY_RS, MAX_PROFIT_PER_DAY_RS
OrdersLIMIT with a buffer, or MARKETORDER_TYPE, LIMIT_BUFFER_PCT
SafetySandbox (UAT / Test — NOT live) by default, dry run by defaultENV, DRY_RUN

The program checks exits in this order, every time the price updates: time exit, lost price feed, daily max loss, per-leg stop loss, combined stop loss, target, daily max profit, lock profit, trailing stop. A stop loss always wins over a target on the same update.

Safety

  • Sandbox and dry run by default. Sandbox (UAT / Test — NOT live) is a test system. A dry run means the program only pretends to send orders. Live needs ENV = "live", --confirm-live and typing I UNDERSTAND. This page never sets --confirm-live for you.
  • A stop loss is required. Lots are capped. Times and numbers are checked here, and checked again by the program before it does anything.
  • Every exit closes open sides and tries again if needed. If a close cannot finish, the program prints MANUAL ACTION NEEDED and exits with code 4.
  • No keys, passwords or tokens are ever written into the program.
  • The disclaimer at the top of this page is also next to the Copy and Download buttons, at the top of every generated program, and printed once when the program starts. You must tick I have read this before you can copy or download.

Before any live use

  • Run the dry run and Sandbox (UAT / Test — NOT live) orders on several days, including a stop-loss exit and a Ctrl+C exit.
  • Confirm the SEBI retail algo rules (exchange approval) for your strategy with [email protected].
  • Register your static IP for API orders. Start with one lot and keep the daily max loss on.

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

Esc
↑↓ navigate↵ openEsc close/ to search