Loading…

Custom Optimisation Criterion

Optimising for raw net profit routinely selects parameter sets that owe most of their result to one enormous trade, which is exactly the kind of setting that fails on the next few months of unseen data. The Strategy Tester supports optimising against a custom value instead - select "Custom max" as the optimisation criterion and it calls the EA's OnTester() function once at the end of every pass and ranks results by whatever number that function returns. This EA implements two such metrics, selectable with an input. The System Quality Number is the mean profit per closed trade divided by its standard deviation, scaled by the square root of the trade count - it rewards a strategy that wins consistently over one that wins big but erratically. Equity-curve linearity is the R-squared of the cumulative closed-trade equity curve against a straight line fitted to it - a smoothly rising curve scores close to 1.0, a lumpy one scores lower even with identical final profit. Neither metric is available from the tester's built-in statistics, so the EA builds its own trade-by-trade ledger during the run and both figures are computed from that ledger inside OnTester(). The underlying strategy is a plain fast/slow EMA crossover with an ATR-scaled stop and target, deliberately unremarkable - the point of this EA is the custom criterion, not the signal. Swap EvaluateSignal() for any other entry logic and the criterion still works, because it is computed from closed-order history, not from the signal itself. Notes and limitations - The System Quality Number here is computed from raw trade profit in account currency, not from R-multiples (profit divided by initial risk) as in Van Tharp's original formulation - a simplification that still rewards consistency over one outsized trade, but is not directly comparable to SQN figures computed the strict way. - Both metrics need a meaningful number of closed trades to mean anything; with fewer than two trades SQN returns 0, and linearity needs at least three. - The placeholder signal has no claimed edge; it exists only to generate a trade ledger for the criteria to be computed from. - OnTester() only runs inside the Strategy Tester - attaching this EA to a live or demo chart trades normally but the custom criterion is never evaluated outside a test pass. - The MQL5 ledger assumes each position closes in one exit deal (no partial closes); see MQL4 VERSUS MQL5 DIFFERENCES above.

Expert advisor · MT5 · v1.0 · Free

About Custom Optimisation Criterion

Custom Optimisation Criterion is an expert advisor (trading robot) for MetaTrader 5 based on Moving Average and ATR. It has 11 settings, such as Criterion, Lots and Fast ma period. MQL source code included.

Files in the download: CustomOptimisationCriterion.ex5, CustomOptimisationCriterion.mq5, CustomOptimisationCriterion.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
CriterionCRITERION_SQNWhich robust fitness value OnTester() returns
Lots0.10Fixed lot size for the placeholder signal
Fast ma period10Fast EMA period for the placeholder signal
Slow ma period30Slow EMA period for the placeholder signal
Atr period14ATR period for the stop and target
Stop atr multiple2.0Stop loss distance, in ATR multiples
Take profit atr multiple3.0Take profit distance, in ATR multiples (0 = none)
Magic number20260833Magic number for orders opened by this EA
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts per trade action before giving up
Retry wait ms500Pause between attempts, milliseconds

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Moving Average · ATR · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria