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

# Optimization

> Sweep a strategy's parameters across ranges, let AskFutures test the grid, and find the best combination by the metric you care about — without overfitting.

**Optimization** sweeps your strategy's tunable numbers across ranges, runs a
backtest for every combination, and reports the one that scored best on the metric
you chose. You pick the parameters and the ranges; AskFutures runs the grid and
ranks the results.

<Note>
  Every run in a sweep is a normal, deterministic backtest — same real prices,
  same modeled costs. Optimization just runs many of them and compares the
  numbers. The AI doesn't decide what's "best"; the simulator does the math and
  the metric does the ranking.
</Note>

## How a sweep works

<Steps>
  <Step title="Pick the parameters to sweep">
    Any tunable number on the strategy card — an EMA period, a stop size, a target,
    an ATR multiple. Hold the rest fixed.
  </Step>

  <Step title="Set a range for each one">
    Give a **min**, a **max**, and either a **step** or a number of test points.
    For example, sweep the stop from $200 to $600 in \$100 steps (5 values), or the
    fast EMA from 5 to 15.
  </Step>

  <Step title="Choose the metric to optimize">
    Best average P\&L per trade, best total P\&L, best win rate — whatever defines
    "better" for your idea.
  </Step>

  <Step title="Run it">
    AskFutures backtests every combination in the grid and reports the winning
    parameter set, with the full table available to download.
  </Step>
</Steps>

The strategy card shows the size of the job before you commit — something like
*"1 parameter · 10 runs"* — and you can switch each range between an evenly spaced
count (**Num. steps**) or your own explicit values (**Custom**), on a **linear** or
**log** scale. Two ranges multiply: a 5-value stop sweep crossed with a 4-value
target sweep is 20 backtests.

## Two-tier sweeps: why some changes are fast and some are slow

Under the hood, a sweep is organized in two tiers — an **outer** loop and an
**inner** loop — and which tier a parameter lands in is why some sweeps fly and
others take a while.

```mermaid theme={null}
flowchart LR
  O["Outer loop<br/>changes the indicators<br/>(recompute everything)"] --> I["Inner loop<br/>changes only the trade rules<br/>(reuse the indicators)"]
  I --> R["Score &amp; rank<br/>by your metric"]
```

<CardGroup cols={2}>
  <Card title="Inner = fast" icon="bolt">
    Parameters that only affect **how trades are managed** — stop size, target,
    trailing stop, max time in trade. The indicators don't change, so the engine
    reuses them and just re-simulates the exits. Many values, cheaply.
  </Card>

  <Card title="Outer = slower" icon="hourglass-half">
    Parameters that change an **indicator itself** — an EMA period, an RSI length,
    an ATR window. Every value forces the indicators to be recomputed across the
    whole window before the trades can run. Fewer values, more work each.
  </Card>
</CardGroup>

<Tip>
  Want a big sweep without a long wait? Put your wide ranges on the **inner**
  parameters (stops and targets) and keep the **outer** parameters (indicator
  lengths) to a handful of values.
</Tip>

## When to optimize

Optimize **after** a strategy already backtests cleanly and shows a plausible
edge — to refine it, not to rescue it.

<AccordionGroup>
  <Accordion title="Good time to optimize" icon="circle-check">
    The strategy passes its backtest, takes a sensible number of trades, and the
    average P\&L per trade is around break-even or better. Now you're asking *"is
    there a better stop, or a slightly different EMA pair?"* — a refinement.
  </Accordion>

  <Accordion title="Not yet" icon="circle-xmark">
    The strategy fails to run, takes a handful of trades, or loses badly across the
    board. No range of stop sizes fixes a broken idea — fix the logic in chat
    first, then sweep.
  </Accordion>
</AccordionGroup>

## The overfitting trap

<Warning>
  **A sweep finds the parameters that fit the *past* best — which is not the same
  as the parameters that will work next.** The more combinations you try, the more
  likely the "winner" is just the one that happened to line up with old noise.
  Treat the best result as a hypothesis, not a guarantee.

  Guard against it:

  * Prefer **wide, robust plateaus** over a single razor-thin peak. If only one
    exact value works and its neighbors fall apart, that's a red flag.
  * Keep sweeps **small and meaningful** — a few parameters, sensible ranges.
  * Re-test the winner on a **different window** (e.g. a more recent stretch you
    didn't sweep over) before you trust it.

  Past performance does not guarantee future results. Always test before you trade.

  For the full picture — why the top-ranked result is often the least
  trustworthy one, and how to read a sweep as a robustness map — see
  [overfitting vs. optimization](/concepts/overfitting-vs-optimization).
</Warning>

## Reading the results

Each sweep finishes as a labelled batch — e.g. *"Optimization 1 · Completed · 6
combinations · Best: \$-2.97 avg"* — naming the winning parameters and the score on
your chosen metric. Open it to see the ranking, or download the full table as a CSV
to compare every combination yourself.

<Tip>
  Found a winner you like? Ask AskFutures to apply it. The chosen parameters become
  a new saved version of the strategy, so you can [compare](/concepts/strategies)
  it against the original.
</Tip>

## Next steps

<CardGroup cols={2}>
  <Card title="Backtesting" icon="chart-line" href="/concepts/backtesting" />

  <Card title="Strategies" icon="diagram-project" href="/concepts/strategies" />

  <Card title="Overfitting vs. optimization" icon="mountain" href="/concepts/overfitting-vs-optimization" />

  <Card title="Is the backtest real?" icon="shield-check" href="/concepts/is-the-backtest-real" />
</CardGroup>
