# Optimization Fundamentals

Optimization is the process of testing many input combinations on historical data.

It can help you compare settings, but it can also create curve-fit results that look good in Strategy Tester and fail later. Treat optimization as research, not proof.

warning

Never evaluate a setup only on the same data used to choose its settings. Always reserve later data for out-of-sample testing and then forward test on demo.

## What Optimization Actually Does[​](#what-optimization-actually-does "Direct link to What Optimization Actually Does")

In MT4, optimization runs repeated Strategy Tester passes with different input values.

For example:

```
Run 1: RiskPercent=1.0, MaxSpread=3.0

Run 2: RiskPercent=1.5, MaxSpread=3.0

Run 3: RiskPercent=2.0, MaxSpread=3.0

...
```

The optimizer then sorts the results by a selected metric.

The top result is the best result inside that specific test setup. It is not automatically the best setting for live trading.

## Backtesting vs Optimization[​](#backtesting-vs-optimization "Direct link to Backtesting vs Optimization")

| Task               | What it does                                                | Main risk                     |
| ------------------ | ----------------------------------------------------------- | ----------------------------- |
| Backtest           | Tests one fixed setup                                       | May be too narrow             |
| Optimization       | Tests many setup variations                                 | Can overfit historical data   |
| Out-of-sample test | Tests selected settings on unseen later data                | Can still fail live           |
| Forward test       | Tests on demo/live market feed without historical hindsight | Takes time and still has risk |

## Why Optimize[​](#why-optimize "Direct link to Why Optimize")

Good reasons:

* Compare a small number of important settings.
* Check whether a preset is sensitive to small changes.
* Adapt a setup to a symbol or timeframe it was designed for.
* Review whether a setting still behaves acceptably after a market regime change.
* Build a candidate preset for later validation.

Bad reasons:

* Trying to recover after one losing trade.
* Searching for a perfect equity curve.
* Optimizing every input because it is available.
* Re-running until one backtest looks impressive.
* Using a short period that happens to contain favorable market behavior.

## Curve Fitting[​](#curve-fitting "Direct link to Curve Fitting")

Curve fitting happens when settings are fitted too closely to historical noise.

Common warning signs:

* Too many optimized inputs.
* Very small step sizes.
* Very short test periods.
* Perfect or near-perfect results.
* One exact value works, nearby values fail.
* The setup fails when tested on later data.
* Most profit comes from one trade or one short period.

## How Many Inputs To Optimize[​](#how-many-inputs-to-optimize "Direct link to How Many Inputs To Optimize")

For most users, optimize only one to three important settings at a time.

More inputs create more combinations and more chances to find a result that only worked by coincidence.

Good candidate inputs are settings that directly affect entry logic, stop placement, trade management, or filters.

Avoid optimizing:

* Magic number
* Chart colors
* Alert preferences
* Detailed journal verbosity
* Display-only settings
* Many on/off switches at the same time

## Risk Settings[​](#risk-settings "Direct link to Risk Settings")

Be careful optimizing `RiskPercent` or lot-size settings.

Risk changes the size of gains and losses. It can make a backtest look better without improving the underlying trade logic.

A safer workflow is:

1. Evaluate the strategy logic at conservative risk.
2. Check drawdown and trade distribution.
3. Choose risk only after the setup has passed validation.

## Ranges And Step Sizes[​](#ranges-and-step-sizes "Direct link to Ranges And Step Sizes")

Use realistic ranges.

Too narrow:

```
BuyBuffer = 7.0 to 7.2, step 0.1
```

This may only search around a value you already chose.

Too wide:

```
BuyBuffer = 1.0 to 100.0, step 0.1
```

This creates too many passes and includes unrealistic values.

Better:

```
BuyBuffer = 5.0 to 20.0, step 2.5
```

This checks a practical range without pretending that one decimal point is meaningful.

## Data Split[​](#data-split "Direct link to Data Split")

Use separate periods:

```
In-sample:

2020-2023

Use this period to optimize.



Out-of-sample:

2024-2025

Use this period to test the selected settings without changing them.
```

If the setup fails out-of-sample, do not keep adjusting it on that same out-of-sample period until it passes. That turns the out-of-sample period into another optimization period.

## Optimization Criteria[​](#optimization-criteria "Direct link to Optimization Criteria")

Avoid optimizing only for net profit. Net profit can reward excessive risk.

Common metrics to review together:

* Profit factor
* Maximum drawdown
* Expected payoff or average trade
* Trade count
* Consecutive losses
* Recovery factor
* Stability across periods

No single metric is enough.

## Complete Search And Genetic Optimization[​](#complete-search-and-genetic-optimization "Direct link to Complete Search And Genetic Optimization")

Complete search tests every combination in the selected grid. It finds the best result inside that grid, but the grid itself may still be badly chosen.

Genetic optimization samples the search space and can be faster when there are many combinations. It may miss useful areas and can still overfit.

Practical workflow:

1. Use broad, sensible ranges.
2. Use genetic optimization only for early exploration if the search is too large.
3. Narrow the range.
4. Re-test with a smaller complete search if practical.
5. Validate on out-of-sample data.

## Modeling Quality[​](#modeling-quality "Direct link to Modeling Quality")

Faster modeling can be useful for early screening, but validation should be done with the most realistic settings available in your MT4 environment.

Be especially careful when the EA uses:

* Tight stops
* Trailing stops
* Pending orders
* Spread filters
* Intrabar entry or exit behavior

These can be more sensitive to tick modeling, spread, slippage, and broker execution.

## Practical Workflow[​](#practical-workflow "Direct link to Practical Workflow")

1. Define the question you are testing.
2. Choose one to three meaningful inputs.
3. Use practical ranges and step sizes.
4. Split data into in-sample and out-of-sample periods.
5. Run the optimization.
6. Review the top group, not only the top row.
7. Look for parameter clustering and stable areas.
8. Test selected settings out-of-sample.
9. Run sensitivity checks.
10. Forward test on demo before live use.

## Decision Rules[​](#decision-rules "Direct link to Decision Rules")

Reject or pause a setup if:

* Out-of-sample performance collapses.
* Nearby parameter values fail badly.
* Drawdown exceeds your tolerance.
* Trade count is too small to review.
* Costs or spreads are unrealistic.
* The result depends on one unusual trade.

## Next Steps[​](#next-steps "Direct link to Next Steps")

* [Running Optimizations](/banana-ea/mt4/docs/v4/backtesting-optimization/running-optimizations.md)
* [Analyzing Results](/banana-ea/mt4/docs/v4/backtesting-optimization/analyzing-results.md)
* [Advanced Optimization](/banana-ea/mt4/docs/v4/backtesting-optimization/advanced-optimization.md)
* [Validation And Forward Testing](/banana-ea/mt4/docs/v4/backtesting-optimization/validation-forward-testing.md)
