{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/fallblu/trading-engine/contracts/strategy/v14/message.schema.json",
  "title": "Trading Engine external strategy protocol v14 message",
  "description": "One strict request or response in the synchronous JSON Lines strategy protocol. The engine additionally enforces direction, sequence pairing, canonical values, response limits, and lifecycle order.",
  "oneOf": [
    { "$ref": "#/$defs/initialize" },
    { "$ref": "#/$defs/ready" },
    { "$ref": "#/$defs/event" },
    { "$ref": "#/$defs/intents" },
    { "$ref": "#/$defs/shutdown" },
    { "$ref": "#/$defs/stopped" },
    { "$ref": "#/$defs/error" }
  ],
  "$defs": {
    "sequence": {
      "type": "string",
      "pattern": "^[1-9][0-9]*$"
    },
    "base": {
      "type": "object",
      "additionalProperties": false,
      "required": ["strategy_protocol_version", "strategy_sequence", "message_type", "payload"],
      "properties": {
        "strategy_protocol_version": { "const": "14" },
        "strategy_sequence": { "$ref": "#/$defs/sequence" },
        "message_type": { "type": "string" },
        "payload": { "type": "object" }
      }
    },
    "initialize": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "properties": {
            "message_type": { "const": "initialize" },
            "payload": { "$ref": "#/$defs/initializePayload" }
          }
        }
      ]
    },
    "initializePayload": {
      "type": "object",
      "additionalProperties": false,
      "required": ["engine_version", "scenario_contract_version", "scenario_sha256", "run_id", "base_currency", "initial_cash", "initial_portfolio", "instruments", "venue_calendars", "risk", "execution", "financing", "settlement", "metadata"],
      "properties": {
        "engine_version": { "type": "string", "minLength": 1 },
        "scenario_contract_version": { "const": "16" },
        "scenario_sha256": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
        "run_id": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/identifier" },
        "base_currency": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/identifier" },
        "initial_cash": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/cashBalance" }
        },
        "initial_portfolio": {
          "oneOf": [
            { "type": "null" },
            { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/initialPortfolio" }
          ]
        },
        "instruments": {
          "type": "array",
          "minItems": 1,
          "maxItems": 4096,
          "items": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/instrument" }
        },
        "venue_calendars": {
          "type": "array",
          "items": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/venueCalendar" }
        },
        "risk": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/risk" },
        "execution": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/execution" },
        "financing": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/financing" },
        "settlement": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/settlement" },
        "metadata": { "type": "object" }
      }
    },
    "ready": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "properties": {
            "message_type": { "const": "ready" },
            "payload": { "$ref": "#/$defs/readyPayload" }
          }
        }
      ]
    },
    "readyPayload": {
      "type": "object",
      "additionalProperties": false,
      "required": ["strategy_name", "strategy_version"],
      "properties": {
        "strategy_name": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/identifier" },
        "strategy_version": {
          "oneOf": [
            { "type": "null" },
            { "type": "string", "minLength": 1, "pattern": "^\\S(?:.*\\S)?$" }
          ]
        }
      }
    },
    "event": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "properties": {
            "message_type": { "const": "event" },
            "payload": { "$ref": "#/$defs/eventPayload" }
          }
        }
      ]
    },
    "eventPayload": {
      "type": "object",
      "additionalProperties": false,
      "required": ["context", "event"],
      "properties": {
        "context": { "$ref": "#/$defs/context" },
        "event": { "$ref": "#/$defs/strategyEvent" }
      }
    },
    "context": {
      "type": "object",
      "additionalProperties": false,
      "required": ["now", "portfolio", "working_orders", "latest_bars"],
      "properties": {
        "now": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/timestamp" },
        "portfolio": { "$ref": "#/$defs/portfolio" },
        "working_orders": {
          "type": "array",
          "items": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/journal.schema.json#/$defs/order" }
        },
        "latest_bars": {
          "type": "array",
          "items": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/bar" }
        }
      }
    },
    "portfolio": {
      "type": "object",
      "additionalProperties": false,
      "required": ["base_currency", "cash", "net_market_value", "long_market_value", "short_market_value", "gross_exposure", "equity", "weights_available", "cash_weight", "cash_balances", "positions", "group_exposures"],
      "properties": {
        "base_currency": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/identifier" },
        "cash": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/signedDecimal" },
        "net_market_value": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/signedDecimal" },
        "long_market_value": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/unsignedDecimal" },
        "short_market_value": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/unsignedDecimal" },
        "gross_exposure": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/unsignedDecimal" },
        "equity": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/signedDecimal" },
        "weights_available": { "type": "boolean" },
        "cash_weight": { "$ref": "#/$defs/optionalWeight" },
        "cash_balances": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/journal.schema.json#/$defs/cashAttribution" }
        },
        "positions": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/markedPosition" }
        },
        "group_exposures": {
          "type": "array",
          "items": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/journal.schema.json#/$defs/groupExposure" }
        }
      }
    },
    "optionalWeight": {
      "oneOf": [
        { "type": "null" },
        { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/signedDecimal" }
      ]
    },
    "markedPosition": {
      "type": "object",
      "additionalProperties": false,
      "required": ["instrument_id", "quantity", "settled_quantity", "unsettled_quantity", "mark", "base_market_value", "weight"],
      "properties": {
        "instrument_id": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/identifier" },
        "quantity": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/signedDecimal" },
        "settled_quantity": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/signedDecimal" },
        "unsettled_quantity": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/signedDecimal" },
        "mark": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/positiveDecimal" },
        "base_market_value": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/signedDecimal" },
        "weight": { "$ref": "#/$defs/optionalWeight" }
      }
    },
    "strategyEvent": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "market_slice"],
          "properties": {
            "type": { "const": "market_slice_closed" },
            "market_slice": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/marketSlice" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "fill"],
          "properties": {
            "type": { "const": "fill_received" },
            "fill": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/journal.schema.json#/$defs/fill" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "order"],
          "properties": {
            "type": { "const": "order_updated" },
            "order": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/journal.schema.json#/$defs/order" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "reason"],
          "properties": {
            "type": { "const": "intent_rejected" },
            "reason": { "type": "string", "minLength": 1 }
          }
        }
      ]
    },
    "intents": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "properties": {
            "message_type": { "const": "intents" },
            "payload": { "$ref": "#/$defs/intentsPayload" }
          }
        }
      ]
    },
    "intentsPayload": {
      "type": "object",
      "additionalProperties": false,
      "required": ["intents"],
      "properties": {
        "intents": {
          "type": "array",
          "maxItems": 4096,
          "items": { "$ref": "https://github.com/fallblu/trading-engine/contracts/v16/scenario.schema.json#/$defs/intent" }
        }
      }
    },
    "shutdown": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "properties": {
            "message_type": { "const": "shutdown" },
            "payload": { "$ref": "#/$defs/emptyPayload" }
          }
        }
      ]
    },
    "stopped": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "properties": {
            "message_type": { "const": "stopped" },
            "payload": { "$ref": "#/$defs/emptyPayload" }
          }
        }
      ]
    },
    "emptyPayload": {
      "type": "object",
      "additionalProperties": false,
      "maxProperties": 0
    },
    "error": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "properties": {
            "message_type": { "const": "error" },
            "payload": { "$ref": "#/$defs/errorPayload" }
          }
        }
      ]
    },
    "errorPayload": {
      "type": "object",
      "additionalProperties": false,
      "required": ["message"],
      "properties": {
        "message": { "type": "string", "minLength": 1, "pattern": "^\\S(?:.*\\S)?$" }
      }
    }
  }
}
