{
  "openapi": "3.1.1",
  "info": {
    "title": "RavingDesk Paper Portfolio & Exposure API",
    "version": "1.0.0",
    "summary": "Turn paper fills and your own marks into positions, P&L, exposure and a price-shock check.",
    "description": "Every endpoint performs the same calculation as the human page and returns inputs, result, units, assumptions, sources, warnings and computedAt. MCP endpoint: /mcp."
  },
  "servers": [
    {
      "url": "https://ravingdeskpaperportfolio.usefulweb.site"
    }
  ],
  "paths": {
    "/api/v1/calculate": {
      "post": {
        "operationId": "calculate",
        "summary": "Turn paper fills and your own marks into positions, P&L, exposure and a price-shock check.",
        "description": "Replays a list of paper fills (buys and sells, long and short) with weighted-average cost, then values the open positions at the marks you supply: cash, equity, realized and unrealized profit or loss, long/short/gross/net exposure, concentration and the equity after a uniform price shock. Totals are withheld when a position has no mark. It is the ledger engine of the RavingDesk paper-trading workspace.\n\nMethodology:\n- Fills are replayed in the order given. Adding to a position updates the weighted-average entry; reducing it realizes (exit − average entry) × quantity for longs and the reverse for shorts, minus the proportional entry fee and the exit fee.\n- A fill larger than the open position closes it and opens the remainder in the other direction at the fill price.\n- Cash = starting cash − buys + sells − fees (short sales add cash; negative cash is allowed, no margin checks). Equity = cash + quantity × mark of every open position.\n- Exposure: long = sum of long market values, short = sum of absolute short market values, gross = long + short, net = long − short; concentration is the largest position’s share of gross. Ratios to equity are only shown when equity is positive.\n- If any open position lacks a mark, equity, unrealized P&L, exposure and the shock are withheld instead of being calculated from partial data.\n- The price shock multiplies every mark by (1 + shock %), floored at zero, and recalculates equity.\n\nPaper-trading calculation for education and planning only. It is not investment, tax or legal advice, uses only the numbers you enter and never places an order.",
        "tags": [
          "apps"
        ],
        "x-mcp-tool": "ravingdesk_paper_portfolio",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RavingdeskPaperPortfolioInput"
              },
              "examples": {
                "example1": {
                  "summary": "Fictional long and short with a partial close, −10 % shock",
                  "value": {
                    "startingCash": 100000,
                    "trades": [
                      {
                        "symbol": "EXAMPLE-A",
                        "side": "buy",
                        "quantity": 100,
                        "price": 50,
                        "fee": 2
                      },
                      {
                        "symbol": "EXAMPLE-A",
                        "side": "sell",
                        "quantity": 40,
                        "price": 56,
                        "fee": 2
                      },
                      {
                        "symbol": "EXAMPLE-B",
                        "side": "sell",
                        "quantity": 30,
                        "price": 120,
                        "fee": 3
                      }
                    ],
                    "marks": [
                      {
                        "symbol": "EXAMPLE-A",
                        "price": 54
                      },
                      {
                        "symbol": "EXAMPLE-B",
                        "price": 112
                      }
                    ],
                    "shockPercent": -10
                  }
                },
                "example2": {
                  "summary": "Reversal from long to short in one fill",
                  "value": {
                    "startingCash": 10000,
                    "trades": [
                      {
                        "symbol": "DEMO",
                        "side": "buy",
                        "quantity": 10,
                        "price": 100,
                        "fee": 1
                      },
                      {
                        "symbol": "DEMO",
                        "side": "sell",
                        "quantity": 25,
                        "price": 110,
                        "fee": 2
                      }
                    ],
                    "marks": [
                      {
                        "symbol": "DEMO",
                        "price": 105
                      }
                    ],
                    "shockPercent": 5
                  }
                },
                "example3": {
                  "summary": "Missing mark: totals withheld",
                  "value": {
                    "startingCash": 50000,
                    "trades": [
                      {
                        "symbol": "EXAMPLE-C",
                        "side": "buy",
                        "quantity": 20,
                        "price": 75,
                        "fee": 1
                      },
                      {
                        "symbol": "EXAMPLE-D",
                        "side": "buy",
                        "quantity": 5,
                        "price": 300,
                        "fee": 1
                      }
                    ],
                    "marks": [
                      {
                        "symbol": "EXAMPLE-C",
                        "price": 80
                      }
                    ],
                    "shockPercent": 0
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Calculation result envelope.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RavingdeskPaperPortfolioResult"
                }
              }
            }
          },
          "400": {
            "description": "Malformed JSON body.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Invalid input (see errors[]).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "Rate limit exceeded (see Retry-After).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/tool": {
      "get": {
        "operationId": "getToolDefinition",
        "summary": "Inputs, output schema and examples for this tool",
        "responses": {
          "200": {
            "description": "Tool definition.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Problem": {
        "type": "object",
        "description": "RFC 9457 problem details. 422 responses include per-field `errors`.",
        "required": [
          "type",
          "title",
          "status",
          "detail"
        ],
        "properties": {
          "type": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "status": {
            "type": "integer"
          },
          "detail": {
            "type": "string"
          },
          "code": {
            "type": "string"
          },
          "errors": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "field",
                "message"
              ],
              "properties": {
                "field": {
                  "type": "string"
                },
                "message": {
                  "type": "string"
                }
              }
            }
          }
        }
      },
      "RavingdeskPaperPortfolioInput": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "startingCash": {
            "title": "Starting cash",
            "type": "number",
            "minimum": 0,
            "maximum": 1000000000,
            "default": 100000
          },
          "trades": {
            "title": "Paper fills (in the order they happened)",
            "type": "array",
            "minItems": 1,
            "maxItems": 500,
            "items": {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "symbol": {
                  "title": "Symbol",
                  "type": "string",
                  "maxLength": 30
                },
                "side": {
                  "title": "Side",
                  "type": "string",
                  "enum": [
                    "buy",
                    "sell"
                  ]
                },
                "quantity": {
                  "title": "Quantity",
                  "type": "number",
                  "minimum": 1e-8,
                  "maximum": 1000000000
                },
                "price": {
                  "title": "Fill price",
                  "type": "number",
                  "minimum": 1e-8,
                  "maximum": 1000000000
                },
                "fee": {
                  "title": "Fee",
                  "type": "number",
                  "minimum": 0,
                  "maximum": 1000000000
                }
              },
              "required": [
                "symbol",
                "side",
                "quantity",
                "price"
              ]
            }
          },
          "marks": {
            "title": "Marks (your own current prices)",
            "description": "One price per open symbol. Without a mark, equity and exposure totals are not calculated.",
            "type": "array",
            "maxItems": 500,
            "items": {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "symbol": {
                  "title": "Symbol",
                  "type": "string",
                  "maxLength": 30
                },
                "price": {
                  "title": "Mark price",
                  "type": "number",
                  "minimum": 0,
                  "maximum": 1000000000
                }
              },
              "required": [
                "symbol",
                "price"
              ]
            }
          },
          "shockPercent": {
            "title": "Uniform price shock",
            "description": "Applied to every mark at once (sensitivity check, not a forecast).",
            "type": "number",
            "minimum": -100,
            "maximum": 100,
            "default": -10,
            "x-unit": "%"
          }
        },
        "required": [
          "startingCash"
        ]
      },
      "RavingdeskPaperPortfolioResult": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "schemaVersion",
          "tool",
          "computedAt",
          "inputs",
          "summary",
          "result",
          "units",
          "assumptions",
          "sources",
          "warnings",
          "presentation",
          "links"
        ],
        "properties": {
          "schemaVersion": {
            "type": "string",
            "const": "1.0"
          },
          "tool": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "id",
              "name",
              "version"
            ],
            "properties": {
              "id": {
                "type": "string"
              },
              "name": {
                "type": "string"
              },
              "version": {
                "type": "string"
              }
            }
          },
          "computedAt": {
            "type": "string",
            "format": "date-time"
          },
          "inputs": {
            "type": "object",
            "description": "Normalised inputs actually used, including applied defaults."
          },
          "summary": {
            "type": "string"
          },
          "result": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "cash",
              "equity",
              "totalPnl",
              "realizedPnl",
              "unrealizedPnl",
              "missingMarks",
              "positions",
              "exposure",
              "shock"
            ],
            "properties": {
              "cash": {
                "type": "number"
              },
              "equity": {
                "type": [
                  "number",
                  "null"
                ]
              },
              "totalPnl": {
                "type": [
                  "number",
                  "null"
                ]
              },
              "realizedPnl": {
                "type": "number"
              },
              "unrealizedPnl": {
                "type": [
                  "number",
                  "null"
                ]
              },
              "missingMarks": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "positions": {
                "type": "array",
                "items": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "symbol",
                    "side",
                    "quantity",
                    "averageEntry",
                    "mark",
                    "marketValue",
                    "unrealizedPnl"
                  ],
                  "properties": {
                    "symbol": {
                      "type": "string"
                    },
                    "side": {
                      "type": "string",
                      "enum": [
                        "long",
                        "short"
                      ]
                    },
                    "quantity": {
                      "type": "number"
                    },
                    "averageEntry": {
                      "type": "number"
                    },
                    "mark": {
                      "type": [
                        "number",
                        "null"
                      ]
                    },
                    "marketValue": {
                      "type": [
                        "number",
                        "null"
                      ]
                    },
                    "unrealizedPnl": {
                      "type": [
                        "number",
                        "null"
                      ]
                    }
                  }
                }
              },
              "exposure": {
                "type": [
                  "object",
                  "null"
                ],
                "additionalProperties": false,
                "required": [
                  "longNotional",
                  "shortNotional",
                  "grossNotional",
                  "netNotional",
                  "grossEquityPercent",
                  "netEquityPercent",
                  "largestSharePercent"
                ],
                "properties": {
                  "longNotional": {
                    "type": "number"
                  },
                  "shortNotional": {
                    "type": "number"
                  },
                  "grossNotional": {
                    "type": "number"
                  },
                  "netNotional": {
                    "type": "number"
                  },
                  "grossEquityPercent": {
                    "type": [
                      "number",
                      "null"
                    ]
                  },
                  "netEquityPercent": {
                    "type": [
                      "number",
                      "null"
                    ]
                  },
                  "largestSharePercent": {
                    "type": [
                      "number",
                      "null"
                    ]
                  }
                }
              },
              "shock": {
                "type": [
                  "object",
                  "null"
                ],
                "additionalProperties": false,
                "required": [
                  "percent",
                  "equity",
                  "change"
                ],
                "properties": {
                  "percent": {
                    "type": "number"
                  },
                  "equity": {
                    "type": "number"
                  },
                  "change": {
                    "type": "number"
                  }
                }
              }
            }
          },
          "units": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "assumptions": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "sources": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "title"
              ],
              "properties": {
                "title": {
                  "type": "string"
                },
                "url": {
                  "type": "string"
                },
                "publisher": {
                  "type": "string"
                },
                "retrievedAt": {
                  "type": "string"
                },
                "note": {
                  "type": "string"
                }
              }
            }
          },
          "warnings": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "presentation": {
            "type": "array",
            "items": {
              "type": "object"
            },
            "description": "Display blocks used by the human page (same data)."
          },
          "links": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "human",
              "api",
              "openapi",
              "methodology"
            ],
            "properties": {
              "human": {
                "type": "string",
                "format": "uri"
              },
              "api": {
                "type": "string",
                "format": "uri"
              },
              "openapi": {
                "type": "string",
                "format": "uri"
              },
              "methodology": {
                "type": "string",
                "format": "uri"
              }
            }
          }
        }
      }
    }
  }
}