{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://tessera.dev/schema.json",
  "title": "Tessera strategy (v2) — DRAFT",
  "description": "Draft JSON Schema for the Tessera strategy format. Illustrative and incomplete; the format is versioned and the published schema is the contract once the engine ships. See /llms.txt and /docs.",
  "type": "object",
  "required": ["resource", "version", "venue", "instrument", "legs"],
  "additionalProperties": false,
  "properties": {
    "resource": { "const": "strategy" },
    "version": { "type": "integer", "enum": [2] },
    "name": { "type": "string" },
    "venue": { "type": "string", "description": "e.g. NSE" },
    "instrument": { "type": "string", "description": "underlying, e.g. NIFTY" },
    "let": {
      "type": "object",
      "description": "named, reusable Ref expressions; usable anywhere a Ref is accepted",
      "additionalProperties": { "$ref": "#/$defs/ref" }
    },
    "entry": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "candidates": {
          "type": "object",
          "required": ["every"],
          "properties": {
            "every": { "enum": ["trading_day"] },
            "at": { "type": "string", "pattern": "^[0-2][0-9]:[0-5][0-9]$" }
          }
        },
        "trigger": { "$ref": "#/$defs/predicate" }
      }
    },
    "legs": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/leg" } },
    "exit": { "type": "array", "items": { "$ref": "#/$defs/exitRule" } },
    "execution": { "$ref": "#/$defs/execution" }
  },
  "$defs": {
    "instrumentRef": {
      "oneOf": [
        { "type": "object", "required": ["Equity"], "properties": { "Equity": { "type": "object", "required": ["symbol"], "properties": { "symbol": { "type": "string" } } } } },
        { "type": "object", "required": ["Future"], "properties": { "Future": { "type": "object", "required": ["underlying", "expiry"], "properties": { "underlying": { "type": "string" }, "expiry": { "$ref": "#/$defs/expiryRef" } } } } },
        { "type": "object", "required": ["Option"], "properties": { "Option": { "type": "object", "required": ["underlying", "expiry", "right"], "properties": { "underlying": { "type": "string" }, "expiry": { "$ref": "#/$defs/expiryRef" }, "right": { "enum": ["CE", "PE"] } } } } }
      ]
    },
    "expiryRef": { "type": "string", "pattern": "^(W|W2|W3|M|M2|M3|Y|\\d{4}-\\d{2}-\\d{2})$" },
    "leg": {
      "type": "object",
      "required": ["ref", "instrument", "side", "qty"],
      "additionalProperties": false,
      "properties": {
        "ref": { "type": "string", "pattern": "^[a-z][a-z0-9_]*$" },
        "instrument": { "$ref": "#/$defs/instrumentRef" },
        "side": { "enum": ["BUY", "SELL"] },
        "qty": { "type": "integer", "minimum": 1 },
        "select": { "$ref": "#/$defs/selector" },
        "exit": { "type": "array", "items": { "$ref": "#/$defs/exitRule" } }
      }
    },
    "selector": {
      "type": "object",
      "required": ["on", "pick"],
      "properties": {
        "over": { "type": "string", "description": "strike_ladder | universe(<name>)" },
        "on": { "enum": ["Strike", "Premium", "OI", "Delta", "Price"] },
        "pick": { "description": "Nearest | Max | Min | AtStep(n)", "oneOf": [ { "enum": ["Nearest", "Max", "Min"] }, { "type": "string", "pattern": "^AtStep\\(-?\\d+\\)$" } ] },
        "target": { "$ref": "#/$defs/ref" },
        "tolerance": { "$ref": "#/$defs/ref" }
      }
    },
    "ref": {
      "description": "closed algebra: Absolute | Obs | Strike | Premium | Pct | ScalePct | Steps | Offset | Sum. Illustrative — real refs carry units.",
      "oneOf": [ { "type": "string" }, { "type": "number" }, { "type": "object" } ]
    },
    "predicate": {
      "description": "closed boolean algebra: Compare | Within | And | Or | Not | Truthy | Expiry",
      "oneOf": [ { "type": "string", "const": "Expiry" }, { "type": "object" } ]
    },
    "exitRule": {
      "type": "object",
      "required": ["when", "then"],
      "properties": {
        "scope": { "enum": ["basket", "leg"] },
        "when": { "$ref": "#/$defs/predicate" },
        "then": { "enum": ["SquareOffAll", "SquareOffLeg", "MoveSLToCost", "Repeat", "Trail"] },
        "repeat": { "enum": ["same_contracts", "reselect"] },
        "max_repeats": { "type": "integer", "minimum": 1 }
      }
    },
    "execution": {
      "type": "object",
      "properties": {
        "product": { "enum": ["intraday", "carry_forward", "delivery"] },
        "order_type": { "enum": ["market", "limit"] },
        "limit_offset": { "$ref": "#/$defs/ref" },
        "slippage_tolerance": { "$ref": "#/$defs/ref" },
        "validity": { "enum": ["day", "ioc"] },
        "multi_leg_policy": { "enum": ["legs_atomic", "hedge_first", "unwind_on_partial", "accept_partial"] },
        "max_retries": { "type": "integer", "minimum": 0 }
      },
      "required": ["product"]
    }
  }
}
