Skip to content

Code examples

Get your strategy's weight_norm once from a backtest (the /backtest page in the app returns calibrated_weight_norm), then size live with it — per leg (/sizing/leg-size, naked) or the whole structure (/sizing/structure, hedged). Pick the horizon that matches your strategy (intraday / positional); the sizing runs in derisk mode (the default — down-size risky legs, no extra margin).

import os, requests
API_KEY = os.environ["TRADLYT_API_KEY"]
BASE = "https://api-beta.tradlyt.com/api/v1"
HDR = {"X-API-Key": API_KEY, "Content-Type": "application/json"}

Size each leg live

For every leg you're about to place, call /sizing/leg-size. The model returns recommended_lots — trade that instead of your flat base.

WEIGHT_NORM = 0.497        # your backtest's calibrated_weight_norm
BASE_LOTS   = 10
HORIZON     = "intraday"   # "intraday" (same-day) or "positional" (overnight / multi-day)
MODE        = "derisk"     # de-risk: down-size risky legs, no extra margin, lowest drawdown

def size_leg(leg):
    r = requests.post(
        f"{BASE}/sizing/leg-size",
        headers=HDR,
        json={**leg, "base_lots": BASE_LOTS, "weight_norm": WEIGHT_NORM,
              "horizon": HORIZON, "mode": MODE},
        timeout=2,
    )
    r.raise_for_status()
    return r.json()

# 9:30 SENSEX short straddle — size each leg
legs = [
    {"underlying": "SENSEX", "option_type": "CE", "direction": "SELL",
     "strike": 74100, "expiry_date": "2026-06-12"},
    {"underlying": "SENSEX", "option_type": "PE", "direction": "SELL",
     "strike": 74100, "expiry_date": "2026-06-12"},
]
for leg in legs:
    out = size_leg(leg)
    print(f"{leg['option_type']} {leg['strike']}: risk {out['risk']} "
          f"-> {out['recommended_lots']} lots (weight {out['weight']}, {out['mode']})")
    place_order(leg, lots=out["recommended_lots"])    # your broker call
const axios = require('axios');
const API_KEY = process.env.TRADLYT_API_KEY;
const BASE = 'https://api-beta.tradlyt.com/api/v1';
const WEIGHT_NORM = 0.497, BASE_LOTS = 10;   // weight_norm = your backtest's calibrated_weight_norm

async function sizeLeg(leg) {
  const { data } = await axios.post(
    `${BASE}/sizing/leg-size`,
    { ...leg, base_lots: BASE_LOTS, weight_norm: WEIGHT_NORM,
      horizon: 'intraday', mode: 'derisk' },   // mode 'derisk' (default); horizon 'intraday' or 'positional'
    { headers: { 'X-API-Key': API_KEY }, timeout: 2000 }
  );
  return data;   // { recommended_lots, risk, weight, mode, horizon, norm_source, method, ... }
}

const out = await sizeLeg({
  underlying: 'SENSEX', option_type: 'CE', direction: 'SELL',
  strike: 74100, expiry_date: '2026-06-12',
});
placeOrder({ lots: out.recommended_lots });
curl -X POST https://api-beta.tradlyt.com/api/v1/sizing/leg-size \
  -H "X-API-Key: tlyt_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "underlying": "SENSEX", "option_type": "CE", "direction": "SELL",
    "strike": 74100, "expiry_date": "2026-06-12",
    "base_lots": 10, "weight_norm": 0.497,
    "horizon": "intraday", "mode": "derisk"
  }'
# -> { "recommended_lots": 8, "risk": 0.63, "weight": 0.79, "norm_source": "strategy",
#      "mode": "derisk", "horizon": "intraday", "sizing_active": true, "method": "ml", ... }

Match the horizon to your strategy

horizon: intraday uses the same-day model; horizon: positional uses the multi-day (underlying-driven) model with a live daily-history lookup. A mismatch — an intraday strategy scored by the positional model or vice-versa — won't align with your P&L. Use the horizon you backtested.

Always pass weight_norm

Without it the model falls back to its training-distribution norm, which can systematically over- or under-size a specific strategy. norm_source in the response tells you which was used ("strategy" = good, "training" = you forgot to pass it).


Size a hedged structure

For an iron-fly / condor / spread, send all legs in one call to /sizing/structure. We apply one weight from the short legs to every leg, so your buy:sell ratio is preserved — the wings are never stripped. Don't size hedge legs one-by-one on /sizing/leg-size; that drifts the ratio.

def size_structure(underlying, legs, base_lots=10, mode="derisk"):
    r = requests.post(
        f"{BASE}/sizing/structure",
        headers=HDR,
        json={"underlying": underlying, "legs": legs, "base_lots": base_lots,
              "weight_norm": WEIGHT_NORM, "horizon": "intraday", "mode": mode},
        timeout=3,
    )
    r.raise_for_status()
    return r.json()

# NIFTY iron-fly — short straddle at 23350, wings bought 200 pts out
legs = [
    {"option_type": "PE", "direction": "BUY",  "strike": 23150, "expiry_date": "2026-06-16"},
    {"option_type": "PE", "direction": "SELL", "strike": 23350, "expiry_date": "2026-06-16"},
    {"option_type": "CE", "direction": "SELL", "strike": 23350, "expiry_date": "2026-06-16"},
    {"option_type": "CE", "direction": "BUY",  "strike": 23550, "expiry_date": "2026-06-16"},
]
out = size_structure("NIFTY", legs)
print(f"hedged={out['hedged']}  ratio {out['hedge_ratio']['designed_sell_buy']} "
      f"-> {out['hedge_ratio']['sized_sell_buy']}")
for leg, sized in zip(legs, out["legs"]):
    place_order(leg, lots=sized["recommended_lots"])    # every leg same weight → ratio held
curl -X POST https://api-beta.tradlyt.com/api/v1/sizing/structure \
  -H "X-API-Key: tlyt_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "underlying": "NIFTY", "base_lots": 10,
    "horizon": "intraday", "mode": "derisk", "weight_norm": 0.471,
    "legs": [
      {"option_type": "PE", "direction": "BUY",  "strike": 23150, "expiry_date": "2026-06-16"},
      {"option_type": "PE", "direction": "SELL", "strike": 23350, "expiry_date": "2026-06-16"},
      {"option_type": "CE", "direction": "SELL", "strike": 23350, "expiry_date": "2026-06-16"},
      {"option_type": "CE", "direction": "BUY",  "strike": 23550, "expiry_date": "2026-06-16"}
    ]
  }'
# -> { "legs": [ {"strike":23150,"recommended_lots":8}, ... ], "hedged": true,
#      "sizing_mode": "structure-level", "structure_weight": 0.77,
#      "hedge_ratio": {"designed_sell_buy":1.0,"sized_sell_buy":1.0}, "method": "ml", ... }

Naked vs hedged

A naked straddle / strangle is sized per leg with /sizing/leg-size (the CE/PE tilt is edge, and there's no ratio to protect). A hedged structure goes through /sizing/structure so the buy:sell ratio survives. If any leg can't be priced, the whole structure runs flat — the ratio is never touched.