# Optimization

MT5's built-in optimizer runs the EA across a grid of parameter values and ranks results by a fitness score. Banana EA MT5 returns a **robustness score** rather than raw profit, which is a more meaningful selection criterion for live performance.

## Robustness fitness function[​](#robustness-fitness-function "Direct link to Robustness fitness function")

The EA's tester fitness is based on:

* **Daily equity returns** — normalised to reduce scale bias across different account sizes
* **Probabilistic Sharpe Ratio (PSR)** — statistical significance of the Sharpe Ratio against a benchmark
* **Bounded fitness** — results are bounded to prevent extreme outliers dominating the ranking
* **Hard gates** — results failing minimum trade count or maximum drawdown criteria are penalised heavily

The optimizer therefore selects configurations that are **consistently profitable and low-drawdown** rather than just high raw-profit over the test period.

## Setting up an optimization run[​](#setting-up-an-optimization-run "Direct link to Setting up an optimization run")

1. Open Strategy Tester and configure as per [Backtesting Setup](/banana-ea/mt5/docs/backtesting/setup.md).
2. Select **Optimization** mode in the tester.
3. In the **Inputs** tab, enable the checkboxes on the parameters you want to optimize and set their Start / Step / Stop ranges.
4. Choose an optimization criterion — select **Custom max** to use the EA's built-in robustness score.
5. Click **Start**.

MT5 supports multi-threaded and cloud optimization natively, which significantly reduces run times on multi-core machines.

## Recommended inputs to optimize[​](#recommended-inputs-to-optimize "Direct link to Recommended inputs to optimize")

Good starting candidates (keep ranges narrow to avoid over-fitting):

| Input                                                  | Suggested range                             |
| ------------------------------------------------------ | ------------------------------------------- |
| Buy stop-loss buffer in pips                           | 0.0-2.0, step 0.25                          |
| Sell stop-loss buffer in pips                          | 0.0-2.0, step 0.25                          |
| Default TP in R, 0 disables                            | 1.0-5.0, step 0.5                           |
| Break-even trigger in R                                | 0.5-3.0, step 0.25                          |
| Partial close 1 trigger R                              | 0.5-3.0, step 0.25                          |
| Trailing activation R                                  | 0.5-4.0, step 0.25                          |
| Fast EMA period / Slow EMA period / Slowest EMA period | Keep ranges narrow around the preset values |

## Avoiding over-fitting[​](#avoiding-over-fitting "Direct link to Avoiding over-fitting")

* **Out-of-sample validation:** optimize on years 1–3, validate on year 4–5 with no further changes.
* **Prefer robust zones:** a configuration that is second-best on the in-sample period but holds up out-of-sample beats the top in-sample result.
* **Limit the number of optimized inputs:** the more parameters you vary simultaneously, the higher the risk of curve-fitting to historical noise.
* Use clear preset filenames and notes so you can track which set you are running on live charts.

## Saving best settings as a preset[​](#saving-best-settings-as-a-preset "Direct link to Saving best settings as a preset")

After optimization:

1. In the Results tab, right-click the best result → **Set input parameters**.
2. In the Inputs tab, click **Save** to write a `.set` file.
3. Load this preset in the EA inputs dialog when attaching to a live chart.
