millionchart
فارسی

Lesson 15: Testing in the Strategy Lab

Test a strategy over months of history in the Strategy Lab. Open it, choose the data and the costs, read the report and the trades, and know its limits.

Lesson 15 of 18. The chart shows a strategy's trades on the bars you are looking at. The Strategy Lab goes further: it tests the strategy over a period you choose, with a finer model of the price inside each candle, with the costs you set, and writes a full report. Everything it shows is a simulation on past prices. No order is ever sent to a broker.

A strategy to test#

Save this strategy in My scripts first. It has inputs, so the Lab can show them and change them.

Cuprel
strategy "Lab crossover"
    edition = 1
    display = price

setup:
    capital = 10000
    size = sizing.lots(0.1)
    fees = fee.per_lot(3.5)

execution:
    spread = 20 ticks
    slippage = 2 ticks

input fast_length: int = 10, name = "Fast length", min = 1, max = 100
input slow_length: int = 30, name = "Slow length", min = 2, max = 300
input stop_atr: float = 2.0, name = "Stop (ATRs)", min = 0.5, step = 0.5
let fast = tech.ema(close, fast_length)
let slow = tech.ema(close, slow_length)
let atr = tech.atr(14)
if tech.crosses_above(fast, slow)
    orders.enter("Long", side.long)
if tech.crosses_below(fast, slow)
    orders.close("Long")
if position.size > 0
    orders.exit("Stop", "Long", stop = position.avg_price - stop_atr * atr)
plot(fast, name = "Fast", color = color.green)
plot(slow, name = "Slow", color = color.red)

Opening the Lab#

There are three ways in:

  • the flask button Strategy lab — backtest a strategy in the strip at the top of the chart (on a narrow screen it is under More › Strategy lab);
  • Test in the script editor, shown when the script is a strategy: it saves the script and runs it in the Lab at once;
  • from the Lab itself: its Write a new strategy button opens the editor with a small working strategy.

The Lab opens as a panel under the chart, on the chart's symbol and timeframe. Drag its top border to make it taller. Its tabs are Setup, Report, Trades, Optimise, Robustness and Journal.

Setup#

FieldWhat it decides
Strategywhich of your scripts is tested; indicators are listed apart, because they place no orders
Symbol, Timeframe, Periodwhat is tested and over which dates (a month up to five years, or Custom with From – to)
Price modelhow the price moves inside each candle: Candles only (the quickest, and it reaches furthest back), 1-minute candles (every minute inside the candle; the default), Every tick (built) (ticks built from each minute) or Real ticks (the recorded bid and ask, where they exist)
Stop and target in one candlewhat to assume when the data cannot say which came first: Classic rule (a rising candle dips first), Assume the worse (careful) or Assume the better (optimistic)
Deposit (USD), Leveragethe account of the test (10,000 and 1:100 until you change them)
Order sizeThe strategy's own, Fixed lots, Share of equity or Money per trade
Spread, Commission, Slippage, Swaps, Order delaythe costs of the test
Strategy inputsthe inputs of the script, for this test only

The settings in the script's header are used for every cost you have not changed in Setup: here the test uses a spread of 20 ticks, a slippage of 2 ticks and 3.5 per lot. A field you change in Setup wins over the header. The margin comes from the header's margin: block. Then press Run test.

Report and trades#

The Report opens with one line: the net profit and its percentage. Under it are the main figures (Net profit, Profit factor, Win rate, Max drawdown, Trades, Average trade, Sharpe ratio, Recovery factor), the equity and drawdown over the test, a table of months, the result by hour and by weekday, and All figures. The report also says plainly:

  • when there are too few trades to judge (aim for 100 or more);
  • whether the average trade could be luck;
  • how many candles reached both a stop and a target, so that the order was a guess, and how many trades depend on that guess;
  • which orders the broker model refused (under the smallest lot, or beyond the free margin);
  • when the candles come from a data vendor's archive rather than the broker's own prices.

Trades lists every trade with its side, entry, exit, size, profit, time held and how it ended. Filter it by wins, losses or guessed exits, and click a trade to see it on the chart. While the Lab is open the chart marks the trades of the last test.

Journal holds the notes of the script and of the broker model after a test.

Optimise and robustness#

Optimise runs the test many times with the inputs you tick, each over a range (From, To, Step), and ranks the results. It can keep the end of the period unseen (Forward test) and test every setting it found on it, and it ignores settings with too few trades. A Stable column says whether the neighbouring settings agree, and a note says how likely the top result is to be luck after so many tries.

Robustness asks how far a result can be trusted: a Monte Carlo test (the same trades in other orders, drawn again, or with some missed), a prop challenge check (would the strategy have passed the rules of a funded-account challenge), and walk-forward (optimise, then trade the next unseen stretch, again and again).

What your plan includes#

Free chart planMillionChart Pro
Test runs10 a monthno limit
History a test may reachthe last 92 daysno limit
Optimisation, walk-forward, Monte Carlonoyes

Each Run test, including the editor's Test, counts as one run. When the month's runs are used, the Lab says so and points to the plans. A guest cannot run tests.

Try changing#

  • Change Price model from 1-minute candles to Candles only and run again. The Data part of the report says how many guesses inside a candle mattered.
  • Set Spread to a fixed value twice as large and run again. Compare the net profit with the first test.
  • Change the Period to the last month and then to a longer one (on the free plan a test reaches back 92 days). Notice how few trades a short period gives.
  • Under Strategy inputs, change stop_atr from 2 to 1, run again, and count in Trades how many trades ended at the stop.

Checkpoint#

  • You can open the Lab three ways and choose the strategy, the symbol, the timeframe and the period.
  • You know which cost fields come from the script's header and that a field you change wins.
  • You can read the main figures of the report and find a trade on the chart.
  • You know what the free plan includes and that every result is a simulation.

Next: Lesson 16: Results that look too good