> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aioka.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Run Backtest

> Historical performance simulation (max 90 days)

<Note>
  To use the playground, enter your API key in the **X-API-Key** field above.
  Requires PRO tier key (\$199/mo). Contact [info@aioka.io](mailto:info@aioka.io) to upgrade.
</Note>

**Tier:** Pro (\$199/mo) 🔒
**Cache TTL:** 3600 seconds (1 hour)
**Max period:** 90 days

> ⚠️ Results are based on historical simulated conditions.
> Past performance does not guarantee future results.

## Request Body

| Field        | Type   | Required | Description                           |
| ------------ | ------ | -------- | ------------------------------------- |
| `start_date` | string | ✅        | Start date (YYYY-MM-DD)               |
| `end_date`   | string | ✅        | End date (YYYY-MM-DD)                 |
| `mode`       | string | —        | Entry mode: `A` or `B` (default: `B`) |

<RequestExample>
  ```bash curl theme={null}
  curl -X POST https://api.aioka.io/v1/backtest \
    -H "X-API-Key: aik_pro_your_key_here" \
    -H "Content-Type: application/json" \
    -d '{"start_date": "2026-01-01", "end_date": "2026-04-01", "mode": "B"}'
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "total_trades": 8,
    "win_rate": 0.625,
    "total_pnl_usd": 312.50,
    "max_drawdown_pct": 4.2,
    "avg_hold_hours": 18.4,
    "period_days": 90,
    "mode": "B",
    "cached": false,
    "cache_ttl_seconds": 3600
  }
  ```

  ```json 400 theme={null}
  {
    "detail": {
      "error": "period_too_long",
      "message": "Maximum backtest period is 90 days.",
      "requested_days": 365
    }
  }
  ```
</ResponseExample>


## OpenAPI

````yaml POST /v1/backtest
openapi: 3.1.0
info:
  title: AIOKA Intelligence API
  description: |

    ## AI-powered crypto market intelligence

    AIOKA Intelligence API provides real-time access to our AI Council verdicts,
    market signals, regime detection, and Ghost Trader entry signals.

    ### Tiers
    - **Free**: 100 calls/day — Verdict + Regime
    - **Basic** ($49/mo): 1,000 calls/day — + Signals
    - **Pro** ($199/mo): 10,000 calls/day — + Council + Ghost

    ### Authentication
    Pass your API key in the `X-API-Key` header:

    ```
    X-API-Key: aik_free_xxxxxxxxxxxx
    ```

    ### Get your API key
    `POST /v1/keys/generate` (free tier, no credit card)
  contact:
    name: AIOKA Support
    url: https://docs.aioka.io/
    email: api@aioka.io
  license:
    name: Commercial
    url: https://aioka.io/terms
  version: 1.0.0
servers:
  - url: https://api.aioka.io
    description: Production — AIOKA Intelligence API
security: []
paths:
  /v1/backtest:
    post:
      tags:
        - Backtest
      summary: Run Historical Backtest
      description: >-
        Runs a read-only historical performance simulation using AIOKA Ghost
        Trader

        logic against past market data stored in Neon DB.


        **Tier:** Pro ($199/mo) 🔒

        **Cache TTL:** 3600 seconds (1 hour)

        **Rate limit:** 10,000 calls/day, 100/minute

        **Max period:** 90 days


        ⚠️ Results are based on simulated historical conditions.

        Past performance does not guarantee future results.
      operationId: run_backtest_v1_backtest_post
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/BacktestRequest'
        required: true
      responses:
        '200':
          description: Backtest results
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BacktestResponse'
        '400':
          description: Invalid date range or period > 90 days
        '401':
          description: Invalid or missing API key
        '403':
          description: Pro tier required
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
        '429':
          description: Rate limit exceeded
      security:
        - APIKeyHeader: []
components:
  schemas:
    BacktestRequest:
      properties:
        start_date:
          type: string
          title: Start Date
          description: Start date (YYYY-MM-DD)
        end_date:
          type: string
          title: End Date
          description: End date (YYYY-MM-DD)
        mode:
          type: string
          title: Mode
          description: 'Entry mode: A or B'
          default: B
      type: object
      required:
        - start_date
        - end_date
      title: BacktestRequest
      example:
        end_date: '2026-04-01'
        mode: B
        start_date: '2026-01-01'
    BacktestResponse:
      properties:
        total_trades:
          type: integer
          title: Total Trades
        win_rate:
          type: number
          title: Win Rate
        total_pnl_usd:
          type: number
          title: Total Pnl Usd
        max_drawdown_pct:
          type: number
          title: Max Drawdown Pct
        avg_hold_hours:
          type: number
          title: Avg Hold Hours
        period_days:
          type: integer
          title: Period Days
        mode:
          type: string
          title: Mode
        cached:
          type: boolean
          title: Cached
        cache_ttl_seconds:
          type: integer
          title: Cache Ttl Seconds
      type: object
      required:
        - total_trades
        - win_rate
        - total_pnl_usd
        - max_drawdown_pct
        - avg_hold_hours
        - period_days
        - mode
        - cached
        - cache_ttl_seconds
      title: BacktestResponse
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
  securitySchemes:
    APIKeyHeader:
      type: apiKey
      description: 'Your AIOKA Intelligence API key (format: aik_{tier}_{random})'
      in: header
      name: X-API-Key

````