# Backtesting & optimization

### Run a backtest

{% stepper %}
{% step %}

### Prepare the strategy

* Ensure **Market Data** exists.
* Ensure at least one **Buy Order** or **Sell Order** exists.
* Check all required inputs are connected.
  {% endstep %}

{% step %}

### Configure

* Symbol and timeframe are pulled from **Market Data**.
* Set a date range or period (example: `2y`, `30d`).
* Confirm trading direction (auto or manual override).
  {% endstep %}

{% step %}

### Run and review

Review:

* Total return, Sharpe, max drawdown, win rate.
* Trade statistics (profit factor, avg return).
* Equity curve.
* Executed trades list.
  {% endstep %}
  {% endstepper %}

### Trading direction

#### Auto-detection

* Long only: only buy orders.
* Short only: only sell orders.
* Long & short: both.

#### Manual override

Use the backtest configuration to override auto-detection. Manual selection wins.

### Optimization mode

Use optimization when a strategy is stable. Keep trials low until the graph is correct.

* Choose optimization metric (Sharpe, total return, profit factor).
* Set number of trials (example: `50` to start).
* Enable parallel execution if available.

{% hint style="warning" %}
Optimization can overfit. Validate on a separate date range.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.quantbytes.net/bot-studio/backtesting-and-optimization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
