{
  "openapi": "3.1.0",
  "info": { "title": "ReplayEdge Algorithm API", "version": "1.0.0", "description": "Trade anonymous historical market replays with scripts. API endpoints require a Bearer API Key; documentation is public. Each key is limited to 500 authenticated requests and 2 replay creations per minute. Market orders execute at the next tick open; limit and stop orders use each newly revealed tick's OHLC to determine whether and where they trigger." },
  "servers": [{ "url": "http://localhost:5178/api/v1", "description": "Local development" }],
  "security": [{ "bearerAuth": [] }],
  "paths": {
    "/me": { "get": { "summary": "Get authenticated API user and stats", "responses": { "200": { "description": "User profile" }, "401": { "$ref": "#/components/responses/Error" } } } },
    "/replays": { "post": { "summary": "Create an anonymous 180-tick replay", "description": "Requires a Bearer API Key generated from a logged-in account. Each key can create at most 2 successful replays per minute.", "requestBody": { "required": false, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ReplayOptions" } } } }, "responses": { "201": { "description": "Replay created" }, "401": { "$ref": "#/components/responses/Error" }, "429": { "$ref": "#/components/responses/RateLimited" }, "502": { "$ref": "#/components/responses/Error" } } } },
    "/replays/{replayId}": { "get": { "summary": "Get replay, account, order, and trade state", "parameters": [{ "$ref": "#/components/parameters/ReplayId" }], "responses": { "200": { "description": "Replay state" }, "401": { "$ref": "#/components/responses/Error" }, "404": { "$ref": "#/components/responses/Error" } } } },
    "/replays/{replayId}/market": { "get": { "summary": "Get revealed bars only", "parameters": [{ "$ref": "#/components/parameters/ReplayId" }, { "name": "from_tick", "in": "query", "schema": { "type": "integer", "minimum": 1 }, "description": "One-based first tick to return" }], "responses": { "200": { "description": "Revealed bars" } } } },
    "/replays/{replayId}/advance": { "post": { "summary": "Reveal 1-30 additional ticks", "parameters": [{ "$ref": "#/components/parameters/ReplayId" }], "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "properties": { "steps": { "type": "integer", "minimum": 1, "maximum": 30, "default": 1 } } } } } }, "responses": { "200": { "description": "Updated replay with newly revealed bars" } } } },
    "/replays/{replayId}/orders": { "post": { "summary": "Submit a market, limit, or stop order", "parameters": [{ "$ref": "#/components/parameters/ReplayId" }], "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/OrderRequest" } } } }, "responses": { "201": { "description": "Order accepted, filled, or rejected" } } } },
    "/replays/{replayId}/orders/{orderId}": { "delete": { "summary": "Cancel a pending or partially filled order", "parameters": [{ "$ref": "#/components/parameters/ReplayId" }, { "name": "orderId", "in": "path", "required": true, "schema": { "type": "string" } }], "responses": { "200": { "description": "Order cancelled" } } } },
    "/replays/{replayId}/settle": { "post": { "summary": "Settle replay and reveal the symbol and date", "parameters": [{ "$ref": "#/components/parameters/ReplayId" }], "responses": { "200": { "description": "Final report" } } } },
    "/leaderboard": { "get": { "security": [], "summary": "Get public leaderboard", "parameters": [{ "name": "mode", "in": "query", "schema": { "type": "string", "enum": ["score", "pnl"], "default": "score" } }], "responses": { "200": { "description": "Leaderboard" } } } }
  },
  "components": {
    "securitySchemes": { "bearerAuth": { "type": "http", "scheme": "bearer", "bearerFormat": "ReplayEdge API Key" } },
    "parameters": { "ReplayId": { "name": "replayId", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } } },
    "responses": {
      "Error": { "description": "API error" },
      "RateLimited": {
        "description": "Per-key rate limit exceeded. Retry-After gives the wait in seconds.",
        "headers": { "Retry-After": { "schema": { "type": "integer" } }, "X-RateLimit-Limit": { "schema": { "type": "integer" } }, "X-RateLimit-Remaining": { "schema": { "type": "integer" } }, "X-RateLimit-Reset": { "schema": { "type": "integer" } } }
      }
    },
    "schemas": {
      "ReplayOptions": { "type": "object", "properties": { "universe": { "type": "string", "enum": ["all", "mega", "tech", "etf"], "default": "all" }, "volatility": { "type": "string", "enum": ["any", "low", "medium", "high"], "default": "any" }, "trend": { "type": "string", "enum": ["any", "up", "down", "range"], "default": "any" }, "timeOfDay": { "type": "string", "enum": ["any", "open", "midday", "close"], "default": "any" }, "totalTicks": { "type": "integer", "minimum": 90, "maximum": 390, "default": 180 }, "startAtOpen": { "type": "boolean", "default": false } } },
      "OrderRequest": { "type": "object", "required": ["side", "type", "quantity"], "properties": { "side": { "type": "string", "enum": ["buy", "sell"] }, "type": { "type": "string", "enum": ["market", "limit", "stop"] }, "quantity": { "type": "integer", "minimum": 1 }, "triggerPrice": { "type": "number", "exclusiveMinimum": 0 }, "stopLoss": { "type": "number", "exclusiveMinimum": 0 }, "takeProfit": { "type": "number", "exclusiveMinimum": 0 } } }
    }
  }
}
