Soon available

Not on the Chrome Web Store yet

ProRealCode Studio is on its way. As soon as it is published, this button will take you straight to its Chrome Web Store page.

Chapter 20 · Part IV — Alerts and strategies

Try every setting of a strategy. Then distrust the best one.

The optimizer runs your strategy once for every combination of its settings, up to 100,000 runs, and ranks them. It is quick to find a combination that did well on the past. This chapter also shows how to tell a setting that holds from a lucky one.

  1. 01Declare the ranges In the code, then in the window.
  2. 02Choose what “best” means Objective, constraints, data kept aside.
  3. 03Run it and read the ranking Every candidate, ranked, with its flags.
  4. 04Read the surface A plateau or a lonely peak.
  5. 05Use a candidate On the chart, in the code, in the results.
In one optimizationLimit
Runs100,000 at most: the number of values of each setting, multiplied together.
Values per setting100,000 at most.
Settings it can varyThose declared with input(): whole numbers, decimals, average types, switches, and texts with a list of options.
Before you startA strategy is on the chart (chapter 18) and declares its settings with input() (chapter 08). The window works on the chart's bars as they are when you open it.
ProRealCode StudioUser guide · Chapter 20
01 · Task · Declare the ranges

A setting with a floor and a ceiling can be searched.

The strategy of chapter 18, with its period and its stop turned into settings.

1input(len, 20, "Length", int, minval = 5, maxval = 60, step = 5)2input(stopPct, 2, "Stop %", float, minval = 0.5, maxval = 4, step = 0.5)3IF NOT LongOnMarket AND close CROSSES OVER Average[len](close) THEN4  BUY 1 SHARES AT MARKET5ENDIF6SET STOP %LOSS stopPct

Length from 5 to 60 by 5 is 12 values; Stop % from 0.5 to 4 by 0.5 is 8 values: 12 × 8 = 96 runs.

The window Optimize · Guide – strategy: chips for the instrument, 4h, 2,997 bars, the Ranked on and Checked on dates, Balance 10,000, Lot 1, Spread 0 and Out-of-sample last 30 %; then Parameters, two switched-on rows: len Length, int, default 20, from 5 to 60 by 5, 12 values; stopPct Stop %, float, default 2, from 0.5 to 4 by 0.5, 8 values.
“Optimize · strategy” set up with two ranges.
  1. On the strategy's row in the Strategies window, click “Optimize”. The results window has the same button.A window titled “Optimize ·” and the strategy's name opens on the list of its settings.
  2. Check the switches at the start of each row.The first two settings with a floor and a ceiling are switched on. Switch on the ones you want to vary.
  3. Adjust “From”, “To” and “Step” if needed.The code does not change. “Values” counts each row.
The window saysBecause
“This strategy has no searchable input() parameters”The code declares no setting the optimizer can vary.
“Apply a strategy to a live chart before optimizing it”The strategy is not on a chart that is open.
“… keep the search inside the code's bounds”A range goes past the minval or maxval of the code.
ProRealCode StudioUser guide · Chapter 20
02 · Task · Choose what “best” means

Pick one figure to rank on, and keep some bars aside.

“Objective”: rank by

Net / max drawdownat first
Net profit
Profit factor
Max drawdown %lower is better
Average trade
Win rate %
Trades
Trade mean / deviation
Sortino (per trade)
SQN
Exposure %lower is better

Constraints

“Min trades” (30 at first) and “Max drawdown %” (25 at first). A combination that misses one is still listed, after all those that pass.

Data split

“Use out-of-sample” keeps the last bars aside: 30 % at first, any whole number from 0 to 99. The window says it in one line: “Ranking uses the first … bars. The last … are scored and never used to rank.”

The cost

Four boxes: the number of runs, how many run “at a time · on this computer”, the time “per run”, measured on the first 8, and the total “estimated”.

  1. Pick the objective in “Objective”, then set the two constraints.
  2. Leave “Use out-of-sample” on, unless the chart holds few bars.The chips at the top show the dates “Ranked on” and “Checked on”.
  3. Click “Optimize · 96 runs”, or whatever count your ranges give.The runs start. The chart stays live while they do.
ProRealCode StudioUser guide · Chapter 20
03 · Task · Run it and read the ranking

Every combination, ranked, with what is wrong with it.

The Ranking table, by Net / max drawdown · robust · in-sample · 96 candidates, 96 fail a constraint: ten rows with the columns #, Len, Stoppct, Net / max drawdown, Robust, OOS, Net, PF, DD %, Trades, Win %, μ/σ and Flags, the flags reading Too few trades, and on three rows Loses out of sample.
The ranking after a complete optimization.

While it runs

A bar counts the runs done, “Calibrating on the first 8 runs”, then “about … left”. “Pause” and “Resume” keep what is done; “Stop” ends the search. “Now testing” names the combination being run, “Best so far” the leader.

The ranking

One row per combination, 50 per page: its settings, the objective, “Robust”, “OOS” (the same figure on the bars kept aside), then Net, PF, DD %, Trades, Win %, μ/σ and “Flags”. Click a row to see it in detail (task 5).

FlagMeans
Too few tradesUnder “Min trades”.
Drawdown limitOver “Max drawdown %”.
No finite profit factorNo losing trade, so the ratio has no value.
Loses out of sampleIt lost money on the bars kept aside.
PlateauIts neighbours, one step either side, score within 10 % of their average.
Isolated peakIt beats the average of its neighbours by more than half.
Run failedThe strategy stopped with an error on that combination.

“Export CSV”, at the top of the window, saves the whole ranking as optimization.csv, to open in a spreadsheet.

ProRealCode StudioUser guide · Chapter 20
04 · Task · Read the surface

Look for a plateau.
A lonely peak is luck.

The Surface panel, Net / max drawdown · robust · in-sample, X axis len and Y axis stopPct: a grid of 12 by 8 squares from red on the left to green on the right, two dashed red outlines on isolated peaks, a colour scale from -0.38 to -0.04, and the line No candidate meets the constraints. Every result remains in the ranking.
The surface: one square per combination.
  • One square per combination: red for the worst scores, green for the best.
  • “X axis” and “Y axis” choose the two settings shown. With a third, a “Slice” list fixes its value.
  • A dashed red outline marks an isolated peak.
  • Click a square to see that combination in detail.
PlateauIsolated peak neighbours score alikeone step aside, the score collapses
Illustration. The score of one setting across its values. “Robust” ranks each combination on the average of its neighbours: a plateau keeps its rank, a peak loses it.

“Raw” or “Robust”. Raw ranks each combination on its own score. Robust, the setting at first, ranks it on the mean of its neighbours, one step either side on every setting that varies. It waits until those neighbours have run.

The sentence under the surface compares the two winners: “Best raw and robust agree on …”, or names both when they differ. When they differ, prefer the robust one.

ProRealCode StudioUser guide · Chapter 20
05 · Task · Use a candidate

Check it on the bars kept aside, then put it to work.

Click a row of the ranking or a square of the surface. The detail of that combination opens beside the surface: its figures, its equity curve, and three buttons.

In the detailWhat to read
The tilesNet, Net / DD, Max DD %, Trades, Profit factor, Win rate %, then “OOS net” and “OOS Net / DD” on the bars kept aside.
The curveThe equity over the bars it was ranked on, then over the bars kept aside, hatched. Red there means it lost money on bars it had never been ranked on.
The note under it“One continuous run.” A trade opened before the split and closed after it counts on the side where it closed, so “OOS net” can hold gains made before the split.
ButtonEffect
Apply to chartGives the strategy on the chart these settings, as its own Settings would. The code does not change. The status bar reads “Candidate applied to the chart”.
Write as defaultsOpens the code's Variables with these values ready; “Apply” there writes them into the code (chapter 08). For ProRealCode Script codes only.
Open runRuns this combination again and opens it in the results window of chapter 18, with every trade.
  1. Pick a candidate whose flags include “Plateau” and whose “OOS net” is positive.
  2. Click “Open run” and read its trades.The run joins the session's runs, named after the strategy and its settings.
  3. Click “Apply to chart” only if both looks agree.If you changed the code or the chart since the search: “Start a new optimization before applying this candidate.”
ProRealCode StudioUser guide · Chapter 20
Chapter 20 · In short

The best run on the past is a question, not an answer.

Try enough combinations and one of them will have fitted the past by chance. That is over-optimisation: the strategy learned the noise of those bars, not a behaviour of the market. Three habits limit it. Keep bars aside and read “OOS”. Prefer a plateau to a peak. Keep the number of runs small next to the number of trades: when a search makes more than 5 runs per trade of its winner, the window warns that it “may overfit”.

The window says “Use at most 100,000 runs”.

The ranges multiply to more than that. Raise a step, narrow a range or switch off a setting.

Can I keep using the chart while it runs?

Yes. The chart stays live. “Pause” stops the search for now and keeps what is done.

Robust shows no value for some rows.

Their neighbours have not all run, or some failed. Robust waits for a full set.

Where do the ranges come from?

From minval, maxval and step in the code's input() lines. Changing them in the window does not touch the code.

No investment advice. An optimization ranks what combinations of settings would have done on past prices, under the assumptions of chapter 18. A high rank is not a forecast, not investment advice and not a recommendation to buy or sell anything. Past results do not predict future results.

Next

Chapter 21 · The dashboard

Part V opens: your markets side by side, in windows you build.

ProRealCode StudioUser guide · Chapter 20
ProRealCode ProRealCode
Loading...