{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/simianx-ai/json-schema/simianx-ai-position-schema.json",
  "title": "Position",
  "description": "A paper position or a resting entry order (status `placed`). Prices in USD, quantity in coins or shares. Simulated inside SimianX; nothing is sent to an exchange or broker.",
  "x-generated": "2026-11-24",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/simianx-ai-openapi.json#/components/schemas/Position",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "side": {
      "type": "string",
      "enum": [
        "long",
        "short"
      ]
    },
    "status": {
      "type": "string",
      "enum": [
        "placed",
        "open",
        "closed",
        "force_closed",
        "cancelled"
      ],
      "description": "placed = entry order waiting; a cancelled or expired entry order is cancelled (crypto) or force_closed (stock), close_reason says which"
    },
    "quantity": {
      "type": [
        "number",
        "null"
      ],
      "description": "Coins or shares held"
    },
    "entry_price": {
      "type": [
        "number",
        "null"
      ],
      "description": "Average entry price"
    },
    "current_price": {
      "type": [
        "number",
        "null"
      ],
      "description": "Last price the monitor marked the position at"
    },
    "close_price": {
      "type": [
        "number",
        "null"
      ],
      "description": "Exit price once closed"
    },
    "stop_loss": {
      "type": [
        "number",
        "null"
      ]
    },
    "take_profit": {
      "type": [
        "number",
        "null"
      ]
    },
    "pnl": {
      "type": [
        "number",
        "null"
      ],
      "description": "USD P&L: unrealised while open, realised once closed"
    },
    "pnl_percentage": {
      "type": [
        "number",
        "null"
      ],
      "description": "P&L in percent of the entry value"
    },
    "position_value_usd": {
      "type": [
        "number",
        "null"
      ],
      "description": "Quantity x current price"
    },
    "close_reason": {
      "type": [
        "string",
        "null"
      ],
      "enum": [
        "ai_decision",
        "manual",
        "stop_loss",
        "take_profit",
        "user_disabled",
        "timeframe_changed",
        "connection_closed",
        "system_error",
        "order_expired",
        "order_cancelled",
        "market_closed",
        "orphan_stop_loss",
        "orphan_take_profit",
        null
      ],
      "description": "Set once closed or cancelled"
    },
    "timeframe": {
      "type": [
        "string",
        "null"
      ]
    },
    "order_type": {
      "type": [
        "string",
        "null"
      ],
      "enum": [
        "market",
        "limit",
        "stop",
        null
      ],
      "description": "How the position was entered"
    },
    "limit_order_price": {
      "type": [
        "number",
        "null"
      ],
      "description": "Resting price of a limit / stop entry order"
    },
    "fees": {
      "type": [
        "number",
        "null"
      ],
      "description": "Simulated fees in USD so far"
    },
    "opened_at": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time"
    },
    "closed_at": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time"
    },
    "filled_at": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time",
      "description": "When a resting order was filled"
    },
    "operations": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/Operation"
      },
      "description": "Only on GET .../positions"
    },
    "holds": {
      "type": "integer",
      "description": "Number of `hold` checks (GET .../positions without include=holds)"
    },
    "last_hold": {
      "oneOf": [
        {
          "$ref": "#/$defs/Operation"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "$defs": {
    "Operation": {
      "type": "object",
      "description": "One change to a paper position, oldest first. The AI's `hold` checks are left out unless `include=holds`.",
      "properties": {
        "id": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "enum": [
            "open",
            "add",
            "reduce",
            "close",
            "close_and_reverse",
            "update_sl",
            "update_tp",
            "update_both",
            "hold",
            "place_order",
            "fill_order",
            "cancel_order",
            "expire_order",
            "replace_order",
            "wait_order"
          ]
        },
        "execution_price": {
          "type": [
            "number",
            "null"
          ],
          "description": "Price the change was made at (quote currency, USD)"
        },
        "quantity_change": {
          "type": [
            "number",
            "null"
          ],
          "description": "Coins or shares added (+) or removed (-)"
        },
        "before_quantity": {
          "type": [
            "number",
            "null"
          ]
        },
        "after_quantity": {
          "type": [
            "number",
            "null"
          ]
        },
        "stop_loss": {
          "type": [
            "number",
            "null"
          ],
          "description": "Stop loss after this operation"
        },
        "take_profit": {
          "type": [
            "number",
            "null"
          ],
          "description": "Take profit after this operation"
        },
        "realized_pnl": {
          "type": [
            "number",
            "null"
          ],
          "description": "USD realised by this operation (reduce / close)"
        },
        "fee": {
          "type": [
            "number",
            "null"
          ],
          "description": "Simulated fee in USD"
        },
        "confidence": {
          "type": [
            "number",
            "null"
          ],
          "description": "Decision agent confidence in percent, 0-100 (a manual crypto action records 100, an engine fill or cancel of a crypto entry order 0)"
        },
        "pnl_at_operation": {
          "type": [
            "number",
            "null"
          ],
          "description": "Unrealised USD P&L of the position at that moment"
        },
        "pnl_percentage_at_operation": {
          "type": [
            "number",
            "null"
          ]
        },
        "rationale": {
          "type": "string",
          "description": "Why (decision agent text or the manual reason), at most 600 characters"
        },
        "created_at": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        }
      }
    }
  }
}